Processing 试着让我的生活和游戏的一部分发挥作用
我正在创建一个碰撞检测游戏,其中: 每次我碰壁,我的生命都会减少。 一旦我有了0条生命,游戏就结束了。 但游戏让我进入了消极的生活。此外,我的点击开始,一旦你赢了似乎也不工作。。。有人知道怎么解决这个问题吗Processing 试着让我的生活和游戏的一部分发挥作用,processing,Processing,我正在创建一个碰撞检测游戏,其中: 每次我碰壁,我的生命都会减少。 一旦我有了0条生命,游戏就结束了。 但游戏让我进入了消极的生活。此外,我的点击开始,一旦你赢了似乎也不工作。。。有人知道怎么解决这个问题吗 PImage startScreen; int gamestate=1; int lives = 3; class Sprite { float x; float y; float dx; float dy; } Sprite rect=new Sprite(); Spr
PImage startScreen;
int gamestate=1;
int lives = 3;
class Sprite {
float x;
float y;
float dx;
float dy;
}
Sprite rect=new Sprite();
Sprite ball=new Sprite();
void setup(){
size(500,500);
rect.x = 500;
rect.y = 12;
ball.y= mouseY;
background(0);
fill(0,255,0);
text("Click to Begin", 10, 250);
}
void draw(){
if(gamestate ==0){
background(0);
fill(0, 255,0);
noStroke();
rect(0,235, 500,2.5);
rect(0,250, 500,2.5);
fill(0);
rect(0,238,rect.x,rect.y);
fill(0,255,0);
ellipse(mouseX, mouseY, 2,2);
text("lives left:"+lives, 10, 20);
if (mouseY<240 || mouseY>247){
background(0);
lives = lives-1;
if(lives <= 0){
text("Game Over. \nClick to Begin", 225,250);
gamestate=1;
}
}
if (mouseX >= 495){
background(0);
text("You Win! \nClick to Begin Again.", 225,250);
}
}
}
void mousePressed(){
if (gamestate ==1){
gamestate=0;
}
}
请记住,在鼠标第一次进入草图之前,mouseX和mouseY都是0。因此,在draw函数中,当您检查mouseY是否<240时,这是正确的。你每秒这样做60次,所以你马上失去了你所有的3条生命 要解决这个问题,您可能需要一个开始矩形,玩家必须单击该矩形才能开始游戏,这样您就知道鼠标在窗口中 然后,在下一轮开始之前,你必须给玩家一个回到起点的机会,否则你只能每秒失去60次生命 基本情况可能如下所示:
int gamestate=1; //1 is start screen, 0 is playing, 2 is between lives, 3 wins
int lives = 3;
class Sprite {
float x;
float y;
float dx;
float dy;
}
Sprite rect=new Sprite();
Sprite ball=new Sprite();
void setup() {
size(500, 500);
rect.x = 500;
rect.y = 12;
ball.y= mouseY;
}
void draw() {
background(0);
if (gamestate ==0) {
fill(0, 255, 0);
noStroke();
rect(0, 235, 500, 2.5);
rect(0, 250, 500, 2.5);
fill(0);
rect(0, 238, rect.x, rect.y);
fill(0, 255, 0);
ellipse(mouseX, mouseY, 2, 2);
text("lives left:"+lives, 10, 20);
if (mouseY<240 || mouseY>247) {
lives = lives-1;
gamestate=2;
if (lives <= 0) {
text("Game Over. \nClick to Begin", 225, 250);
gamestate=1;
}
}
if (mouseX >= 495) {
gamestate=3;
}
}
else if(gamestate == 1 || gamestate==2){
fill(0, 255, 0);
text("Click to Begin", 10, 230);
rect(0, 240, width, 7);
}
else if(gamestate == 3){
text("You Win! \nClick to Begin Again.", 225, 250);
}
}
void mousePressed() {
if (gamestate ==1 || gamestate == 2) {
if(mouseY>240 && mouseY<247){
gamestate=0;
}
}
}
注意,我真的不明白你的游戏应该做什么:你的安全区域只有7像素高,看起来很小。但假设这只是一个示例,我的答案应该概括到您的实际代码:
将游戏分成不同的模式。您开始使用gamestate变量执行此操作,但同时也混合了事件代码和绘图代码。取而代之的是,让每种模式都成为一种状态:开始屏幕、游戏屏幕、中间人屏幕、游戏结束屏幕。仅绘制该模式的内容,然后根据输入更改模式。基本上,与其检查游戏模式,然后在生命耗尽时结束游戏,不如切换到游戏结束模式,让这部分代码将游戏吸引到屏幕上。你的生命数量减少得太快了。将寿命设置为300而不是3,以便更好地了解正在发生的事情。还有,背景0;电话看起来有点奇怪,也许你的意思是在顶部有一个?