简体   繁体   中英

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. 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. 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


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


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



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. You reverse the x velocity. But in the next round the ball has not made it back into the field, now it is at -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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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