1
+ import math
2
+ import random
3
+ import turtle
4
+ #import time
5
+
6
+ win_length = 500
7
+ win_height = 500
8
+
9
+ turtles = 8
10
+
11
+ turtle .screensize (win_length , win_height )
12
+
13
+
14
+ class racer (object ):
15
+ def __init__ (self , color , pos ):
16
+ self .pos = pos
17
+ self .color = color
18
+ self .turt = turtle .Turtle ()
19
+ self .turt .shape ('turtle' )
20
+ self .turt .color (color )
21
+ self .turt .penup ()
22
+ self .turt .setpos (pos )
23
+ self .turt .setheading (90 )
24
+
25
+ def move (self ):
26
+ r = random .randrange (1 , 20 )
27
+ self .pos = (self .pos [0 ], self .pos [1 ] + r )
28
+ self .turt .pendown ()
29
+ self .turt .forward (r )
30
+
31
+ def reset (self ):
32
+ self .turt .penup ()
33
+ self .turt .setpos (self .pos )
34
+
35
+
36
+ def setupFile (name , colors ):
37
+ file = open (name , 'w' )
38
+ for color in colors :
39
+ file .write (color + ' 0 \n ' )
40
+ file .close ()
41
+
42
+
43
+ def startGame ():
44
+ tList = []
45
+ turtle .clearscreen ()
46
+ turtle .hideturtle ()
47
+ colors = ["red" , "green" , "blue" , 'yellow' , 'pink' , 'orange' , 'purple' , 'black' , 'grey' ]
48
+ start = - (win_length / 2 ) + 20
49
+ for t in range (turtles ):
50
+ newPosX = start + t * (win_length )// turtles
51
+ tList .append (racer (colors [t ],(newPosX , - 230 )))
52
+ tList [t ].turt .showturtle ()
53
+
54
+ run = True
55
+ while run :
56
+ for t in tList :
57
+ t .move ()
58
+
59
+ maxColor = []
60
+ maxDis = 0
61
+ for t in tList :
62
+ if t .pos [1 ] > 230 and t .pos [1 ] > maxDis :
63
+ maxDis = t .pos [1 ]
64
+ maxColor = []
65
+ maxColor .append (t .color )
66
+ elif t .pos [1 ] > 230 and t .pos [1 ] == maxDis :
67
+ maxDis = t .pos [1 ]
68
+ maxColor .append (t .color )
69
+
70
+ if len (maxColor ) > 0 :
71
+ run = False
72
+ print ('The winner is: ' )
73
+ for win in maxColor :
74
+ print (win )
75
+
76
+ oldScore = []
77
+ file = open ('scores.txt' , 'r' )
78
+ for line in file :
79
+ l = line .split ()
80
+ color = l [0 ]
81
+ score = l [1 ]
82
+ oldScore .append ([color , score ])
83
+
84
+ file .close ()
85
+
86
+ file = open ('scores.txt' , 'w' )
87
+
88
+ for entry in oldScore :
89
+ for winner in maxColor :
90
+ if entry [0 ] == winner :
91
+ entry [1 ] = int (entry [1 ]) + 1
92
+
93
+ file .write (str (entry [0 ]) + ' ' + str (entry [1 ]) + '\n ' )
94
+
95
+
96
+ file .close ()
97
+
98
+
99
+ start = input ('Would you like to play, type "yes" or "no": ' ).lower ()
100
+ if start == "yes" :
101
+ print ('----------GAME IN PROGRESS--------' )
102
+ startGame ()
103
+ else :
104
+ quit ()
105
+
106
+ while True :
107
+ print ('-----------------------------------' )
108
+ start = input ('Would you like to play again, type "yes" or "no": ' ).lower ()
109
+ if start == "yes" :
110
+ print ('----------GAME IN PROGRESS--------' )
111
+ startGame ()
112
+ else :
113
+ print ('----------THANK YOU FOR PLAYING--------' )
114
+ quit ()
0 commit comments