-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
55 lines (41 loc) · 1.75 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# ==============================================================================================================
# Carrera de torturas con la clase Turtle()
# Proyecto que muestra cómo utilizar objecto de tipo Turtle
# Tendremos una clase: Circuito que tendrá 2 propiedades: corredores (array) y pantalla y un método: competir()
# ==============================================================================================================
# Importamos turtle
import turtle
import random
class Circuito():
corredores = []
__posStartY = (-30, -10, 1, 30)
__colorTurtle = ('red', 'blue', 'green', 'orange')
def __init__(self, width, height):
self.__screen = turtle.Screen() # Dejamos que screen sea privado, además usa el método screen de turtle
self.__screen.setup(width, height)
self.__screen.bgcolor('lightgray')
self.__startLine = -width/2 + 20
self.__finishLine = width/2 -20
self.__createRunners()
def __createRunners(self):
for i in range(4):
new_turtle = turtle.Turtle()
new_turtle.color(self.__colorTurtle[i])
new_turtle.shape('turtle')
new_turtle.penup()
new_turtle.setpos(self.__startLine, self.__posStartY[i])
self.corredores.append(new_turtle)
def competir(self):
hayGanador = False
while not hayGanador:
for tortuga in self.corredores:
avance = random.randint(1, 6)
tortuga.fd(avance)
if tortuga.position()[0] >= self.__finishLine:
hayGanador = True
print("La tortuga de color {} ha ganado".format(tortuga.color()[0]))
break
# mirar desde donde se ejecuta
if __name__ == '__main__':
circuito = Circuito(640, 480)
Circuito.competir()