Video:
Code:
boolean play;
int x, y;
int size = 50;
int speedX, speedY ;
void setup() {
play = true;
size(600, 600);
background(0);
fill(58,90,150);
speedX = int(random(10,20));
speedY = int(random(5,10));
rectMode(CENTER);
}
void draw() {
if (play) {
background(0);
if (mousePressed == true) {
}
ellipse(x,y,size,size);
x = x + speedX;
y = y + speedY;
ballBounce();
}
}
void mousePressed() {
int r = int(random(0, 255));
int g = int(random(0, 255));
int b = int(random(0, 255));
fill (r, g, b);
noStroke();
ellipse(x,y,size,size);
}
void ballBounce(){
if ((x > width) || (x < 0)){
speedX *= -1;
}
if ((y >height)||(y<0)){
speedY *= -1;
}
}
void keyPressed() {
play = !play;
}
Process:
I used the code of a bouncing ball which we learned in the last course as the basis of my work. And I added two more functions to it: every time when you press the mouse, the ball would change to a random color; and when you press a random key, the ball’s moving would be paused until next key press.
I wanted to achieve an idea that when you pause the ball and press the key at that time, a new ball with random color would appear, and when you press next key to restart the ball’s moving, the new ball would be reserved and move with the original one together; in this way, there can be more and more balls on the screen through pause and mouse press. But due to the lack of my knowledge about coding and the time limit in the class, I did not complete this idea. I will continue to work on it in my free time.