Circles need to pause and un-pause when any key is pressed

I'm quite new to processing and I have this piece of code that I need to complete.

float xPos = 10000;

float yPos = 10000;

float dia = 40;

// Circle speed

float xSpeed = 6;

float ySpeed = 4;

// Reset circle position

float resetBall = -40;

// Pause ball coefficient of speed

float pauseBall = 0;

void setup() {

size(600, 400);

background(255);

frameRate(80);

}

void draw() {

background(255);

// Red crosses

fill(255, 0, 0);

noStroke();

rectMode(CENTER);

// Vertical Rectangle

rect(width/2, 0, dia, height*2);

// Horizontal Rectangle

rect(0, height/2, width*2, dia);

//Green circle

fill(0, 255, 0);

ellipse(xPos, height/2, dia, dia);

//Blue circle

fill(0, 0, 255);

ellipse(width/2, yPos, dia, dia);

//Green circle moves in the x direction

xPos = xPos + xSpeed;

//Blue circle moves in the y direction

yPos = yPos + ySpeed;

}

void mousePressed() {

//When mouse is clicked, the circles reset to the starting position

xPos = resetBall;

yPos = resetBall;

}

void keyPressed() {

// When any key is pressed, the circles pause if moving, and start moving if paused

xSpeed = pauseBall * xSpeed;

ySpeed = pauseBall * ySpeed;

pauseBall = pauseBall + 1;

}

The problem that I'm having is to create a piece of code when any key is pressed, if the circles are moving then it needs to pause and if the circles are the paused then they need to resume. I need to complete this without using conditional statements. I've created a variable called pauseBall and set the value to 0. If any key is pressed, then I multiply the ball speed by 0 which stops the the ball, now I need the pauseBall variable become 1 so that the next time I press any key, the ball will resume.

Essentially, I'm trying to change the co-efficient of speed from 1 to 0 to 1 to 0 and so on

Any help would be greatly appreciated. Many thanks

Tagged:

Answers

  • void keyPressed(){
    
      if(key=='p')
         looping=!looping;
    }
    

    Kf

  • Unfortunately, it's for an assignment and I need to do this without using an if statement

  • looping = key == 'p' ? !looping : looping;
    
  • edited December 2017

    "No if statement" -- interesting design restriction. Some things to think about:

    1. Can you use logic that contains implicit if-like behavior?

      • like the ternary operator, ?
      • like a switch statements?
    2. What about boolean operations, like != -- could you use those imperatives to switch something on and off without an "if"?

    3. Can you make the base speed change between 0 and some number (e.g. zero and 1) using only addition? Perhaps then you can ++ the speed and it will start and stop. Take a look at modulo %....

    4. What about binary operations like bitwise AND & or OR | -- could you perform a repeated operation on the lowest bit of an int?

    @Tfguy44's suggestion draws on a couple of these... and you should also be able to accomplish what you want using any one of them on its own....

  • looping ^= keyCode == 'P'; :ar!

    void draw() {
      background((color) random(#000000));
    }
    
    void keyPressed() {
      final int k = keyCode;
      looping ^= k == 'P';
    }
    
  • Based on the description of your co-efficient approach, I am guessing that a modulo-based approach is what you want to experiment with.

    If you aren't allowed to use if then I'm guessing you also aren't allowed to call loop() or directly manipulate the semi-undocumented looping.

  • Hi jeremydouglass, great tip, I figured it out by adding the code below:

    xSpeed = (xSpeed + 1.5) % 3; ySpeed = (ySpeed + 1) % 2;

    That way it switches between 0 and the speed.

Sign In or Register to comment.