Processing 在垂直轴上处理球拍和球类运动

Processing 在垂直轴上处理球拍和球类运动,processing,Processing,这是我目前掌握的代码,但我想把球棒放在y轴上,让它垂直上下移动,而不是水平移动,让球以从左到右的运动反弹,而不是上下移动。很快就需要帮助。谢谢 int x=250;//球的水平位置 int方向_x=2;//每次执行draw()时水平位置的变化 int y=150;//球的垂直位置 int方向_y=2;//每次执行draw()时水平位置的变化 int=3; 智力得分=0; 无效设置() { 大小(400400);//创建一个400x400像素的窗口 } 作废提款() { 背景(255255);//

这是我目前掌握的代码,但我想把球棒放在y轴上,让它垂直上下移动,而不是水平移动,让球以从左到右的运动反弹,而不是上下移动。很快就需要帮助。谢谢

int x=250;//球的水平位置
int方向_x=2;//每次执行draw()时水平位置的变化
int y=150;//球的垂直位置
int方向_y=2;//每次执行draw()时水平位置的变化
int=3;
智力得分=0;
无效设置()
{
大小(400400);//创建一个400x400像素的窗口
}
作废提款()
{
背景(255255);//将屏幕清除为白色
填充(0255,0);//将填充颜色设置为蓝色
rect(mouseY-60380120,20);//使用鼠标定位矩形
填充(0,0255);
椭圆(x,y,20,20);//以x,y直径20为中心绘制蓝色圆盘
x=x+方向\u x;//更新位置
如果(x(宽度-10))方向x=-方向x;
y=y+方向y;
如果(y(高度-10))方向y=-方向y;
如果(y>(高度-10))//如果球咬住屏幕底部,则未命中。。
{
方向y=-方向y;//反弹
生命--;//减少一条生命
if(lifes==0)exit();//如果lifes为零,则退出
}

如果((y>(height-30))&(abs(mouseX-x)因为需要快速的帮助,如果你问一个特定的“我尝试了x,期望了y,但得到了Z”类型的问题,你会有更好的运气。除了提供一般的提示之外,很难帮助你回答一般的“我该怎么做”类型的问题

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

选项一:将你的问题分解为几个小步骤。从一张空白草图开始,让只按你所希望的方式操作球拍。然后尝试添加球。在操作完成后,再尝试添加游戏逻辑。这是更好的选项


选项二:因为你几乎可以上下旋转,你可以使用
rotate()
函数将所有东西旋转90度。不过这有点麻烦。

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

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

选项一:将你的问题分解为几个小步骤。从一张空白草图开始,让只按你所希望的方式操作球拍。然后尝试添加球。在操作完成后,再尝试添加游戏逻辑。这是更好的选项


选项二:因为你几乎可以上下旋转,你可以使用
旋转()功能将所有东西旋转90度。不过这有点麻烦。

谢谢凯文,终于让它工作了,原来我只需要从(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
   } 
int x=250;//球的水平位置
int direction_x=2;//每次执行draw()时,水平位置都会发生变化
int y=150;//球的垂直位置
int direction_y=2;//每次执行dra时水平位置的变化
int=3;
智力得分=0;
无效设置()
{
大小(400400);//创建一个400x400像素的窗口
}
作废提款()
{
背景(255255);//将屏幕清除为白色
填充(0255,0);//将填充颜色设置为蓝色
rect(0,mouseY-60,20120);//使用鼠标定位矩形
填充(0,0255);
椭圆(x,y,20,20);//以x,y直径20为中心绘制蓝色圆盘
x=x+方向\u x;//更新位置
如果(x(宽度-10))方向x=-方向x;
y=y+方向y;
如果(y(高度-10))方向y=-方向y;
如果(y>(高度-10))//如果球咬住屏幕底部,则未命中。。
{
方向y=-方向y;//反弹
生命--;//减少一条生命
if(lifes==0)exit();//如果lifes为零,则退出
}

如果(x谢谢Kevin,终于让它工作了,结果我只需要将桨的坐标从(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
   } 
int x=250;//球的水平位置
int direction_x=2;//每次执行draw()时,水平位置都会发生变化
int y=150;//球的垂直位置
int direction_y=2;//每次执行dra时水平位置的变化
int=3;
智力得分=0;
无效设置()
{
大小(400400);//创建一个400x400像素的窗口
}
作废提款()
{
背景(255255);//将屏幕清除为白色
填充(0255,0);//将填充颜色设置为蓝色
rect(0,mouseY-60,20120);//使用鼠标定位矩形
填充(0,0255);
椭圆(x,y,20,20);//以x,y直径20为中心绘制蓝色圆盘
x=x+方向\u x;//更新位置
如果(x(宽度-10))方向x=-方向x;
y=y+方向y;
如果(y(高度-10))方向y=-方向y;
如果(y>(高度-10))//如果球咬住屏幕底部,则未命中。。
{
方向y=-方向y;//反弹
生命--;//减少一条生命
if(lifes==0)exit();//如果lifes为零,则退出
}
if(x)