2016-10-04 57 views
1

這是我目前的代碼,但我想把蝙蝠放在y軸上,並使其垂直而不是水平地上下移動,並使球彈跳從左到右的動作而不是上下。需要快速幫助。謝謝。在垂直軸上處理蝙蝠和球的遊戲

int x=250; // Horizontal position of ball 
int direction_x=2; // Change in horizontal position each time draw() executed 
int y=150; // Vertical position of ball 
int direction_y=2; // Change in horizontal position each time draw() executed 
int lives=3; 
int score=0; 

void setup() 
{ 
    size(400,400); // Create a window 400x400 pixels 
} 
void draw() 
{ 
    background(255,255,255); // Clear screen to white 
    fill(0,255,0); // Set fill colour to blue 
    rect(mouseY-60,380,120,20); // Position rectangle using mouse 

    fill(0,0,255); 
    ellipse(x,y,20,20); // Draw blue disk centered on x,y diameter 20 
    x=x+direction_x; // Update position 
    if(x<10) direction_x=-direction_x; // Reverse direction if hit boundary 
    if(x>(width-10)) direction_x=-direction_x; 

    y=y+direction_y; 
    if(y<10) direction_y=-direction_y; 
    // if(y>(height-10)) direction_y=-direction_y; 

    if(y>(height-10)) // If ball bits bottom of screen then miss.. 
    { 
     direction_y=-direction_y; // Bounce 
     lives--; // Reduce lives by one 
     if(lives==0) exit(); // If lives is zero then quit 
    } 

    if((y>(height-30))&&(abs(mouseX-x)<60)) // If ball has bit paddle then.. 
    { 
     direction_y=-direction_y; // Bounce 
     score++; // Increase score by one 
    } 

    textSize(32); 
    fill(0,0,255); 
    text(score, 10, 30); // Display score 
    text(lives,width-30, 30); // Display lives 
} 

回答

0

由於需要快速幫助,如果您問一個特定的「我試過X,預期是Y,但得到了Z而不是」類型問題,那麼運氣會更好。除了提供一般提示之外,很難提供一般的「我該怎麼做」類型的問題。

話雖這麼說,有一些我能想到的方法主要有兩種:

方案一:下打破你的問題分解成更小的步驟。從空白草圖開始,並獲得只是槳工作如何你想要它。然後嘗試添加球。在這之後,嘗試添加遊戲邏輯。這是更好的選擇。

方案二:因爲你似乎幾乎都有這方面的工作上上下下,你可能只是使用rotate()功能都旋轉90度。儘管如此,這仍然有點不妥。

0

感謝凱文,最後得到它的工作,事實證明我只需要將paddle的座標從(mouseX,0,0,0)更改爲(0,mouseY,0,0)並且需要添加在另外一行代碼中改變軸參數。我做了我過去的複雜事情的老手段。

int x=250; // Horizontal position of ball 
    int direction_x=2; // Change in horizontal position each time draw() executed 
    int y=150; // Vertical position of ball 
    int direction_y=2; // Change in horizontal position each time dra) executed 
    int lives=3; 
    int score=0; 
    void setup() 
    { 
    size(400,400); // Create a window 400x400 pixels 
    } 
    void draw() 
    { 
    background(255,255,255); // Clear screen to white 
    fill(0,255,0); // Set fill colour to blue 
    rect(0,mouseY-60,20,120); // Position rectangle using mouse 

    fill(0,0,255); 
    ellipse(x,y,20,20); // Draw blue disk centered on x,y diameter 20 
    x=x+direction_x; // Update position 
    if(x<10) direction_x=-direction_x; // Reverse direction if hit boundary 
    if(x>(width-10)) direction_x=-direction_x; 

    y=y+direction_y; 
    if(y<10) direction_y=-direction_y; 
    // if(y>(height-10)) direction_y=-direction_y; 

    if(y>(height-10)) // If ball bits bottom of screen then miss.. 
    { 
    direction_y=-direction_y; // Bounce 
    lives--; // Reduce lives by one 
    if(lives==0) exit(); // If lives is zero then quit 
    } 

    if(x<10) 
    { 
    direction_x=-direction_x; // Bounce 
    x=30; // Force x to beyond the paddle on a restart 
    lives--; // Reduce lives by one 
    if(lives==0) exit(); // If lives is zero then quit 
    } 

    if((x<30)&&(abs(mouseY-y)<60)) // If ball has bit paddle then.. 
    { 
    direction_x=-direction_x; // Bounce 
    score++; // Increase score by one 
    } 

    textSize(32); 
    fill(0,0,255); 
    text(score, 10, 30); // Display score 
    text(lives,width-30, 30); // Display lives 
    }