Processing Programming Modification

Viewed 49

I just started learning to program in Processing and I need help modifying a program. I need help modifying my code so that whenever the mouse presses the target, the target changes directions and moves vertically, then when it’s pressed again on the target, the target changes to moving horizontally again. I want this to continuously happen throughout the game.

Here's the code so far:

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();
  }
}
1 Answers

Create variables for the position (posx, posy) and the direction (balldirectx, balldirecty);

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

Init the position by the center of the screen:

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

    // [...]
}

Move the ball and invert the corresponding direction, if the ball hits the border of the window:

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); 

    // [...]
}

Swap balldirectx and balldirecty when the ball is hit:

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;

        // [...]
    }

    // [...]
}
Related