"Avoid Falling Blocks "处理Java的游戏



所以我是在处理JAVA的过程中制作这个游戏的,代码如下,所以要点是我有一个球,它避免了下落的矩形,所以现在,球可以侧向移动,矩形从上面落下,但当矩形碰到球时,我该如何减少我的生命。

我做了一个名为Lives的函数,如果圆圈被矩形击中,它应该会减少生命,但我无法想出相应的代码。有人能帮忙吗?

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 Lives()
{

}
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);
//fill(0);
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].Lives();
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;
}
}

}

我认为最容易想到的是-

情况1-如果你想球的生命值逐渐降低:

定义maximum_Health变量并将其指定为100。每当一个盖帽击中你的球,就要减少你想要定义的健康量。

情况2-如果每个方块击中都会失去球的生命

定义remaining_Lives变量,并给它一些您想要的生命数的值。每当一个盖帽击中你的球时,将剩余的生命值减少一。

我认为这应该给你一些提示。

最新更新