处理中的简单乒乓球游戏
Simple Pong game in Processing
我正在尝试在 Java 中创建一个简单的乒乓球游戏进行处理。我还没有完成,一切都很顺利,除了我不能让球从乒乓球拍上弹开。我已经成功地做到了,如果球低于球拍,它会反弹回来,但出于某种原因,如果球在球拍上方,它就会通过。
这是我当前的代码,我当前的问题是在 void animatePaddles
函数的 paddleFunctions 选项卡中:
主选项卡:
Ball ball;
Paddle primary, secondary;
void setup()
{
size(1000, 500);
background(0);
smooth();
frameRate(150);
ball = new Ball();
initializePaddles(); // enters x values into primary and secondary paddles
}
void draw()
{
ball.animateBall(); //uses vectors to add movements to ball, also checks if hitting walls
animatePaddles(); //adds movements to paddles
}
球class:
class Ball
{
PVector location, direction;
final int diameter = height/20;
Ball()
{
location = new PVector(width/2, height/2);
direction = new PVector(0.5, 0.5);
}
void animateBall() //movement to balls and checking if hitting walls
{
background(0);
fill(255);
circle(location.x, location.y, diameter);
location.add(direction);
if (location.y+diameter/2>= height|| location.y<=diameter/2)
direction.y*=-1;
}
}
桨 class:
class Paddle //class for paddles
{
PVector location;
PVector direction = new PVector(0, 0);
final int paddleLength = height/5;
final int paddleWidth = width/150;
}
paddleFunctions 选项卡:
void initializePaddles() //enters x values into paddles relative screen size
{
primary = new Paddle();
primary.location= new PVector(width*.987, height/2);
secondary = new Paddle();
secondary.location = new PVector(width*.007, height/2);
}
void animatePaddles() //creates the paddles and adds movement
{
fill(255);
rect(primary.location.x, primary.location.y, primary.paddleWidth, primary.paddleLength);
rect(secondary.location.x, secondary.location.y, secondary.paddleWidth, secondary.paddleLength);
primary.location.add(primary.direction);
secondary.location.add(secondary.direction);
if (ball.location.x+ball.diameter/2==primary.location.x-primary.paddleWidth/2 && ball.location.y>=primary.location.y-primary.paddleLength/2 && ball.location.y<=primary.location.y+primary.paddleLength/2) //THE PROBLEM
ball.direction.x*=-1; //^^ **PROBLEM**
}
void keyPressed() //controls primary paddle
{
if (key == CODED)
if (keyCode == UP)
primary.direction.y=-2;
else if (keyCode == DOWN)
primary.direction.y=2;
}
void keyReleased()
{
primary.direction.y=0;
}
你的建议是错误的。传递给 rect()
is not the center of the rectangle, by default it is the top left corner. But you can change the mode by rectMode()
的坐标:
rectMode(CENTER);
rect(primary.location.x, primary.location.y, primary.paddleWidth, primary.paddleLength);
rect(secondary.location.x, secondary.location.y, secondary.paddleWidth, secondary.paddleLength);
由于您使用浮点数进行操作,因此球永远不会准确击中球拍。您必须评估球的右侧是否大于球拍的左侧:
if (ball.location.x+ball.diameter/2 >= primary.location.x-+primary.paddleWidth/2 &&
ball.location.y >= primary.location.y-primary.paddleLength/2 &&
ball.location.y <= primary.location.y+primary.paddleLength/2) {
ball.direction.x *= -1;
}
我正在尝试在 Java 中创建一个简单的乒乓球游戏进行处理。我还没有完成,一切都很顺利,除了我不能让球从乒乓球拍上弹开。我已经成功地做到了,如果球低于球拍,它会反弹回来,但出于某种原因,如果球在球拍上方,它就会通过。
这是我当前的代码,我当前的问题是在 void animatePaddles
函数的 paddleFunctions 选项卡中:
主选项卡:
Ball ball;
Paddle primary, secondary;
void setup()
{
size(1000, 500);
background(0);
smooth();
frameRate(150);
ball = new Ball();
initializePaddles(); // enters x values into primary and secondary paddles
}
void draw()
{
ball.animateBall(); //uses vectors to add movements to ball, also checks if hitting walls
animatePaddles(); //adds movements to paddles
}
球class:
class Ball
{
PVector location, direction;
final int diameter = height/20;
Ball()
{
location = new PVector(width/2, height/2);
direction = new PVector(0.5, 0.5);
}
void animateBall() //movement to balls and checking if hitting walls
{
background(0);
fill(255);
circle(location.x, location.y, diameter);
location.add(direction);
if (location.y+diameter/2>= height|| location.y<=diameter/2)
direction.y*=-1;
}
}
桨 class:
class Paddle //class for paddles
{
PVector location;
PVector direction = new PVector(0, 0);
final int paddleLength = height/5;
final int paddleWidth = width/150;
}
paddleFunctions 选项卡:
void initializePaddles() //enters x values into paddles relative screen size
{
primary = new Paddle();
primary.location= new PVector(width*.987, height/2);
secondary = new Paddle();
secondary.location = new PVector(width*.007, height/2);
}
void animatePaddles() //creates the paddles and adds movement
{
fill(255);
rect(primary.location.x, primary.location.y, primary.paddleWidth, primary.paddleLength);
rect(secondary.location.x, secondary.location.y, secondary.paddleWidth, secondary.paddleLength);
primary.location.add(primary.direction);
secondary.location.add(secondary.direction);
if (ball.location.x+ball.diameter/2==primary.location.x-primary.paddleWidth/2 && ball.location.y>=primary.location.y-primary.paddleLength/2 && ball.location.y<=primary.location.y+primary.paddleLength/2) //THE PROBLEM
ball.direction.x*=-1; //^^ **PROBLEM**
}
void keyPressed() //controls primary paddle
{
if (key == CODED)
if (keyCode == UP)
primary.direction.y=-2;
else if (keyCode == DOWN)
primary.direction.y=2;
}
void keyReleased()
{
primary.direction.y=0;
}
你的建议是错误的。传递给 rect()
is not the center of the rectangle, by default it is the top left corner. But you can change the mode by rectMode()
的坐标:
rectMode(CENTER);
rect(primary.location.x, primary.location.y, primary.paddleWidth, primary.paddleLength);
rect(secondary.location.x, secondary.location.y, secondary.paddleWidth, secondary.paddleLength);
由于您使用浮点数进行操作,因此球永远不会准确击中球拍。您必须评估球的右侧是否大于球拍的左侧:
if (ball.location.x+ball.diameter/2 >= primary.location.x-+primary.paddleWidth/2 &&
ball.location.y >= primary.location.y-primary.paddleLength/2 &&
ball.location.y <= primary.location.y+primary.paddleLength/2) {
ball.direction.x *= -1;
}