gpt4 book ai didi

Java 2D 平台游戏重力改进

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

我已经开始用Java制作一个小游戏,并加入了碰撞和重力形式,玩家的速度不会像现实生活中那样在下落时增加。我想改进这种重力形式,使其更加真实,我还为地板设置了碰撞 block ,当玩家离开地板时,它会保持在那个高度并且不会掉落。

下面是我的 Player 类:

    import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Player {
private int x, y, width, height, lx, ly, dx, dy;
private final int FallSpeed = 2;
private final long JumpingTime = 8;
public boolean jumping = false, falling = true;

public Player(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
lx = x;
ly = y;
dx = x;
dy = y;
new Thread(new gravityThread()).start(); // Make user fall in case they are in the air
}

public void setX(int x) {
this.x = x;
lx = x;
dx = x;
}

public void setY(int y) {
this.y = y;
ly = y;
dy = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}

public void update() {

}

public void move(int ax, int ay) {
dx += ax;
dy += ay;
checkCollisions();
}

public void checkCollisions() {
if (collided()) {
falling = false;
dx = lx;
dy = ly;
}
x = dx;
y = dy;
lx = x;
ly = y;
}

public boolean collided() {
Rectangle desired = new Rectangle(dx, dy, width, height);
for (Rectangle r : CollisionManager.collisions) if (r.intersects(desired)) return true;
return false;
}

public void jump() {
falling = false;
jumping = true;
new Thread(new gravityThread()).start();
}

public void render(Graphics2D g2d) {
g2d.setColor(Color.black);
g2d.fill(getBounds());
}

private class gravityThread implements Runnable {

int i = 0;

@Override
public void run() {
while(jumping) {
try {
i++;
Thread.sleep(JumpingTime);
move(0, -FallSpeed);
if (i == 40) {
jumping = false;
falling = true;
}
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
while(falling) {
try {
Thread.sleep(JumpingTime);
move(0, FallSpeed);
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
}
}

在另一个处理将所有内容绘制到窗口的类中,我使用以下代码片段控制玩家移动:

    public void onUpdate() {
if (this.screenFactory.getGame().getKeyboardListener().isKeyPressed(KeyEvent.VK_A))
player.move(-WalkSpeed, 0);
if (this.screenFactory.getGame().getKeyboardListener().isKeyPressed(KeyEvent.VK_D))
player.move(WalkSpeed, 0);

if (this.screenFactory.getGame().getKeyboardListener().isKeyPressed(KeyEvent.VK_SPACE)) {
if (!player.jumping && !player.falling)
player.jump();
}

if (player.getY() >= screenHeight - player.getHeight()) {
player.setY(screenHeight - player.getHeight());
player.falling = false;
}
if (player.getY() <= 0)
player.setY(0);
if (player.getX() >= screenWidth - player.getWidth())
player.setX(screenWidth - player.getWidth());
if (player.getX() <= 0)
player.setX(0);
}

最佳答案

  • 重力为 9.81 m/s²
  • 获取你的“draw-update-speed”,这是 s (实际上,你应该每秒至少更新 30 次。计算时请记住这一点)
  • 获取长度的定义,这是 m

每帧的对象位置取决于当前速度 v 和加速度(重力)a。东西下落的时间越长,下落的速度就越快......

编辑:为了使坠落/跳跃时的侧向运动更加真实,您需要使用 sin()、cos()。

关于Java 2D 平台游戏重力改进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37435472/

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