Coche arriba y abajo
Car myCar1;
Car myCar2; // Two objects!
void setup() {
size(600, 400);
// Arguments go inside the parentheses when the object is constructed.
myCar1 = new Car(color(51), 450, 100, 5);
myCar2 = new Car(color(151), 150, 50, -2);
}
void draw() {
background(255);
myCar1.move();
myCar1.display();
myCar2.move();
myCar2.display();
}
// Even though there are multiple objects, only one class is needed.
// No matter how many cookies you make, only one cookie cutter is needed.
class Car {
color c;
float xpos;
float ypos;
float yspeed;
// The Constructor is defined with parameters.
Car(color tempC, float tempXpos, float tempYpos, float tempyspeed) {
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
yspeed = tempyspeed;
}
void display() {
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos, ypos, 20, 10);
}
void move() {
ypos = ypos + yspeed;
if (ypos > height && yspeed>0){
ypos = 0;
}
if (ypos < 0 && yspeed<0){
ypos = 350;
}
}
}
Comentarios
Publicar un comentario