如何在乒乓球游戏中加入球拍?
How to include the paddle in the pong game?
我的代码有一些问题,我无法真正解决。所以我开始编写乒乓球游戏的程序,但我不知道如何包括球拍,所以球在击中球拍时弹开,但在没有击中时通过。我已经为此尝试了 if- & else 语句和约束函数,但不知何故不起作用。
我的代码:
float ballxposition = 0; float ballyposition = 0; float speedx = 0;
float speedy = 0;
void setup() { size(1024, 768); speedx = 10; speedy = 10; }
void draw() { background(255); fill(128); rectMode(CENTER);
rect(mouseX, 730, 250, 20);
fill(0, 0, 255); ellipse(ballxposition, ballyposition, 30, 30);
ballxposition = ballxposition + speedx; ballyposition = ballyposition
+ speedy;
if (ballxposition>width||ballxposition<0) {
speedx = speedx * -1; }
if (ballyposition>height||ballyposition<0) {
speedy = speedy * -1; } }
非常感谢您的帮助。谢谢
您可能会发现这个 YouTube 视频值得一看
它是由一个叫 Dan Shiffman 的人创建的,他的网站是 The Coding Train:http://thecodingtrain.com
编码挑战 #67:Pong!
https://www.youtube.com/watch?v=IIrC5Qcb2G4
我发现他的编码挑战非常有用
我的代码有一些问题,我无法真正解决。所以我开始编写乒乓球游戏的程序,但我不知道如何包括球拍,所以球在击中球拍时弹开,但在没有击中时通过。我已经为此尝试了 if- & else 语句和约束函数,但不知何故不起作用。
我的代码:
float ballxposition = 0; float ballyposition = 0; float speedx = 0;
float speedy = 0;
void setup() { size(1024, 768); speedx = 10; speedy = 10; }
void draw() { background(255); fill(128); rectMode(CENTER);
rect(mouseX, 730, 250, 20);
fill(0, 0, 255); ellipse(ballxposition, ballyposition, 30, 30);
ballxposition = ballxposition + speedx; ballyposition = ballyposition
+ speedy;
if (ballxposition>width||ballxposition<0) {
speedx = speedx * -1; }
if (ballyposition>height||ballyposition<0) {
speedy = speedy * -1; } }
非常感谢您的帮助。谢谢
您可能会发现这个 YouTube 视频值得一看 它是由一个叫 Dan Shiffman 的人创建的,他的网站是 The Coding Train:http://thecodingtrain.com
编码挑战 #67:Pong! https://www.youtube.com/watch?v=IIrC5Qcb2G4
我发现他的编码挑战非常有用