-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
218 lines (191 loc) · 6.75 KB
/
game.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
from globals import FOODMAX, STEPMAX, WIDTH, HEIGHT, X_MAX, Y_MAX, MS_TO_QUIT
from globals import FOOD as food, GHOSTS as ghosts
# Import our own constants
from characters import ghost, doboggi, doboggi_man
# Import our game character classes
from p1 import auto_doboggi_man
# Import your self-moving auto_doboggi_man class
import turtle
import random
def shutDownHander():
"""Outputs score and terminates the program"""
global score
print('Your score:', score)
window.bye()
def checkCollision(t1, t2):
"""Input: 2 coordinate tuples.
Return: True if the coordinates are sufficiently close
False otherwise
"""
if (abs(t1[0] - t2[0]) < 25) and (abs(t1[1] - t2[1]) < 25):
return True
return False
def periodicTimer():
"""Timer function which is called periodically by Python.
This function implements the game logic:
- creating all objects
- arranging objects on the screen
- calling object's move() methods
- checking for collisions
- checking for invalid moves
- detection program termination conditions (all food eaten, ...)
- initiate termination
"""
global phase
global isQuit
global food
global score # doboggi_man's achieved score
turtle.tracer(0, 0) # disable screen updates
for g in ghosts:
g.updateShape() # make ghost change its shape
g.move() # move ghost
if phase % 4 == 0:
# pm shape is updated every 4th iteration.
pm.updateShape() # make doboggi_man change its shape
p_old = pm.getPosition()
pm.move() # move doboggi_man
p_new = pm.getPosition()
# Check for illegal moves:
if (abs(p_old[0] - p_new[0]) > 12) or (abs(p_old[1] - p_new[1]) > 12):
print('Illegal move, game terminated.')
isQuit = True
turtle.update() # force turtle module's screen update
turtle.tracer(1, 10) # re-enable periodic screen updates
pm.decrementSteps() # doboggi_man made one step, decrement counter
if pm.getRemainingSteps() == 0:
print('You ran out of steps!')
isQuit = True
# check collisions pm versus ghosts:
pm_pos = pm.getPosition()
for g in ghosts:
if checkCollision(pm_pos, g.getPosition()):
isQuit = True
print('You bumped into a ghost!')
# check collisions pm versus food:
eaten_dishes = []
for dish in food:
if checkCollision(pm_pos, dish.getPosition()):
pm.setIsYum()
dish.setIsEaten()
eaten_dishes.append(dish)
score += 1
# Remove eaten dishes (cannot remove while iterating!):
for dish in eaten_dishes:
food.remove(dish)
# Check if all food has been eaten already:
if len(food) == 0:
print('Congratulation, all food collected.')
isQuit = True
if phase == 7:
phase = 0
else:
phase += 1
if isQuit:
# Game will terminate, put the "Game Over" image:
game_over.setposition(0, -HEIGHT // 2 + 22)
game_over.shape('img/game_over.gif')
game_over.showturtle()
# Trigger the shutdown handler function to be called in MS_TO_QUIT ms
# from now:
window.ontimer(shutDownHander, MS_TO_QUIT)
else:
# Trigger the next firing of our timer function, in 90ms from now:
window.ontimer(periodicTimer, 90)
def RightKeyHandler():
"""Handler function for key-right events."""
pm.turnEast()
def LeftKeyHandler():
"""Handler function for key-left events."""
pm.turnWest()
def UpKeyHandler():
"""Handler function for key-up events."""
pm.turnNorth()
def DownKeyHandler():
"""Handler function for key-down events."""
pm.turnSouth()
def quitKeyHandler():
"""Handler function for the 'q' key."""
global isQuit
isQuit = True
def placeFood():
"""
Compute doboggi screen positions and instantiate doboggi objects.
Returns: a list of doboggi objects.
"""
food = []
Upper = True
for i in range(0, FOODMAX):
ok = False
while not ok: # loop until proper position was computed:
r_x = random.randrange(-X_MAX + 20, X_MAX - 20)
if Upper:
# Position above the ghosts:
r_y = random.randrange(160, Y_MAX)
else:
# Position below the ghosts:
r_y = random.randrange(-Y_MAX, -40)
new_pos = (r_x, r_y)
HaveCollision = False
for i in food:
HaveCollision = HaveCollision \
or checkCollision(new_pos, i.getPosition())
if not HaveCollision:
food.append(doboggi(r_x, r_y))
ok = True
# Toggle between ``above'' and ``below" ghost's screen part:
Upper = not Upper
return food
#################
# Main program: #
#################
isQuit = False # Set to true to initiate game termination.
score = 0
turtle.setup(WIDTH, HEIGHT)
window = turtle.Screen()
window.title('Doboggi-Man')
window.bgcolor('black')
turtle.register_shape('img/ghost_phase_0.gif')
turtle.register_shape('img/ghost_phase_1.gif')
turtle.register_shape('img/dbm_north_phase_0.gif')
turtle.register_shape('img/dbm_north_phase_1.gif')
turtle.register_shape('img/dbm_south_phase_0.gif')
turtle.register_shape('img/dbm_south_phase_1.gif')
turtle.register_shape('img/dbm_east_phase_0.gif')
turtle.register_shape('img/dbm_east_phase_1.gif')
turtle.register_shape('img/dbm_west_phase_0.gif')
turtle.register_shape('img/dbm_west_phase_1.gif')
turtle.register_shape('img/doboggi.gif')
turtle.register_shape('img/yum.gif')
turtle.register_shape('img/game_over.gif')
#
# Instantiate ghost objects:
#
ghosts.append(ghost('inky'))
ghosts.append(ghost('pinky', 120, 120))
#
# Comment-out the following line and uncomment the next line to
# use your auto-doboggi_man:
#
# pm = doboggi_man(120, -40) # Instantiate doboggi_man object
pm = auto_doboggi_man(120, -40) # Instantiate your auto_doboggi_man object
#
#
#
food += placeFood()
# Prepare the "game over" turtle already:
game_over = turtle.Turtle()
game_over.hideturtle()
game_over.speed('fastest')
phase = 0
# Install the keyboard handlers:
window.onkey(RightKeyHandler, 'Right')
window.onkey(LeftKeyHandler, 'Left')
window.onkey(UpKeyHandler, 'Up')
window.onkey(DownKeyHandler, 'Down')
window.onkey(quitKeyHandler, 'q')
# Call periodic timer function for the first time.
# Subsequent calls will be triggered from inside
# this function, by setting up a timer:
periodicTimer()
window.listen()
window.mainloop()