-
Notifications
You must be signed in to change notification settings - Fork 29
/
tron.py
60 lines (46 loc) · 1.23 KB
/
tron.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
"""Tron, classic arcade game.
Exercises
1. Make the tron players faster/slower.
2. Stop a tron player from running into itself.
3. Allow the tron player to go around the edge of the screen.
4. How would you create a computer player?
"""
from turtle import *
from freegames import square, vector
p1xy = vector(-100, 0)
p1aim = vector(4, 0)
p1body = set()
p2xy = vector(100, 0)
p2aim = vector(-4, 0)
p2body = set()
def inside(head):
"Return True if head inside screen."
return -200 < head.x < 200 and -200 < head.y < 200
def draw():
"Advance players and draw game."
p1xy.move(p1aim)
p1head = p1xy.copy()
p2xy.move(p2aim)
p2head = p2xy.copy()
if not inside(p1head) or p1head in p2body:
print('Player blue wins!')
return
if not inside(p2head) or p2head in p1body:
print('Player red wins!')
return
p1body.add(p1head)
p2body.add(p2head)
square(p1xy.x, p1xy.y, 3, 'red')
square(p2xy.x, p2xy.y, 3, 'blue')
update()
ontimer(draw, 50)
setup(420, 420, 370, 0)
hideturtle()
tracer(False)
listen()
onkey(lambda: p1aim.rotate(90), 'a')
onkey(lambda: p1aim.rotate(-90), 'd')
onkey(lambda: p2aim.rotate(90), 'j')
onkey(lambda: p2aim.rotate(-90), 'l')
draw()
done()