gpt4 book ai didi

java - 平台碰撞和跳跃的奇怪行为

转载 作者:行者123 更新时间:2023-12-02 06:20:17 25 4
gpt4 key购买 nike

我使用 AWT 和 Swing 用 java 开发了一个简单的游戏。你所要做的就是用空格键控制一个方 block ,它会在永无休止的平台流上上下跳跃。

Weeee!

一切正常,但是当玩家跳下一个平台,按住空格键并降落在另一个平台上时,他/她会随机弹跳或简单地穿过平台。据我所知,错误出在玩家行为的某个地方。

这是测试它所需的所有代码:

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class main {

private static JFrame gui;
private static Container pane;
private static JPanel panel;

public static void main(String[] args) {
gui = new JFrame();
gui.setSize(500,500);
gui.setTitle("An Experiment");
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane=gui.getContentPane();
panel = new ColorPanel(Color.WHITE, 500, 500);
pane.add(panel);
gui.setVisible(true);
gui.pack();
}

//****************
//ColorPanel class
//****************

private static class ColorPanel extends JPanel{
private static final long serialVersionUID = -1101242028703991986L;
private Timer timer, dtimer;
private KeyListener k;
private Platform platform;
private Player player;
private int difficulty, score;

public ColorPanel(Color backColor, int width, int height){
setBackground(backColor);
setPreferredSize(new Dimension(width, height));
difficulty=-1;
score=0;

platform = new Platform(new Point(240,250), 50);

player=new Player(width/2, height/2-100);

k=new keyboardListener();
addKeyListener(k);
setFocusable(true);

timer = new javax.swing.Timer(15, new timerListener());
timer.start();

dtimer = new javax.swing.Timer(50000, new difficultyListener());
dtimer.start();
}

public void paintComponent(Graphics g){
super.paintComponent(g);
if(player.getY()<500){
platform.draw(g);
player.draw(g);
}else{
timer.stop();
dtimer.stop();
setBackground(Color.BLACK);
g.setColor(Color.RED);
g.drawString("Game Over.", 215, 230);
g.setColor(Color.YELLOW);
g.drawString("Platforms cleared: "+score, 200, 240);
g.drawString("End difficulty: "+Math.abs(difficulty), 200, 250);

}
}

private class timerListener implements ActionListener{
public void actionPerformed(ActionEvent e){
//Player control
player.move(platform);

repaint();
}
}

private class difficultyListener implements ActionListener{
public void actionPerformed(ActionEvent e){
difficulty--;
if(difficulty<=-10){
dtimer.stop();
}
}
}
private class keyboardListener implements KeyListener{
@Override
public void keyPressed(KeyEvent e){
//space=32
if(e.getKeyCode()==32){
player.setJumping(true);
}
}
@Override
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==32){
player.setJumping(false);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
}
}

//************
//Player class
//************

private static class Player {

private int x, y, w, h, initialSpeed, speed, endingSpeed, acceleration, jumpHeight, timeCount;
private boolean isJumping, onPlatform;
private Timer accel;

public Player(int x1, int y1){
x=x1;
y=y1;
w=15;
h=15;
isJumping=false;
onPlatform=false;
speed=3;
accel=new Timer(50, new timerListener());
}

public void draw(Graphics g){
g.drawRect(x, y, w, h);
}

public void move(Platform p){
//platform collision
onPlatform=false;
if((x>p.getP1().getX() && x<p.getP2().getX()) || (x+w>p.getP1().getX() && x+w<p.getP2().getX())){
if(Math.abs(y+h-p.getP1().getY())<2){
onPlatform=true;
}
}
//speed update and movement
if(isJumping && y>0 && jumpHeight>=-140){
y+=speed;
jumpHeight+=speed;
}else if(onPlatform){
speed=0;
}else if(!onPlatform){
jumpHeight=0;
if(!isJumping && speed!=3){
setSpeed(3);
}
y+=speed;
}
if(jumpHeight<-140){
jumpHeight=0;
}
}

public void setJumping(boolean b){
if(onPlatform && b && jumpHeight>=-140){
isJumping=true;
speed=-4;
}else{
isJumping=false;
setSpeed(3);
}
}

public int getY(){
return y;
}

public void setSpeed(int s){
//speed=starting speed + acceleration*time
timeCount=0;
initialSpeed=speed;
endingSpeed=s;
if(speed>s){
acceleration=-1;
}else{
acceleration=1;
}
accel.start();
}

private class timerListener implements ActionListener{
public void actionPerformed(ActionEvent e){
timeCount++;
if(Math.abs(speed-endingSpeed)<=0.5){
accel.stop();
}else if(acceleration<0 && initialSpeed+acceleration*timeCount>0){
speed--;
}else{
speed=initialSpeed+acceleration*timeCount;
}
}
}

}

//**************
//Platform class
//**************

private static class Platform {

private Point p1, p2;

public Platform(Point p, int l){
p1=p;
p2=new Point((int)(p1.getX()+l), (int)(p1.getY()));
}

public Point getP1(){
return p1;
}
public Point getP2(){
return p2;
}

public void draw(Graphics g){
g.drawLine((int)(p1.getX()), (int)(p1.getY()), (int)(p2.getX()), (int)(p2.getY()));
}

}

}

最佳答案

很难百分百确定,但它“看起来”正在发生的是,你在太空事件想要做什么和你的“重力”想要做什么之间进行斗争.

请记住,当空格关闭时,您将收到多个按键事件,具体取决于操作系统的键盘延迟/计时(例如,您可能每秒收到多个事件)

而不是让用户像这样按住按键......

private class keyboardListener implements KeyListener{
@Override
public void keyPressed(KeyEvent e){
//space=32
if(e.getKeyCode()==32){
player.setJumping(true);
}
}
@Override
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==32){
player.setJumping(false);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
}

尝试忽略任何新的Space事件...

我要做的是添加一个名为 hasJumped 的标志。虽然它是true,但您将简单地忽略任何新的跳转事件。一旦用户释放空格,您就重置标志,以便再次按下空格将导致新的跳转。

在你的游戏引擎中,我会重置跳跃标志,这样一旦你注册了跳跃发生的事实,直到他们释放并按下空格再说一次,它不会触发跳转...

private class keyboardListener implements KeyListener{
@Override
public void keyPressed(KeyEvent e){
//space=32
if(e.getKeyCode()==32){
if (!player.hasJumped()) {
player.setJumping(true);
}
}
}

@Override
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==32){
player.hasJumped(false);
}
}
//...
}

我还建议您查看Key Bindings通过KeyListener

您可以查看类似 JApplet creates a ball that bounces and gets progressively less high in Java 的内容例如

关于java - 平台碰撞和跳跃的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21081972/

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