简体   繁体   English

当我在 java 中提高速度时,我的球不会从屏幕边缘反弹

[英]My ball doesn't bounce off the edge of screen when I up it's speed in java

I made a simple project in java where the ball bounces off the edges of the screen.我在 java 中做了一个简单的项目,球从屏幕边缘反弹。 When I changed the speed of the ball, it went through the edge of the screen.当我改变球的速度时,它穿过了屏幕的边缘。 The speed of the ball are in the variables xvelocity and yVelocity.球的速度在变量 xvelocity 和 yVelocity 中。 How can I avoid this?我怎样才能避免这种情况? Also the ball doesn't vanish, when I make my screen bigger I can see it bounce around.球也不会消失,当我把屏幕放大时,我可以看到它反弹。

Class: MyPanel Class:我的面板


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyPanel extends JPanel implements ActionListener{
    
    final int panelWidth = 500;
    final int panelHeight = 500;
    Image backgroundImage;
    Image enemy;
    Timer timer;
    int x = 0;
    int y = 0;
    int xVelocity = 3; 
    int yVelocity = 2;
    
    MyPanel() {
        this.setPreferredSize(new Dimension(500,500));
        enemy = new ImageIcon("enemy.png").getImage();
        backgroundImage = new ImageIcon("space.png").getImage();
        Timer timer = new Timer(5, this);
        timer.start();
    }

    public void paint(Graphics g) {
        
        super.paint(g);
        
        Graphics2D g2D = (Graphics2D) g;
        
        g2D.drawImage(backgroundImage, 0, 0, null);
        g2D.drawImage(enemy, x, y, null);
        
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        
        if (x == panelWidth - enemy.getWidth(null) || x < 0 ) {
            xVelocity *= -1;
        }
        if (y == panelHeight - enemy.getHeight(null) || y < 0 ) {
            yVelocity *= -1;
        }
        
        y += yVelocity;
        x += xVelocity;
        repaint();
        
    }
    
}

Class: MyFrame Class:MyFrame


import javax.swing.JFrame;

public class MyFrame extends JFrame {
    
    MyPanel panel = new MyPanel();
    
    MyFrame() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(panel);
        this.pack();
        this.setVisible(true);
    }
    
}

Class: Perserve Class:坚持



public class Perserve {
    
    public static void main(String args[]) {
        
        MyFrame frame = new MyFrame();
        
    }
    
}

Been there, done that.去过也做过。

Assume your ball speed is big enough so after one move it's x position is -2.假设你的球速足够大,所以在一次移动之后它的 x position 是 -2。 You reverse the x velocity.你反转 x 速度。 But in the next round the ball has not made it back into the field, now it is at -1.但是在下一轮球没有回到场上,现在是-1。 Again you reverse the speed, and the ball is caught in nowhere on the left of the screen.再次反转速度,球被挡在屏幕左侧的任何地方。

Of course the very same can happen in all other three edges of the screen.当然,同样的情况也可能发生在屏幕的所有其他三个边缘。 What to do?该怎么办?

When the ball is off-screen, reverse the speed and initialize the position as well to make sure the ball is back on the screen, like so:当球离开屏幕时,反转速度并初始化 position 以确保球回到屏幕上,如下所示:

if (x > panelWidth - enemy.getWidth(null)) {
        xVelocity *= -1;
        x = panelWidth - enemy.getWidth(null);
} else if (x < 0 ) {
        xVelocity *= -1;
        x = 0;
}
if (y > panelHeight - enemy.getHeight(null)) {
    yVelocity *= -1;
    y = panelHeight - enemy.getHeight(null);
} else if (y < 0 ) {
    yVelocity *= -1;
    y = 0;
}

BTW, do not compare whether the ball is on the edge (==) but check if it has passed the edge.顺便说一句,不要比较球是否在边缘(==),而是检查它是否已经通过边缘。

A more advanced code would not just move the ball and test afterwards.更高级的代码不会只是移动球并在之后进行测试。 It would calculate the future position, perform all checks and in case of a collision go back to find the real collision position, then perform only that move plus initiate the collision.它将计算未来的 position,执行所有检查,如果发生碰撞 go 会返回以找到真正的碰撞 position,然后仅执行该移动并启动碰撞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM