Objetos: ejemplo coche
Car myCar1;
Car myCar2;
void setup() {
size(800,600);
myCar1 = new Car(color(255,0,0),800,400,-5);
myCar2 = new Car(color(0,0,255),0,300,5);
}
void draw() {
background(255);
myCar1.drive();
myCar1.display();
text(myCar1.xpos,400,200);
myCar2.drive();
myCar2.display();
}
class Car {
color c;
float xpos;
float ypos;
float xspeed;
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 && xspeed>0) {
xpos = 0;
}
xpos = xpos + xspeed;
if (xpos < 0 && xspeed<0) {
xpos = 800;
}
}
}
Comentarios
Publicar un comentario