-
Notifications
You must be signed in to change notification settings - Fork 2
/
TurtleTron.py
290 lines (267 loc) · 8.54 KB
/
TurtleTron.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import turtle
import time
import random
#Made By RedRiver559#5916
#V0.021
#Thanks And Enjoy!
#Todo : More Optimizations and eventually add AI and powerups.
Size = 0.5
if __name__ == '__main__':
wn = turtle.Screen()
wn.title('Turtle Tron')
wn.bgcolor('black')
wn.screensize(320,320)
wn.setup(width=1200, height=950,startx=None,starty=None)
wn.tracer(0)
cyan_history = set()
magenta_history = set()
#Specials (Coming soon...)
#Y grid lines
grid_y = turtle.Turtle()
x = -320
for y_lines in range(80):
grid_y.color('#2F4F4F')
grid_y.penup()
grid_y.goto(x, 320)
grid_y.pendown()
grid_y.goto(x, -320)
grid_y.hideturtle()
x += 8
#X grid lines
grid_x = turtle.Turtle()
y = -320
for x_lines in range(80):
grid_x.color('#2F4F4F')
grid_x.penup()
grid_x.goto(320, y)
grid_x.pendown()
grid_x.goto(-320, y)
grid_x.hideturtle()
y += 8
#Bike Initilization
class magenta_bike_init(turtle.Turtle):
def __init__(self, color, pensize, shape, speed):
#Magenta Bike
super().__init__(shape)
self.color(color)
self.pensize(pensize)
self.speed(speed)
magenta_bike = magenta_bike_init("#FF00FF", 3, "turtle", 8)
magenta_bike.shapesize(stretch_wid=Size, stretch_len=Size, outline=1)
magenta_bike.penup()
magenta_bike.sety(224)
magenta_bike.setheading(-90)
magenta_bike.pendown()
#
class cyan_bike_init(turtle.Turtle):
def __init__(self, color, pensize, shape, speed):
#Cyan Bike
super().__init__(shape)
self.color(color)
self.pensize(pensize)
self.speed(speed)
cyan_bike = cyan_bike_init("#00FFFF", 3, "turtle", 8)
cyan_bike.shapesize(stretch_wid=Size, stretch_len=Size, outline=1)
cyan_bike.penup()
cyan_bike.sety(-224)
cyan_bike.setheading(90)
cyan_bike.pendown()
#Score
class score_system:
def __init__(self):
self.magenta_score = 0
self.cyan_score = 0
def mag_add_one(self):
self.magenta_score += 1
def cyan_add_one(self):
self.cyan_score += 1
one = score_system()
score = turtle.Turtle()
score.pensize(2)
score.pencolor('White')
score.penup()
score.goto(0,340)
score.pendown()
score.write(f'Cyan {one.cyan_score}, Magenta {one.magenta_score}',align="center",font=("Courier", 24, "normal"))
score.hideturtle()
#Author
name = turtle.Turtle()
name.pensize(2)
name.hideturtle()
name.pencolor('White')
name.penup()
name.goto(0,320)
name.pendown()
name.write('Made By RedRiver559#5916',align='center',font=("Courier", 8, "normal"))
#Controls
info = turtle.Turtle()
info.color('white')
info.penup()
info.goto(-460,230)
info.pendown()
info.write('Cyan Controls\nW = Up\nS = Down\nA = Left\nD = Right',align='Left',font=("Courier", 12, "normal"))
info.penup()
info.home()
info.goto(500,230)
info.pendown()
info.write('Magenta Controls\n↑ = Up\n↓ = Down\n← = Left\n→ = Right',align='Right',font=("Courier", 12, "normal"))
info.hideturtle()
#border
pen = turtle.Turtle()
pen.color('White')
pen.penup()
pen.setposition(-320,-320)
pen.pendown()
pen.pensize(1)
pen.shape('square')
pen.hideturtle()
for x in range(4):
pen.forward(640)
pen.left(90)
wn.update()
pen.penup()
pen.goto(0,380)
pen.pendown()
pen.pencolor('#2F4F4F')
pen.write('Turtle Tron',align="center",font=("Courier", 48, "normal"))
#Controls (Up, Down, Left, Right)
#checking if it can reverse in the opposite x or y direction vs its current pos.
#Cyan Bike
def cyan_up():
if cyan_bike.heading() - 270 != 0:
cyan_bike.setheading(90)
def cyan_down():
if cyan_bike.heading() != 90 or 0:
cyan_bike.setheading(-90)
def cyan_right():
if cyan_bike.heading() != 180:
cyan_bike.setheading(0)
def cyan_left():
if cyan_bike.heading() != 0:
cyan_bike.setheading(180)
#Magenta Bike
def magenta_up():
if magenta_bike.heading() - 270 == 0:
pass
else:
magenta_bike.setheading(90)
def magenta_down():
if magenta_bike.heading() == 90 or 0:
pass
else:
magenta_bike.setheading(-90)
def magenta_right():
if magenta_bike.heading() == 180:
pass
else:
magenta_bike.setheading(0)
def magenta_left():
if magenta_bike.heading() == 0:
pass
else:
magenta_bike.setheading(180)
#Explosion Particles
explosion_effects = []
def bike_explosion(x,y):
explosion = turtle.Turtle()
explosion.pensize(1)
explosion.hideturtle()
explosion.penup()
explosion.setposition(x,y)
explosion.pendown()
explosion.color('#FF4500')
for _ in range(7):
length = random.randint(10,30)
if explosion.stamp() in explosion_effects:
explosion.clearstamps()
else:
explosion_effects.append(explosion.stamp())
explosion.forward(length)
explosion.backward(length)
explosion.left(50)
wn.update()
time.sleep(1)
explosion.clear()
def cyan_win(): # Win system for adding points
#print('Cyan Wins!')
one.cyan_add_one()
score.clear()
score.write(f'Cyan {one.cyan_score}, Magenta {one.magenta_score}',align="center",font=("Courier", 24, "normal"))
def magenta_win():
#print('Magenta Wins!')
one.mag_add_one()
score.clear()
score.write(f'Cyan {one.cyan_score}, Magenta {one.magenta_score}',align="center",font=("Courier", 24, "normal"))
#Resets ALL Coords and line historys.
def Reset():
magenta_bike.sety(224)
magenta_bike.setx(0)
cyan_bike.sety(-224)
cyan_bike.setx(0)
magenta_bike.setheading(-90)
cyan_bike.setheading(90)
cyan_bike.clear()
magenta_bike.clear()
magenta_history.clear()
cyan_history.clear()
wn.update()
def magenta_reset(): # Main reset system for each bike
bike_explosion(x_cyan,y_cyan)
magenta_win()
Reset()
def cyan_reset():
bike_explosion(x_magenta,y_magenta)
cyan_win()
Reset()
#Bike and keyboard listeners.
#Cyan
wn.onkey(cyan_up,'w')
wn.onkey(cyan_down,'s')
wn.onkey(cyan_right,'d')
wn.onkey(cyan_left,'a')
#Magenta
wn.onkey(magenta_up,'Up')
wn.onkey(magenta_down,'Down')
wn.onkey(magenta_right,'Right')
wn.onkey(magenta_left,'Left')
#Main Game Loop
while True:
wn.listen()
wn.update()
time.sleep(0.1)
cyan_bike.forward(cyan_bike.speed())
magenta_bike.forward(magenta_bike.speed())
#Position Logging
x_cyan, y_cyan = round(cyan_bike.xcor()), round(cyan_bike.ycor())
x_magenta, y_magenta = round(magenta_bike.xcor()), round(magenta_bike.ycor())
#Border Detection/ Border Scoring
if (x_cyan, y_cyan) in magenta_history or (x_cyan, y_cyan) in cyan_history: #checks if the current cyan coords are in magentas history or cyan's history
print('Maginta Wins!')
magenta_reset()
elif (x_magenta, y_magenta) in cyan_history or (x_magenta, y_magenta) in magenta_history: #checks if the current magenta coords are in cyan's history or magenta's history
print('Cyan Wins!')
cyan_reset()
#Border Control
elif cyan_bike.xcor() >= 320 or cyan_bike.xcor() <= -320: #Boundry Wall
print('Out Of Bounds')
magenta_reset()
elif cyan_bike.ycor() >= 320 or cyan_bike.ycor() <= -320: #Boundry Wall
print('Out Of Bounds')
magenta_reset()
elif magenta_bike.xcor() >= 320 or magenta_bike.xcor() <= -320: #Boundry Wall
print('Out Of Bounds')
cyan_reset()
elif magenta_bike.ycor() >= 320 or magenta_bike.ycor() <= -320: #Boundry Wall
print('Out Of Bounds')
cyan_reset()
elif (x_cyan,y_cyan) == (x_magenta,y_magenta): #Head on collisions. Ties
print('Tie')
bike_explosion(x_magenta,y_magenta)
Reset()
else:
cyan_history.add((x_cyan,y_cyan))
magenta_history.add((x_magenta,y_magenta))
#Adding New X/Y Coords To Sets/Historys at the end of the loop.
#Just some informational logs if you want to see how it works.
#print(f'Cyan Logging :\n \t{cyan_history}')
#print(f'Magenta Logging :\n ,\t{magenta_history}')