Processing 中制作游戏的问题 "Falling Blocks"
Problems on making the game "Falling Blocks" on Processing
所以我正在制作一个关于 Processing 的游戏,我似乎无法找到如何制作这个 if 语句条件。当我的圈子碰到一个块时,该条件应该扣除一条生命我有这个想法,但我不能把它写成代码,请帮助我。 “如果”条件在下方,完整的游戏代码紧随其后。
void difficulty()
{
if(/*what do i write here*/){
lives--;
}
else{
fill(0, 255, 0);
}
}
///////主要游戏代码从这里开始////////
int score;
int score1;
int miss;
int lives=10;
int ballx, bally;
class Rect
{
float x;
float y;
float speed;
float leng;
color c;
boolean valid;
final int MAX_COLOR = 255;
final int MIN_X = 50, MAX_X = 750;
final int MIN_Y = -800, MAX_Y = -100;
int MIN_SPEED = 1, MAX_SPEED = 2;
final int MIN_Leng = 50, MAX_Leng =100 ;
Rect()
{
initAll();
}
void initAll() {
valid = true;
c = color(random(255), random(255), random(255));
x = random(MIN_X, MAX_X);
y = random(MIN_Y, MAX_Y);
speed = random(MIN_SPEED, MAX_SPEED);
leng = random(MIN_Leng, MAX_Leng);
}
void update() {
if (!valid) {
initAll();
return;
}
move();
draw_rect();
}
void draw_rect()
{
fill(c);
rect (x, y, leng, leng);
}
void move()
{
if (y-leng <= height)
{
y += speed;
} else if (y-leng > height )
{
valid = false;
miss++;
}
}
void difficulty()
{
if(){
lives--;
}
else{
fill(0, 255, 0);
}
}
void GameOver()
{
if (lives==0)
{
for (int i = 0; i < Obj.length; i++)
{
Obj[i] = new Rect();
}
background(0);
textSize(50 );
fill(255);
text( "You Lose ", 15, 150);
text( "Score: " + score, 15, 100);
}
}
boolean isOver(int mx, int my) {
float disX = x - mx;
float disY = y - my;
if (sqrt(sq(disX) + sq(disY)) < leng/2 ) {
return true;
} else {
return false;
}
}
}
Rect [] Obj = new Rect [10];
void setup() {
size (400, 600);
ballx=200;
bally=300;
for (int i = 0; i < Obj.length; i++)
{
Obj[i] = new Rect();
}
}
void draw() {
background(0);
textSize(50);
text( "Score: " + score, 0, 100);
text("Lives: " + lives, 0, 50);
ellipse(ballx,bally,20,20);
for (int i = 0; i < Obj.length; i++) {
Obj[i].update();
Obj[i].difficulty();
Obj[i].GameOver();
}
surface.setTitle(nf(frameRate, 3, 2));
}
void keyPressed(){
for(Rect s : Obj){
if ( key =='q' ){
ballx=ballx-2;
score++;
score1++;
s.valid = false;
break;
}
if ( key =='e'){
ballx=ballx+2;
score++;
score1++;
s.valid = false;
break;
}
}
}
您要做的是检查玩家是否与任何矩形发生碰撞。
想一想,玩家碰撞到一个矩形意味着什么?
就是玩家的x-position要大于左边的x-position,但要小于右边的x-positon。
y-positions.
也是如此
由于您需要检查所有个矩形的位置,您应该使用for或foreach循环。
你现在可以按照我到现在为止所说的去做,它会起作用,但你也应该考虑到播放器本身的大小。如果他的 left/right 边与矩形碰撞,玩家仍然应该与矩形碰撞,而不仅仅是他的中心。
播放器在技术上是一个圆形,但为了简单起见,我们将其近似为一个矩形。
boolean isColliding = false;
for(Rect r : Obj)
{
if (ballx + 10 > r.x && //is the right side of the ball further right than the left side of the rectangle?
ballx - 10 < r.x + r.leng && //is the left side of the ball further left than the right side of the rectangle?
//if both of the above are true, the balls x-position is between the left and right side of the ball
bally + 10 > r.y && //is the bottom of the ball further down than the top of the rectangle?
bally - 10 < r.y + r.leng) { //is the top of the ball further up than the bottom of the rectangle?
isColliding = true; //if all of the above are true, the ball is colliding with the rectangle
}
}
if (isColliding) {
lives--;
} else {
fill(0, 255, 0);
}
所以我正在制作一个关于 Processing 的游戏,我似乎无法找到如何制作这个 if 语句条件。当我的圈子碰到一个块时,该条件应该扣除一条生命我有这个想法,但我不能把它写成代码,请帮助我。 “如果”条件在下方,完整的游戏代码紧随其后。
void difficulty()
{
if(/*what do i write here*/){
lives--;
}
else{
fill(0, 255, 0);
}
}
///////主要游戏代码从这里开始////////
int score;
int score1;
int miss;
int lives=10;
int ballx, bally;
class Rect
{
float x;
float y;
float speed;
float leng;
color c;
boolean valid;
final int MAX_COLOR = 255;
final int MIN_X = 50, MAX_X = 750;
final int MIN_Y = -800, MAX_Y = -100;
int MIN_SPEED = 1, MAX_SPEED = 2;
final int MIN_Leng = 50, MAX_Leng =100 ;
Rect()
{
initAll();
}
void initAll() {
valid = true;
c = color(random(255), random(255), random(255));
x = random(MIN_X, MAX_X);
y = random(MIN_Y, MAX_Y);
speed = random(MIN_SPEED, MAX_SPEED);
leng = random(MIN_Leng, MAX_Leng);
}
void update() {
if (!valid) {
initAll();
return;
}
move();
draw_rect();
}
void draw_rect()
{
fill(c);
rect (x, y, leng, leng);
}
void move()
{
if (y-leng <= height)
{
y += speed;
} else if (y-leng > height )
{
valid = false;
miss++;
}
}
void difficulty()
{
if(){
lives--;
}
else{
fill(0, 255, 0);
}
}
void GameOver()
{
if (lives==0)
{
for (int i = 0; i < Obj.length; i++)
{
Obj[i] = new Rect();
}
background(0);
textSize(50 );
fill(255);
text( "You Lose ", 15, 150);
text( "Score: " + score, 15, 100);
}
}
boolean isOver(int mx, int my) {
float disX = x - mx;
float disY = y - my;
if (sqrt(sq(disX) + sq(disY)) < leng/2 ) {
return true;
} else {
return false;
}
}
}
Rect [] Obj = new Rect [10];
void setup() {
size (400, 600);
ballx=200;
bally=300;
for (int i = 0; i < Obj.length; i++)
{
Obj[i] = new Rect();
}
}
void draw() {
background(0);
textSize(50);
text( "Score: " + score, 0, 100);
text("Lives: " + lives, 0, 50);
ellipse(ballx,bally,20,20);
for (int i = 0; i < Obj.length; i++) {
Obj[i].update();
Obj[i].difficulty();
Obj[i].GameOver();
}
surface.setTitle(nf(frameRate, 3, 2));
}
void keyPressed(){
for(Rect s : Obj){
if ( key =='q' ){
ballx=ballx-2;
score++;
score1++;
s.valid = false;
break;
}
if ( key =='e'){
ballx=ballx+2;
score++;
score1++;
s.valid = false;
break;
}
}
}
您要做的是检查玩家是否与任何矩形发生碰撞。
想一想,玩家碰撞到一个矩形意味着什么?
就是玩家的x-position要大于左边的x-position,但要小于右边的x-positon。 y-positions.
也是如此由于您需要检查所有个矩形的位置,您应该使用for或foreach循环。
你现在可以按照我到现在为止所说的去做,它会起作用,但你也应该考虑到播放器本身的大小。如果他的 left/right 边与矩形碰撞,玩家仍然应该与矩形碰撞,而不仅仅是他的中心。 播放器在技术上是一个圆形,但为了简单起见,我们将其近似为一个矩形。
boolean isColliding = false;
for(Rect r : Obj)
{
if (ballx + 10 > r.x && //is the right side of the ball further right than the left side of the rectangle?
ballx - 10 < r.x + r.leng && //is the left side of the ball further left than the right side of the rectangle?
//if both of the above are true, the balls x-position is between the left and right side of the ball
bally + 10 > r.y && //is the bottom of the ball further down than the top of the rectangle?
bally - 10 < r.y + r.leng) { //is the top of the ball further up than the bottom of the rectangle?
isColliding = true; //if all of the above are true, the ball is colliding with the rectangle
}
}
if (isColliding) {
lives--;
} else {
fill(0, 255, 0);
}