-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathracer2.py
More file actions
109 lines (74 loc) · 3.08 KB
/
racer2.py
File metadata and controls
109 lines (74 loc) · 3.08 KB
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
"""
In this version we have created a Racer class instead of just using a circle object to represent the race-car.
"""
# You need the file graphics.py from https://mcsp.wartburg.edu/zelle/python/
from graphics import *
speedLimit = 5 # How fast is the racer allowed to move
animationSpeed = 50 # How fast does time progress (higher value makes it faster), unit is updates per second
class Racer:
def __init__(self, win):
self.circle = Circle(Point(0,0), 10)
self.circle.setFill('blue')
self.circle.draw(win)
# New feature - car now has a cool-looking red R on it!
self.letter = Text(Point(0,0), "R")
self.letter.setTextColor('red')
self.letter.draw(win)
self.xvelocity = 0
self.yvelocity = 0
def _moveRelative(self, xDiff,yDiff):
self.circle.move(xDiff, yDiff)
# Don't forget to move the 'R'
self.letter.move(xDiff, yDiff)
def _moveAbsolute(self, x,y):
self._moveRelative(x-self.getX(),y-self.getY())
def accelerate(self, xacc, yacc):
self.xvelocity += xacc
self.yvelocity += yacc
# Enforce the speedlimit
self.yvelocity = min(speedLimit, self.yvelocity)
self.yvelocity = max(-speedLimit, self.yvelocity)
self.xvelocity = min(speedLimit, self.xvelocity)
self.xvelocity = max(-speedLimit, self.xvelocity)
def slowDown(self):
self.xvelocity *= 0.9
self.yvelocity *= 0.9
def drive(self):
self._moveRelative(self.xvelocity, self.yvelocity)
def reset(self):
self._moveAbsolute(0,0)
self.xvelocity = 0
self.yvelocity = 0
def getX(self):
return self.circle.getCenter().getX()
def getY(self):
return self.circle.getCenter().getY()
# Create the main window. Disabling autoflush means the window will only be updated when update() is called.
win = GraphWin("Racer" , 640, 480, autoflush=False)
# This line sets (0,0) to be the middle of the window
win.setCoords(-320, -240, 320, 240)
# Use the constructor of the Racer-class to create a race-car
racer = Racer(win)
# Main animation loop
# * If the user presses an arrow key accelerate in that direction (up to speed limit)
# * If the user presses space, apply breaks to reduce speed
# * If the user presses backspace the simulation is restarted
# The speed of the loop is controlled by the update() function
while(True):
key = win.checkKey() # The key being pressed (if any)
if key == "Up":
racer.accelerate(0,1)
elif key == "Down":
racer.accelerate(0,-1)
elif key == "Right":
racer.accelerate(1,0)
elif key == "Left":
racer.accelerate(-1,0)
elif key == "space":
racer.slowDown()
elif key == "BackSpace":
racer.reset()
# Move the racer based on current veclocity
racer.drive()
# Waits for the animation step to finish based on animationspeed, then redraws the window
update(animationSpeed)