PYTHON: coches con array
numCoches = 40
myCars = []
def setup():
size(800, 600)
global myCars
myCars = []
for i in range(numCoches):
c = color(random(255), random(255), random(255))
xpos = random(width - 10)
ypos = random(height - 10)
xspeed = random(-6, 6)
if xspeed == 0:
xspeed = 1
myCars.append(Car(c, xpos, ypos, xspeed))
def draw():
background(255)
for car in myCars:
car.drive()
car.display()
# Clase Car
class Car:
def __init__(self, c, xpos, ypos, xspeed):
self.c = c
self.xpos = xpos
self.ypos = ypos
self.xspeed = xspeed
def display(self):
stroke(0)
fill(self.c)
rectMode(CENTER)
rect(self.xpos, self.ypos, 20, 10)
def drive(self):
self.xpos = self.xpos + self.xspeed
if self.xpos > width:
self.xpos = 0
elif self.xpos < 0:
self.xpos = width
Comentarios
Publicar un comentario