?? gamecharmovable.java
字號:
package net.java.gamebase.core;
import java.awt.Component;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
public class GameCharMovable
extends GameChar {
private GameControlKeySet keySet;
private int deslocamento = 5;
public GameCharMovable(String name, int x, int y, int width, int height) {
super(name, x, y, width, height);
keySet = new GameControlKeySet();
}
public void keyPressed(int key) {
if (keySet.keyPressed(this, key)) {
repaint();
}
}
public boolean left() {
return moveTo(getLocation().x - deslocamento, getLocation().y);
}
public boolean right() {
return moveTo(getLocation().x + deslocamento, getLocation().y);
}
public boolean down() {
return moveTo(getLocation().x, getLocation().y + deslocamento);
}
public boolean up() {
return moveTo(getLocation().x, getLocation().y - deslocamento);
}
public boolean moveTo(int x, int y) {
boolean canMove = true;
if (x >= 0 && y >= 0 && (x + getWidth() <= getParent().getWidth())
&& (y + getHeight() <= getParent().getHeight())) {
Rectangle toTest = getBounds();
toTest.x = x;
toTest.y = y;
boolean repaint = false;
Component[] brothers = getParent().getComponents();
for (int i = 0; i < brothers.length; i++) {
// not the same and implents GameChar
if (!brothers[i].equals(this)
&& brothers[i] instanceof GameChar) {
if (toTest.intersects(brothers[i].getBounds())) {
canMove = false;
repaint = ( (GameChar) brothers[i]).colideTo(this);
repaint = repaint || colideTo( (GameChar) brothers[i]);
if (repaint) {
getParent().repaint();
}
break;
}
}
}
if (canMove) {
setLocation(x, y);
}
}
else {
hitWall();
canMove = false;
}
return canMove;
}
public void hitWall() {
message("sai da parede !");
}
public int getDeslocamento() {
return deslocamento;
}
public void setDeslocamento(int deslocamento) {
this.deslocamento = deslocamento;
}
public GameControlKeySet getKeySet() {
return keySet;
}
public void setKeySet(GameControlKeySet keySet) {
this.keySet = keySet;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -