Several weeks ago we built Frogger in class. We created a Car class and then built a number of car objects (car0, car1, car2, etc). What do these objects have in common?
This is a perfect use case for an array. Especially if we want to make a large number of cars.
Let’s re-use our car class:
class Car {
float x;
float y;
float xspeed;
color c;
Car(float tempX, float tempY, float tempXspeed, color tempC){
c = tempC;
x = tempX;
y = tempY;
xspeed = tempXspeed;
}
void display(){
rectMode(CENTER);
stroke(0);
fill(c);
rect(x, y, 20, 10);
}
void move(){
x = x + xspeed;
if (x > width){
x = 0;
}
if (x < 0){
x = width;
}
}
}
Let’s say we have 2 cars.
Our main code:
Car car0, car1;
void setup(){
size(500,500);
car0 = new Car(100,150,1,color(100,0,100));
car1 = new Car(300,230,2,color(220,0,120));
}
void draw(){
background(255);
car0.display();
car0.move();
car1.display();
car1.move();
}
What happens if we have more than 2 cars. What if we have 10 cars? 30 cars?
It would be a lot easier if we didn’t have type in all of the cars individually.
Car[] cars = new Car[20]; //an array of 20 cars
void setup(){
size(500,500);
for (int i = 0; i < cars.length; i++){
cars[i] = new Car(random(width),random(height),random(2),color(random(255)));
}
}
void draw(){
for (int i = 0; i < cars.length; i++){
cars[i].display();
cars[i].move();
}
}
Processing comes with a Debugger.
What’s a bug?
an error, flaw, failure or fault in a computer program or system that causes it to produce an incorrect or unexpected result, or to behave in unintended ways. Wikipedia
Bugs cause disasters:
After all of the other work we’ve done this semester, working with images is quite easy.
To work with images, declare a global variable of type PImage
PImage img1;
PImage img2;
//we'll have two image files
Make a new instance of PImage by loading in an image file in your setup
void setup(){
size(500,500);
img1 = loadImage("dawg.jpg");
img2 = loadImage("cat.jpg");
}
Now that we’ve loaded our image, we can draw them to the screen.
void draw(){
imageMode(CORNER); //the default, like rectMode's default
image(img1,0,0); //now draw it to screen at x,y
imageMode(CENTER);
image(img2,width/2,height/2,200,200);
//draws img2 in center as a square 200 wide, 200 high
//width and height are optional parameters
}
Images can be manipulated just as other shapes. For example, they can be drawn on screen at mouseX,mouseY
.
noCursor();
image(myImg,mouseX,mouseY); //replace the cursor with an image
In the data folder.
Go to Sketch > Add file OR Sketch > Show Sketch Folder and drag your image files in there.
tint()
tint(255) // original state
tint(100) // darker
adding a second argument, for transparency
tint(255, 127) //50% opacity
You can change brightness of red, green, blue components of color
tint(200, 0, 255) //most red, no green, all blue components
Note: If you use a tint on one image but want NO TINT on another, make sure you set it back to the default tint(255);
.
tint(200,0,0,127)
Grading Rubric for final of virtual pet project: