2
我正在制作我自己的马里奥版本。我遇到的一个问题是如何做侧面滚动游戏的一部分。但我不知道如何将imageableX和imageableY实现到世界展示中。 任何帮助将不胜感激!以下是我迄今为止:Java侧面滚动游戏
public class World extends JPanel implements Runnable, KeyListener{
private static final int BEGINNING_X = 0, ENDING_X = 10000;
private static final int TOP_Y = 0, BOTTOM_Y = 5000;
private boolean inGame = false;
private Player player;
//set up a new world with a player
public World(Player player){
inGame = true;
this.player = player;
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
//the world size
g.drawRect(0, 0, BEGINNING_X, ENDING_X);
}
//check to see if the new x postition is valid on the game board
public boolean isValidXPosition(double x){
if(x < BEGINNING_X || x > ENDING_X){
return false;
}
return true;
}
public double imageableStartX(Player player){
if(player.returnX() - 100 <= BEGINNING_X)
return BEGINNING_X;
else
return player.returnX() - 100;
}
public double imageableTopY(Player player){
if(player.returnY()-100 <= BOTTOM_Y)
return BOTTOM_Y;
else
return BOTTOM_Y - 100;
}
public double imageableEndX(Player player){
if(player.returnX() + 200 <= ENDING_X)
return ENDING_X;
else
return player.getX() + 200;
}
public double imageableBottomY(Player player){
if(player.returnY()+100 >= BOTTOM_Y)
return BOTTOM_Y;
else
return BOTTOM_Y + 100;
}
public void run() {
while(inGame){
//TODO run game code
}
}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
player.moveRight();
}
if(e.getKeyCode() == KeyEvent.VK_LEFT){
player.moveLeft();
}
if(e.getKeyCode() == KeyEvent.VK_SPACE){
player.setSprint(true);
}
if(e.getKeyCode() == KeyEvent.VK_UP){
player.Jump();
}
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP){
player.Descend();
player.setFalling(true);
}
if(e.getKeyCode() == KeyEvent.VK_SPACE){
player.setSprint(false);
}
}
}
你可能会考虑使用2D平铺引擎来处理绘图http://stackoverflow.com/questions/903760/java-2d-game-engine-for-tile-based-game – LumpN 2011-04-07 08:10:15
对不起,这听起来有点愚蠢,但是从来没有从事瓦片引擎的工作,你能简单地解释他们做什么以及如何制作一个? – mimis40 2011-04-07 20:15:26