在垂直轴上处理球棒和球类比赛

时间:2016-10-04 09:11:47

标签: processing

这是我现在的代码,但是我想把球棒放在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
} 

2 个答案:

答案 0 :(得分:0)

由于需要快速帮助,如果你问一个特定的“我试过X,期望Y,但得到Z而不是”类型问题,你会有更好的运气。除了提供一般提示之外,很难帮助一般的“我该怎么做”这类问题。

话虽如此,我可以想到两种主要方法:

选项一:将您的问题分解为更小的步骤。从空白草图开始,然后按照您希望的方式获得只需要桨。然后尝试添加球。在那之后,尝试添加游戏逻辑。这是更好的选择。

选项二:由于您似乎几乎可以上下工作,可以使用rotate()功能将所有内容旋转90度。这有点像黑客。

答案 1 :(得分:0)

谢谢Kevin,最后让它工作了,原来我只需要将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
   }