====== Assignment 7 ======
Create a project in your open processing with this code. Make sure you put your name at the top with the date and project description.
- Comment every line of the code to explain what it does
- Change the square to a ball
- When the user clicks the screen, reset the position of the ball to where the mouse is
// Credit: Ren Ervin Bettendorf
/* SCREENSAVERS FER DAYS BROSKIES */
void setup(){
size(600,500);
background(0);
frameRate(60);
}
int dx = 2;
int dy = 3;
int x = 50;
int y = 50;
void draw(){
fill(255);
rect(x,y,25,25,5);
x = x + dx;
y = y + dy;
if ( x< 0 || x > 475){
dx = -1 * dx;
}
if ( y < 0 || y > 475){
dy = -1 * dy;
}
}
If you get done early, check out this code from http://processing.org/tutorials/objects/:
// http://processing.org/tutorials/objects/
// Example: Two Car objects
Car myCar1;
Car myCar2; // Two objects!
void setup() {
size(200,200);
// Parameters go inside the parentheses when the object is constructed.
myCar1 = new Car(color(255,0,0),0,100,2);
myCar2 = new Car(color(0,0,255),0,10,1);
}
void draw() {
background(255);
myCar1.drive();
myCar1.display();
myCar2.drive();
myCar2.display();
}
// Even though there are multiple objects, we still only need one class.
// No matter how many cookies we make, only one cookie cutter is needed.
class Car {
color c;
float xpos;
float ypos;
float xspeed;
// The Constructor is defined with arguments.
Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
xspeed = tempXspeed;
}
void display() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos,ypos,20,10);
}
void drive() {
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
}
}
}
Once you are done that, check this out: http://www.openprocessing.org/sketch/103348