2015-11-05 83 views
0

我創建一個碰撞檢測遊戲,其中:試圖讓我的遊戲的生命和遊戲結束部分工作

  • 每次我打在牆上,我的生命的數量下降。
  • 一旦我的生命值爲0,遊戲就結束了。

但是遊戲讓我陷入消極的生活。另外,一旦你贏了我的點擊開始似乎也沒有工作......有人知道如何解決這個問題嗎?

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; 
    } 
} 
+0

你正在減少生命的數量太快。將''''''設置爲300而不是3,以更好地瞭解發生了什麼。另外,'''background(0);'''調用看起來有點奇怪,也許你打算在頂部有一個調用? –

回答

0

請記住,你的鼠標進入首次草圖之前,mouseXmouseY都是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變量做到這一點,但你也混合了事件代碼和繪圖代碼。相反,使每個模式都處於一種狀態:開始屏幕,播放屏幕,「在兩人之間」屏幕,遊戲結束屏幕。只繪製該模式的內容,然後根據輸入更改模式。基本上,如果不檢查「播放模式」,然後在用完遊戲時畫「遊戲結束」,只需切換到「遊戲結束模式」,讓代碼的這一部分將「遊戲結束」畫到屏幕上。