使用 while 循环和 if 语句处理对象不会在 x 轴上移动
Processing objects won't move on x-axis using while loop & if statement
我正在做一个 space ship Processing 项目,当按下按键时,子弹会在 x 轴上移动,并从停止的地方继续向右移动,直到达到屏幕的宽度.一旦它到达屏幕的宽度,子弹就会再次回到 space 飞船前面的原始位置。换句话说,给人一种老街机 space 射击游戏的感觉。
我是如何尝试做到这一点的,是在 KeyPressed 循环中使用 while 和 if 语句。当 position = width 时,再次创建子弹(回到船前);然而子弹永远不会移动。
我对处理真的很陌生,所以我不明白他们的 while 和 if 语句是如何工作的。
//bullet
int bulletP1X = mouseX+248;
int bulletP1Y =mouseY+100;
int bulletP2X = mouseX+207;
int bulletP2Y = mouseY+100;
int bulletRextX = mouseX+215;
int bulletRextY = mouseY+95;
int bulletT1X = mouseX+240;
int bulletT1Y = mouseY+100;
int bulletT2X = mouseX+215;
int bulletT2Y = mouseY+100;
void setup(){
size(1300,810);
}
void draw(){
background(0,0,0);
fill(121,17,17);
strokeWeight(1);
stroke(0,0,0);
triangle(mouseX+20,mouseY+50,mouseX+20,mouseY+150,mouseX+200,mouseY+100);
fill(255,255,210);
triangle(mouseX+150,mouseY+87,mouseX+150,mouseY+113,mouseX+200,mouseY+100);
//Left wing of Space Ship
beginShape();
stroke(255,255,255);
fill(121,17,17);
vertex(mouseX+20,mouseY+50);
vertex(mouseX+40,mouseY+40);
vertex(mouseX+200,mouseY+95);
vertex(mouseX+200,mouseY+105);
endShape();
//Right wing of Space Ship
beginShape();
stroke(255,255,255);
fill(121,17,17);
vertex(mouseX+20,mouseY+150);
vertex(mouseX+40,mouseY+160);
vertex(mouseX+200,mouseY+105);
vertex(mouseX+200,mouseY+100);
endShape();
//Inside white banner of Space Ship (top)
beginShape();
stroke(255,255,255);
fill(255,255,255);
vertex(mouseX+20,mouseY+65);
vertex(mouseX+55,mouseY+85);
vertex(mouseX+150,mouseY+90);
vertex(mouseX+150,mouseY+100);
vertex(mouseX+20,mouseY+100);
endShape();
//Inside white banner of Space Ship (bottom)
beginShape();
vertex(mouseX+20,mouseY+135);
vertex(mouseX+55,mouseY+115);
vertex(mouseX+150,mouseY+112);
vertex(mouseX+150,mouseY+100);
vertex(mouseX+20,mouseY+100);
endShape();
//White back of ship behind the cockpit
beginShape();
fill(255,255,255);
vertex(mouseX+5,mouseY+80);
vertex(mouseX+20,mouseY+65);
vertex(mouseX+20,mouseY+135);
vertex(mouseX+5,mouseY+115);
endShape();
fill(0,0,0);
ellipse(mouseX+30,mouseY+100,30,20);
ellipse(mouseX+60,mouseY+70,10,10);
ellipse(mouseX+60,mouseY+130,10,10);
stroke(0,0,0);
//square shapes inside ship
fill(255,255,255);
stroke(0,0,0);
rect(mouseX+100,mouseY+95,50,10);
rect(mouseX+105,mouseY+98,40,4);
//line design inside white part of Space Ship
line(mouseX+20,mouseY+100,mouseX+60,mouseY+70);
line(mouseX+30,mouseY+110,mouseX+60,mouseY+130);
//bullet
bulletP1X = mouseX+248;
bulletP1Y =mouseY+100;
bulletP2X = mouseX+207;
bulletP2Y = mouseY+100;
bulletRextX = mouseX+215;
bulletRextY = mouseY+95;
bulletT1X = mouseX+240;
bulletT1Y = mouseY+100;
bulletT2X = mouseX+215;
bulletT2Y = mouseY+100;
fill(255,0,0);
strokeWeight(5);
stroke(255,0,0);
//side tips
point(bulletP1X,bulletP1Y);
point(bulletP2X,bulletP2Y);
//bullet body
strokeWeight(1);
fill(255,255,255);
rect(bulletRextX,bulletRextY,24,10);
//TWO_PI IS FULL CIRCLE
//PI + HALF_PI == 1.5 PI == -0.5 PI
//TAU + HALF_PI == TWO_PI + HALF_PI == 2.5 PI == 0.5 PI
//round ends of bullet
arc(bulletT1X,bulletT1Y,12,12,-HALF_PI, HALF_PI);
arc(bulletT2X,bulletT2Y,13,12,radians(90),radians(270));
}
这是我的代码的一部分,应该在 x 轴上移动项目符号,但它没有。
void keyPressed(){
background(0,0,0);
fill(255,0,0);
while (bulletP1X != width || bulletP1X < width || bulletP2X != width || bulletP2X < width
|| bulletRextX != width || bulletRextX < width || bulletT1X != width || bulletT1X < width || bulletT2X != width || bulletT2X < width){
fill(255,255,255);
stroke(0,0,0);
bulletP1X +=25;
bulletP2X+=25;
bulletRextX +=25;
bulletT1X+=25;
bulletT2X+=25;
fill(255,0,0);
strokeWeight(5);
stroke(255,0,0);
//side tips
point(bulletP1X,bulletP1Y);
point(bulletP2X,bulletP2Y);
//bullet body
strokeWeight(1);
fill(255,255,255);
rect(bulletRextX,bulletRextY,24,10);
//round ends of bullet
arc(bulletT1X,bulletT1Y,12,12,-HALF_PI, HALF_PI);
arc(bulletT2X,bulletT2Y,13,12,radians(90),radians(270));
}
if (bulletP1X == width || bulletP1X > width || bulletP2X == width || bulletP2X > width
|| bulletRextX == width || bulletRextX > width || bulletT1X == width || bulletT1X > width || bulletT2X == width || bulletT2X > width){
fill(255,255,255);
stroke(0,0,0);
//bullet
bulletP1X = mouseX+248;
bulletP1Y =mouseY+100;
bulletP2X = mouseX+207;
bulletP2Y = mouseY+100;
bulletRextX = mouseX+215;
bulletRextY = mouseY+95;
bulletT1X = mouseX+240;
bulletT1Y = mouseY+100;
bulletT2X = mouseX+215;
bulletT2Y = mouseY+100;
fill(255,0,0);
strokeWeight(5);
stroke(255,0,0);
//side tips
point(bulletP1X,bulletP1Y);
point(bulletP2X,bulletP2Y);
//bullet body
strokeWeight(1);
fill(255,255,255);
rect(bulletRextX,bulletRextY,24,10);
//round ends of bullet
arc(bulletT1X,bulletT1Y,12,12,-HALF_PI, HALF_PI);
arc(bulletT2X,bulletT2Y,13,12,radians(90),radians(270));
}
}
我会推荐你一些读物:
http://forum.processing.org/two/discussion/8087/what-are-setup-and-draw
http://forum.processing.org/two/discussion/8082/from-several-variables-to-arrays
http://forum.processing.org/two/discussion/8081/from-several-arrays-to-classes
函数keyPressed()
因为它是兄弟mousePressed()
、mouseClicked()
等...不是"loops"如你所说,它们是回调函数,意思是它们是每次事件发生时调用一次,因此当按下 key
时,keyPressed()
中的代码将执行一次。
draw()
是一个无限循环,每秒尝试 运行 自身 60 次。它里面的命令是按顺序执行的(top -> bottom),每次循环结束时最终渲染结果。
这就是为什么在回调函数中绘制通常不是一个好主意。用它来设置状态或标志来驱动 draw()
中的东西。还有一个 while 循环将暂停执行,防止绘制到达循环的末尾并渲染图形...
还有一个内置变量 keyPressed
,您可以在 draw()
中使用它来检查是否按下了某个键。
此外,每当你有很多 someVar1
、someVar2
...你应该考虑使用数组,如果你有一些数组保存相关的东西,比如 xPos[]
, yPos[]
、xSpeed[]
等等 classes 变得非常方便。看似很难,其实学了之后,事情就简单多了。
此外,为了将事物保持在所需范围内 (xPos),模 %
运算符非常有帮助。
这里有一个简单的方法让"bullet"在按下任意键时移动,到达边缘后重置:
int initial_x = 50;
int x_pos = initial_x;
void setup() {
size(300, 300);
background(0);
}
void draw() {
background(0);
if(keyPressed){
x_pos+=3;
if(x_pos >= width){
x_pos = initial_x;
}
}
ellipse(x_pos, 100, 10, 10);
}
旁注,您可以考虑将所有代码包装在一个函数中(或者更好的 class),这样您就可以保持 draw()
的潮流。类似于:
void spaceShip(int x, int y){
//code to draw ship at (x,y)
}
在平局中你会得到
spaceShip(mouseX, mouseY);
:)
我正在做一个 space ship Processing 项目,当按下按键时,子弹会在 x 轴上移动,并从停止的地方继续向右移动,直到达到屏幕的宽度.一旦它到达屏幕的宽度,子弹就会再次回到 space 飞船前面的原始位置。换句话说,给人一种老街机 space 射击游戏的感觉。
我是如何尝试做到这一点的,是在 KeyPressed 循环中使用 while 和 if 语句。当 position = width 时,再次创建子弹(回到船前);然而子弹永远不会移动。
我对处理真的很陌生,所以我不明白他们的 while 和 if 语句是如何工作的。
//bullet
int bulletP1X = mouseX+248;
int bulletP1Y =mouseY+100;
int bulletP2X = mouseX+207;
int bulletP2Y = mouseY+100;
int bulletRextX = mouseX+215;
int bulletRextY = mouseY+95;
int bulletT1X = mouseX+240;
int bulletT1Y = mouseY+100;
int bulletT2X = mouseX+215;
int bulletT2Y = mouseY+100;
void setup(){
size(1300,810);
}
void draw(){
background(0,0,0);
fill(121,17,17);
strokeWeight(1);
stroke(0,0,0);
triangle(mouseX+20,mouseY+50,mouseX+20,mouseY+150,mouseX+200,mouseY+100);
fill(255,255,210);
triangle(mouseX+150,mouseY+87,mouseX+150,mouseY+113,mouseX+200,mouseY+100);
//Left wing of Space Ship
beginShape();
stroke(255,255,255);
fill(121,17,17);
vertex(mouseX+20,mouseY+50);
vertex(mouseX+40,mouseY+40);
vertex(mouseX+200,mouseY+95);
vertex(mouseX+200,mouseY+105);
endShape();
//Right wing of Space Ship
beginShape();
stroke(255,255,255);
fill(121,17,17);
vertex(mouseX+20,mouseY+150);
vertex(mouseX+40,mouseY+160);
vertex(mouseX+200,mouseY+105);
vertex(mouseX+200,mouseY+100);
endShape();
//Inside white banner of Space Ship (top)
beginShape();
stroke(255,255,255);
fill(255,255,255);
vertex(mouseX+20,mouseY+65);
vertex(mouseX+55,mouseY+85);
vertex(mouseX+150,mouseY+90);
vertex(mouseX+150,mouseY+100);
vertex(mouseX+20,mouseY+100);
endShape();
//Inside white banner of Space Ship (bottom)
beginShape();
vertex(mouseX+20,mouseY+135);
vertex(mouseX+55,mouseY+115);
vertex(mouseX+150,mouseY+112);
vertex(mouseX+150,mouseY+100);
vertex(mouseX+20,mouseY+100);
endShape();
//White back of ship behind the cockpit
beginShape();
fill(255,255,255);
vertex(mouseX+5,mouseY+80);
vertex(mouseX+20,mouseY+65);
vertex(mouseX+20,mouseY+135);
vertex(mouseX+5,mouseY+115);
endShape();
fill(0,0,0);
ellipse(mouseX+30,mouseY+100,30,20);
ellipse(mouseX+60,mouseY+70,10,10);
ellipse(mouseX+60,mouseY+130,10,10);
stroke(0,0,0);
//square shapes inside ship
fill(255,255,255);
stroke(0,0,0);
rect(mouseX+100,mouseY+95,50,10);
rect(mouseX+105,mouseY+98,40,4);
//line design inside white part of Space Ship
line(mouseX+20,mouseY+100,mouseX+60,mouseY+70);
line(mouseX+30,mouseY+110,mouseX+60,mouseY+130);
//bullet
bulletP1X = mouseX+248;
bulletP1Y =mouseY+100;
bulletP2X = mouseX+207;
bulletP2Y = mouseY+100;
bulletRextX = mouseX+215;
bulletRextY = mouseY+95;
bulletT1X = mouseX+240;
bulletT1Y = mouseY+100;
bulletT2X = mouseX+215;
bulletT2Y = mouseY+100;
fill(255,0,0);
strokeWeight(5);
stroke(255,0,0);
//side tips
point(bulletP1X,bulletP1Y);
point(bulletP2X,bulletP2Y);
//bullet body
strokeWeight(1);
fill(255,255,255);
rect(bulletRextX,bulletRextY,24,10);
//TWO_PI IS FULL CIRCLE
//PI + HALF_PI == 1.5 PI == -0.5 PI
//TAU + HALF_PI == TWO_PI + HALF_PI == 2.5 PI == 0.5 PI
//round ends of bullet
arc(bulletT1X,bulletT1Y,12,12,-HALF_PI, HALF_PI);
arc(bulletT2X,bulletT2Y,13,12,radians(90),radians(270));
}
这是我的代码的一部分,应该在 x 轴上移动项目符号,但它没有。
void keyPressed(){
background(0,0,0);
fill(255,0,0);
while (bulletP1X != width || bulletP1X < width || bulletP2X != width || bulletP2X < width
|| bulletRextX != width || bulletRextX < width || bulletT1X != width || bulletT1X < width || bulletT2X != width || bulletT2X < width){
fill(255,255,255);
stroke(0,0,0);
bulletP1X +=25;
bulletP2X+=25;
bulletRextX +=25;
bulletT1X+=25;
bulletT2X+=25;
fill(255,0,0);
strokeWeight(5);
stroke(255,0,0);
//side tips
point(bulletP1X,bulletP1Y);
point(bulletP2X,bulletP2Y);
//bullet body
strokeWeight(1);
fill(255,255,255);
rect(bulletRextX,bulletRextY,24,10);
//round ends of bullet
arc(bulletT1X,bulletT1Y,12,12,-HALF_PI, HALF_PI);
arc(bulletT2X,bulletT2Y,13,12,radians(90),radians(270));
}
if (bulletP1X == width || bulletP1X > width || bulletP2X == width || bulletP2X > width
|| bulletRextX == width || bulletRextX > width || bulletT1X == width || bulletT1X > width || bulletT2X == width || bulletT2X > width){
fill(255,255,255);
stroke(0,0,0);
//bullet
bulletP1X = mouseX+248;
bulletP1Y =mouseY+100;
bulletP2X = mouseX+207;
bulletP2Y = mouseY+100;
bulletRextX = mouseX+215;
bulletRextY = mouseY+95;
bulletT1X = mouseX+240;
bulletT1Y = mouseY+100;
bulletT2X = mouseX+215;
bulletT2Y = mouseY+100;
fill(255,0,0);
strokeWeight(5);
stroke(255,0,0);
//side tips
point(bulletP1X,bulletP1Y);
point(bulletP2X,bulletP2Y);
//bullet body
strokeWeight(1);
fill(255,255,255);
rect(bulletRextX,bulletRextY,24,10);
//round ends of bullet
arc(bulletT1X,bulletT1Y,12,12,-HALF_PI, HALF_PI);
arc(bulletT2X,bulletT2Y,13,12,radians(90),radians(270));
}
}
我会推荐你一些读物:
http://forum.processing.org/two/discussion/8087/what-are-setup-and-draw
http://forum.processing.org/two/discussion/8082/from-several-variables-to-arrays
http://forum.processing.org/two/discussion/8081/from-several-arrays-to-classes
函数keyPressed()
因为它是兄弟mousePressed()
、mouseClicked()
等...不是"loops"如你所说,它们是回调函数,意思是它们是每次事件发生时调用一次,因此当按下 key
时,keyPressed()
中的代码将执行一次。
draw()
是一个无限循环,每秒尝试 运行 自身 60 次。它里面的命令是按顺序执行的(top -> bottom),每次循环结束时最终渲染结果。
这就是为什么在回调函数中绘制通常不是一个好主意。用它来设置状态或标志来驱动 draw()
中的东西。还有一个 while 循环将暂停执行,防止绘制到达循环的末尾并渲染图形...
还有一个内置变量 keyPressed
,您可以在 draw()
中使用它来检查是否按下了某个键。
此外,每当你有很多 someVar1
、someVar2
...你应该考虑使用数组,如果你有一些数组保存相关的东西,比如 xPos[]
, yPos[]
、xSpeed[]
等等 classes 变得非常方便。看似很难,其实学了之后,事情就简单多了。
此外,为了将事物保持在所需范围内 (xPos),模 %
运算符非常有帮助。
这里有一个简单的方法让"bullet"在按下任意键时移动,到达边缘后重置:
int initial_x = 50;
int x_pos = initial_x;
void setup() {
size(300, 300);
background(0);
}
void draw() {
background(0);
if(keyPressed){
x_pos+=3;
if(x_pos >= width){
x_pos = initial_x;
}
}
ellipse(x_pos, 100, 10, 10);
}
旁注,您可以考虑将所有代码包装在一个函数中(或者更好的 class),这样您就可以保持 draw()
的潮流。类似于:
void spaceShip(int x, int y){
//code to draw ship at (x,y)
}
在平局中你会得到
spaceShip(mouseX, mouseY);
:)