试图创建一个Pong游戏

时间:2015-02-22 16:55:58

标签: java pong

我正在尝试用Java创建一个乒乓球比赛,而我在球类物理方面遇到了一些麻烦。这一切都非常完美,直到球太慢。我做到这样,球的速度每帧每帧减少0.02像素,但我不希望它变得太慢,所以我有一个检查,一旦x和y速度低于2,它就会阻止它减速。问题是一旦发生这种情况,如果x或y速度明显小于另一个,则它不会保持当前轨迹。它似乎平滑,因此几乎是直线,除了每隔几秒向上或向下移动1个像素。这是我的球类:

import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Ball {

    private static int DIAMETER = 30;
    double x = 0;
    double y = 0;
    double xVel = 5;
    double yVel = 5;
    double prevX = 0;
    double prevY = 0;
    double minSpeed = 2;
    double maxSpeed = 10;
    int drawX = 0;
    int drawY = 0;
    boolean tooSlow = false;
    public game game;




    public Ball(game game) {
        this.game = game;
    }



    public void move()
    {
        prevX = xVel;
        prevY = yVel;

        if(xVel > maxSpeed)
            xVel = maxSpeed;
        if(yVel > maxSpeed)
            yVel = maxSpeed;


        if(Math.abs(xVel) < minSpeed  && Math.abs(yVel) < minSpeed)
            tooSlow = true;
        else
            tooSlow = false;



        if(x + xVel < 0){
            xVel *= -1;
        }
        if(x + xVel > 484 - DIAMETER){
            xVel *= -1;

        }
        if(y + yVel < 0){
            yVel *= -1;
        }
        if(y + yVel > 261 - DIAMETER){
            yVel *= -1;

        }

        if(collision()){
            if(game.paddle.paddleTop().intersects(getBounds())){
                y += game.paddle.y - game.paddle.prevY;
                yVel = -1 * Math.abs(yVel) + (game.paddle.y - game.paddle.prevY);
            }

            if(game.paddle.paddleBottom().intersects(getBounds())){
                y += game.paddle.y - game.paddle.prevY;
                yVel = Math.abs(yVel) + (game.paddle.y - game.paddle.prevY);
            }
            if(game.paddle.paddleRight().intersects(getBounds())){
                x += game.paddle.x - game.paddle.prevX;
                xVel = Math.abs(xVel) + (game.paddle.x - game.paddle.prevX);
                yVel += (game.paddle.y - game.paddle.prevY) / 2;
            }
            if(game.paddle.paddleLeft().intersects(getBounds())){
                x += game.paddle.x - game.paddle.prevX;
                xVel = -1 * Math.abs(xVel) + (game.paddle.x - game.paddle.prevX);
                yVel += (game.paddle.y - game.paddle.prevY) / 2;
            }

        } 


        x += xVel;
        y += yVel;

        if(!tooSlow)
            slow();

        drawX = (int)x;
        drawY = (int)y;

    }

    public void slow()
    {
        xVel = (Math.abs(xVel) - 0.02) * Math.signum(xVel);
        yVel = (Math.abs(yVel) - 0.02) * Math.signum(yVel);
    }


    public void paint(Graphics2D g) {
        g.fillOval(drawX, drawY, 30, 30);

    }

    public Rectangle getBounds()
    {
        return new Rectangle(drawX, drawY, DIAMETER, DIAMETER);

    }

    public boolean collision()
    {
        return game.paddle.getBounds().intersects(getBounds());
    }
}

这些是我用来运行游戏的其余部分(如果它有帮助):

import java.awt.Rectangle;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;

public class Paddle {

    int x = 50;
    int y = 10;
    int xVel = 0;
    int yVel = 0;
    int prevX = 50;
    int prevY = 10;
    int speed = 4;
    private static final int WIDTH = 10;
    private static final int HEIGHT = 80;
    private game game;



    public Paddle(game game)
    {
        this.game = game;
    }

    public void move()
    {
        prevX = x;
        prevY = y;
        if(y + yVel > 0 && y + yVel < 261 - HEIGHT){
            y += yVel;
        }
        if(x + xVel > 0 && x + xVel < 484 - WIDTH){
            x += xVel;
        }


    }

