gpt4 book ai didi

java弹跳球碰撞不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 14:25:43 24 4
gpt4 key购买 nike

我一直在尝试用java制作这个弹跳球游戏,这是我的代码。我无法理解当球撞到墙上时速度的变化。当我尝试改变速度时,球只是不动,而是停留在一个位置并振动。我不知道问题出在哪里,但球没有从墙上弹开。有人能帮我吗?

    import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ballGame.SimpleSprite;


public class mainGame extends JPanel
{


public static void main(String args[])
{
JFrame frame = new JFrame("hello");

mainGame mainPanel = new mainGame(600, 400);

SimpleSprite st = new SimpleSprite(mainPanel);

frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}


private ArrayList<SimpleSprite> sprites = new ArrayList<SimpleSprite>();

final static int timer_msec=30;

mainGame(int width, int height)
{
setPreferredSize(new Dimension(width, height));
sprites.add(new SimpleSprite(Color.blue, 0, 35, 2, 1));

startTimer();

}

public void startTimer()
{
java.util.Timer timer = new java.util.Timer();
TimerTask timerTask = new TimerTask()
{

@Override
public void run() {
// TODO Auto-generated method stub
updateSimulation();
}

};
timer.scheduleAtFixedRate(timerTask, 0, timer_msec);

}

public void updateSimulation()
{
for(SimpleSprite s: sprites)
{
s.update();
}
repaint();
}

public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
paintBackground(g2d);
for (SimpleSprite s : sprites) {
s.draw(g2d);
}
}

public void paintBackground(Graphics2D g2d)
{
g2d.setColor(Color.LIGHT_GRAY);
g2d.fillRect(0, 0, getWidth(), getHeight());
}

}

class SimpleSprite extends JPanel
{
// basic x,y movement at constant velocity
float x, y, dx, dy; // position and velocity (pixels/TIMER_MSEC)
Color color;
private static float width=600;;
SimpleSprite(mainGame g)
{
width=g.getWidth();
}

public SimpleSprite(Color color, float x, float y, float dx, float dy) {
// initial position and velocity
this.color = color;
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
}
public void update() { // update position and velocity every n milliSec

x += dx; // velocity in x direction
y += dy; // velocity in y direction

if(x<0)
{
dx=+dx;
}
if(x>width)
{
dx=-dx;
}





if(y<0)
{
dy=+dy;
}
if(y>width)
{
dy=-dy;
}


}
public void draw(Graphics2D g2d) {
g2d.setColor(color);
g2d.fillOval((int) x, (int) y, 100, 100); // draw this at latest position.
}
}

最佳答案

dx=+dxdy=+dy 不执行任何操作。

如果你想反转方向

dx=-dx;

无论 dx 在更改之前是正数还是负数,情况仍然如此。

关于java弹跳球碰撞不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26688468/

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