Java横向卷轴游戏
我正在制作我自己的马里奥版本。我遇到的一个问题是如何完成游戏的横向滚动部分。但我不太知道如何将 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);
}
}
}
I am making my own version of mario. One problem im running into is how to do the side scrolling part of the game. But i dont quite know how to implement the imageableX and imageableY into the world display.
Any help would be much appreciated! Here is what i have so far:
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会发现在 PaintComponent() 方法中实现 X 和 Y 位置定义更容易,而不是将过程抽象到其他方法并每次都传递 Player 对象。尝试为视口尺寸定义常量(VIEWPORT_WIDTH、VIEWPORT_HEIGHT 等)。在您的paintComponent()方法中,将它们与玩家的坐标结合起来引用,以将您的角色定位在屏幕上。还要确保使用相对坐标:
如果您希望视口始终保留在世界中,即使您的角色移动到屏幕边缘,请正常执行所有位置计算,但要进行一些检查在实际绘制任何内容之前,您可以更改视口位置,以防它碰巧超出屏幕。
祝你好运。
You might find it easier to implement your X and Y position definitions in your paintComponent() method instead of abstracting the process to other methods and passing a Player object every time. Try defining constants for your viewport dimensions (VIEWPORT_WIDTH, VIEWPORT_HEIGHT, etc). In your paintComponent() method, reference these in conjunction with your player's coordinates to position your character on the screen. Be sure to use relative coordinates, as well:
If you want your viewport to remain in the world at all times, even when your character moves to the edges of the screen, do all of your positional calculations normally, but put in a few checks right before actually drawing anything so you can make changes to your viewport location in case it happens to go offscreen.
Good luck.