gpt4 book ai didi

Java横向卷轴游戏

转载 作者:行者123 更新时间:2023-11-30 11:54:34 25 4
gpt4 key购买 nike

我正在制作我自己的马里奥版本。我遇到的一个问题是如何进行游戏的横向滚动部分。但我不太清楚如何将 imageableX 和 imageableY 实现到世界显示中。任何帮助将非常感激!这是我到目前为止所拥有的:

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);
}
}
}

最佳答案

您可能会发现在 paintComponent() 方法中实现 X 和 Y 位置定义比将过程抽象为其他方法并每次传递一个 Player 对象更容易。尝试为您的视口(viewport)尺寸定义常量(VIEWPORT_WIDTH、VIEWPORT_HEIGHT 等)。在您的 paintComponent() 方法中,将这些与您的玩家坐标一起引用以在屏幕上定位您的角色。一定要使用相对坐标:

// center the screen on the player, though you can modify it to do otherwise
int viewportX = player.returnX() - VIEWPORT_WIDTH/2;
int viewportY = player.returnY() - VIEWPORT_HEIGHT/2;

g.setColor(Color.WHITE);
g.fillRect(viewportX, viewportY, VIEWPORT_WIDTH, VIEWPORT_HEIGHT);

如果您希望您的视口(viewport)始终保留在世界中,即使您的角色移动到屏幕边缘,请正常进行所有位置计算,但在实际绘制任何东西之前进行一些检查,以便您可以更改您的视口(viewport)位置,以防它碰巧离开屏幕。

祝你好运。

关于Java横向卷轴游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5575799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com