Java 需要关于突破游戏球速度的帮助吗

Java 需要关于突破游戏球速度的帮助吗,java,Java,这是一个12级面向对象编程项目 我有一个叫做Ball的类来构造我的Ball对象 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class Ball{ private double xPos; private double yPos; private int direction; public Ball(int ixPos,

这是一个12级面向对象编程项目

我有一个叫做Ball的类来构造我的Ball对象

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

public class Ball{
    private double xPos;
    private double yPos;
    private int direction;
    public Ball(int ixPos, int iyPos, int idirection){
        xPos = ixPos;
        yPos = iyPos;
        direction = idirection;
    }
    public int returnX(){
        return (int)xPos;
    }
    public int returnY(){
        return (int)yPos;
    }
    public int returnDirection(){
        return direction;
    }
    public void move(){
        xPos += 1*Math.cos(Math.toRadians(direction));
        yPos -= 1*Math.sin(Math.toRadians(direction));
    }
    public void collide(int collideWith){
        if(collideWith==1){//collide with left wall
            if(90<direction && direction<180){
                direction = 180-direction;
            }
            if(180<direction && direction<270){
                direction = 540-direction;
            }
        }
        if(collideWith==2){//collide with right wall
            if(0<direction && direction<90){
                direction = 180-direction;
            }
            if(270<direction && direction<360){
                direction = 540-direction;
            }
        }
        if(collideWith==3){//collide with up wall
            if(0<direction && direction<90){
                direction = 360-direction;
            }
            if(90<direction && direction<180){
                direction = 360-direction;
            }
        }
        if(collideWith==4){//collide with down wall
            direction = 360-direction;
        }
    }
    public void collidePaddle(int collidePos){
        if(collidePos!=50 && collidePos!=0){
            direction = (50-collidePos)*180/50;
        }
    }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入java.util.*;
公共班级舞会{
私人双XPO;
私人双YPO;
私人int方向;
公共舞会(国际ixPos、国际iyPos、国际idirection){
xPos=ixPos;
yPos=iyPos;
方向=方向;
}
public int returnX(){
返回(int)XPO;
}
公共int returnY(){
返回(int)YPO;
}
公共交通方向({
返回方向;
}
公开作废动议(){
xPos+=1*Math.cos(Math.toRadians(方向));
yPos-=1*数学正弦(数学环面(方向));
}
公共无效碰撞(int碰撞){
如果(碰撞==1){//与左墙碰撞

如果(90而不是在您的
Collide Pable
检查中使用绝对检查,您应该考虑范围

例如

public void collidePaddle(int collidePos){
    if (collidePos >= 50) {
        direction = (50-collidePos)*180/50;
        // Correct the position of the ball to meet the minimum requirements
        // of the collision...
    } else if (collidePos <= 0) {
        direction = (50-collidePos)*180/50;
        // Correct the position of the ball to meet the minimum requirements
        // of the collision...
    }
}
public-void-collidate挡板(int-collidatepos){
如果(位置>=50){
方向=(50个位置)*180/50;
//纠正球的位置,以满足最低要求
//碰撞的原因。。。
}else如果(collidePos=(bounds.x+bounds.width)){
速度*=-1;
p、 x=((bounds.x+bounds.width)-半径)+速度;
}如果(p.x=(bounds.x+bounds.width)){
速度*=-1;
p、 x=((bounds.x+bounds.width)-半径)+速度;
//我们已经越过左边了吗??

}否则如果(p.x我不知道我的问题提得是否足够好,但对我来说,我并没有问任何关于划桨的问题……你能告诉我你认为我问了什么吗……因为我请求帮助,当速度太高时,解决球撞到墙上和挡块的问题,而不是击球后的方向。是的,你似乎在检查一个绝对位置,拉特呃,然后考虑到游骑兵。随着速度的增加,你不能依赖于球的边缘会与任何东西的边缘完美地相交。相反,你需要看看球的任何部分是否击中了世界上任何可能的部分(包括重叠)。
public class SimpleBouncyBall {

    public static void main(String[] args) {
        new SimpleBouncyBall();
    }

    public SimpleBouncyBall() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new CourtPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class CourtPane extends JPanel {

        private Ball ball;
        private int speed = 5;

        public CourtPane() {
            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Rectangle bounds = new Rectangle(new Point(0, 0), getSize());
                    if (ball == null) {
                        ball = new Ball(bounds);
                    }
                    speed = ball.move(speed, bounds);
                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(100, 100);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); 
            if (ball != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                Point p = ball.getPoint();
                g2d.translate(p.x, p.y);
                ball.paint(g2d);
                g2d.dispose();
            }
        }

    }

    public class Ball {

        private Point p;
        private int radius = 12;

        public Ball(Rectangle bounds) {                
            p = new Point();
            p.x = 0;
            p.y = bounds.y + (bounds.height - radius) / 2;                
        }

        public Point getPoint() {
            return p;
        }

        public int move(int speed, Rectangle bounds) {                
            p.x += speed;
            if (p.x + radius >= (bounds.x + bounds.width)) {                    
                speed *= -1;
                p.x = ((bounds.x + bounds.width) - radius) + speed;                    
            } else if (p.x <= bounds.x) {
                speed *= -1;
                p.x = bounds.x + speed;                    
            }
            p.y = bounds.y + (bounds.height - radius) / 2;                
            return speed;                
        }

        public void paint(Graphics2D g) {
            g.setColor(Color.RED);
            g.fillOval(0, 0, radius, radius);
        }            
    }        
}
public int move(int speed, Rectangle bounds) {                
    // Apply the delta
    p.x += speed;
    // Have we passed beyond the right side??
    if (p.x + radius >= (bounds.x + bounds.width)) {                    
        speed *= -1;
        p.x = ((bounds.x + bounds.width) - radius) + speed;                    
    // Have we past beyond the left side??
    } else if (p.x <= bounds.x) {
        speed *= -1;
        p.x = bounds.x + speed;                    
    }
    p.y = bounds.y + (bounds.height - radius) / 2;                
    return speed;                
}