    public void keyPressed(KeyEvent e)
    {
        if(e.getKeyCode() == KeyEvent.VK_UP){
            yVel = -speed;
        }
        if(e.getKeyCode() == KeyEvent.VK_DOWN){
            yVel = speed;
        }
        if(e.getKeyCode() == KeyEvent.VK_LEFT){
            xVel = -speed;
        }
        if(e.getKeyCode() == KeyEvent.VK_RIGHT){
            xVel = speed;
        }
    }

    public void keyReleased(KeyEvent e)
    {
        yVel = 0;
        xVel = 0;
    }

    public void paint(Graphics2D g)
    {
        g.fillRect(x, y, WIDTH, HEIGHT);
    }

    public Rectangle getBounds()
    {
        return new Rectangle(x, y, WIDTH, HEIGHT);
    }

    public Rectangle paddleTop()
    {
        return new Rectangle(x + WIDTH / 2, y, 1, 1);
    }

    public Rectangle paddleLeft()
    {
        return new Rectangle(x, y, 1, HEIGHT);
    }

    public Rectangle paddleRight()
    {
        return new Rectangle(x + WIDTH, y, 1, HEIGHT);
    }

    public Rectangle paddleBottom()
    {
        return new Rectangle(x + WIDTH / 2, y + HEIGHT, 1, 1);
    }

    public Rectangle test()
    {
        return new Rectangle(x, y, WIDTH, HEIGHT);
    }
}

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;




import javax.swing.JPanel;
import javax.swing.Timer;



public class game extends JPanel implements ActionListener, KeyListener{
    private Timer t;
    Ball ball = new Ball(this);
    Paddle paddle = new Paddle(this);




    public game() 
    {
        t = new Timer(15,this);
        t.start();
        addKeyListener(this);
        setFocusable(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        ball.move();
        paddle.move();
        repaint();
    }

    public void keyPressed(KeyEvent e)
    {
        paddle.keyPressed(e);
    }

    public void keyReleased(KeyEvent e)
    {
        paddle.keyReleased(e);
    }

    public void keyTyped(KeyEvent e) {}

    public void paint(Graphics g)
    {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        ball.paint(g2d);
        paddle.paint(g2d);
    }




}

import javax.swing.JFrame;


public class main {
    public static void main(String[]args)
    {
      JFrame frame = new JFrame("MLG Pong");
      game display = new game();
      frame.add(display);
      frame.setSize(500,300);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setResizable(false);
      System.out.println(""+display.getHeight()+" "+display.getWidth());

    }
}

很抱歉我的编码很混乱,几个月前我才学习Java。

3 个答案:

答案 0 :(得分:0)

问题出在slow()方法:

public void slow()
{
    xVel = (Math.abs(xVel) - 0.02) * Math.signum(xVel);
    yVel = (Math.abs(yVel) - 0.02) * Math.signum(yVel);
}

实际上,您正在从速度矢量中减去另一个方向的矢量。一开始,方向的变化很小,但后来变得更大。您总是从速度矢量中减去(+ -0.02,+ -0.02)。

一个简单的解决方法是:

public void slow()
{
    xVel = xVel*0.99;
    yVel = yVel*0.99;
}

但这会比小速度更快地减慢速度。

如果你不想要这种行为,你必须计算一个恒定长度的小向量,该向量与速度向量的方向相同,并减去它。

例如:

public void slow()
{
    double len = Math.sqrt(xVel*xVel + yVel*yVel);
    double sx = xVel/len*0.02;
    double sy = yVel/len*0.02;
    xVel = xVel - sx;
    yVel = yVel - sy;
}

答案 1 :(得分:0)

减速计算出错。当速度非常慢时,abs(v)-0.02是无意义的。

顺便说一下,你计算的方式并不好。你应该不直接修改矢量:

采用速度向量:vv=(length,angle)。然后根据需要修改长度,或者通过减去一些常量或者在每个帧上使用一个因子。然后vx=length*cos(angle)vy=length*sin(angle)

答案 2 :(得分:0)

您不必分别检查x和y方向的速度。你必须计算球的速度(在飞行方向上),并以最小速度检查结果。

更改行

if(Math.abs(xVel) < minSpeed  && Math.abs(yVel) < minSpeed)

if(xVel*xVel + yVel*yVel < minSpeed * minSpeed)