1

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

我有一个名为 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;
        }
    }
}

正如您在“移动”功能中看到的那样,现在球的速度非常低。但我需要球跑得更快。如果我将 1 更改为 5 之类的东西,就会出现问题。在我的主要课程中,它检查球是否撞到墙壁或块以改变方向,如果球每次可以移动的像素量大于 1,则球将进入墙壁或块。

对我来说,似乎没有办法解决这个问题,不知道我会从哪里开始思考,有没有更好的方法来检查碰撞或其他什么?

谢谢你。

4

1 回答 1

2

collidePaddle您应该允许范围,而不是在检查中使用绝对检查。

例如...

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 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;                
}

玩转速度,看看你得到了什么;)

于 2013-02-14T02:01:20.850 回答