【问题标题】:Processing Programming Modification [closed]处理编程修改[关闭]
【发布时间】:2020-03-03 14:28:49
【问题描述】:

我刚开始在 Processing 中学习编程,我需要帮助来修改程序。我需要帮助修改我的代码,以便每当鼠标按下目标时,目标会改变方向并垂直移动,然后当再次按下目标时,目标会再次变为水平移动。我希望这在整个游戏中不断发生。

这是目前为止的代码:

float asize = 40;
float y = height;
int start;                      //Position of the ball
int speed=1;                   //ball speed
int balldirect=1;                    //ball's direction
int score=0;                   //Your score
int lives=5;                   
boolean lostcond =false;           
boolean wincond = false;

void setup()                   
{
  size (400,400);
  smooth();
  start=width/2;               
  fill(255,0,0);               
  textSize(13);                
}

void draw()                                      
{
  background (0);                                

  ellipse(start, height/2,asize,asize);                 
  start=start+(speed*balldirect);                        //update the ball's position 
  if (start > width-20 || start<20)                //if ball hits side of screen
  {
    balldirect=-balldirect;                                  //change directions
  }
  text("Your score = "+score,10,10);                  
  text("lives left= "+lives,width-80,10);            
  if (lives<=0)                                  
  {
    textSize(20);
    text("You Lost. Click to Restart", 125,100);
    noLoop();                                    
    lostcond=true;
    textSize(13);
  }
}

void mousePressed()                              
{
  if (dist(mouseX, mouseY, start, 200)<=20)      //if target hit
  {
    score=score+speed;                           //Increase the speed
    speed=speed+1;                               //Increase the Score

    asize=asize-2;
    y++;
    if (y > height) {
      y = 0;
    }
  }
  else                                           //We missed
  {
    if (speed<1)                                 //If speed is greater than 1 decrease the speed
    {
      speed=speed-1;
    }
    lives=lives-1;                               //Take away one life
  }
  if (lostcond==true)                                //If we lost the game, reset now and start over 
  {
    speed=1;                                     //Reset all variables
    lives=5;
    score=0;
    start=width/2;
    balldirect=1;
    lost=false;
    loop();                                     
  }
  if(score == 100){
    wincond = true;
    textSize(20);
    text("You won!!!", 125,100);
    noLoop();
  }
}

【问题讨论】:

  • 您的问题/问题是什么?你有什么尝试自己解决这个问题?您可以分享任何错误吗?
  • 你已经有了你要找的逻辑,但是应用在 X 轴上:start=start+(speed*balldirect); 阅读并尝试应用在 Y 轴上。
  • 如果不代替您做,很难帮助您。尝试一些东西,如果失败了再回来,那么你会有一个我们可以回答的具体问题,这既可以帮助你提高技术,也可能对未来遇到类似问题的用户有用。

标签: java animation processing


【解决方案1】:

为位置(posxposy)和方向(balldirectxballdirecty)创建变量;

int posx;
int posy;
int balldirectx = 1;
int balldirecty = 0;

通过屏幕中心初始化位置:

void setup() {
    size(400, 400);
    posx = width/2;     
    posy = height/2;

    // [...]
}

移动小球并反转相应的方向,如果小球碰到窗口的边框:

void draw() {
    posx += speed * balldirectx;
    if (posx < asize || posx > width-asize) {
        posx = (int)max(asize, min(width-asize, posx));
        balldirectx *= -1;
    }
    posy += speed*balldirecty;
    if (posy < asize || posy > height-asize) {
        posy = (int)max(asize, min(width-asize, posy));
        balldirecty *= -1;
    }

    background (0);                                
    ellipse(posx, posy, asize, asize); 

    // [...]
}

击球时交换balldirectxballdirecty

void mousePressed()                              
{
    if (dist(mouseX, mouseY, posx, posy) <= asize) {
        score = score+speed;
        speed = speed+1;
        if (asize > 5) {
            asize = asize-2;
        }

        int temp = balldirectx;
        balldirectx = balldirecty;
        balldirecty = temp;

    } else {
        if (speed > 1) {
            speed = speed-1;
        }
        lives=lives-1;
    }
    if (lostcond==true) {
        posx = width/2;     
        posy = height/2;
        balldirectx = 1;
        balldirecty = 0;

        // [...]
    }

    // [...]
}

【讨论】:

    猜你喜欢
    • 2018-01-17
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    相关资源
    最近更新 更多