-
Notifications
You must be signed in to change notification settings - Fork 1
/
AngryPythons.pyw
320 lines (242 loc) · 9.38 KB
/
AngryPythons.pyw
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
from graphics import *
import physics
import shapeGen
# Below are some constants that are globabaly visible to make the code simpler
# Note that in Python, a tuple is a list that cannot be modified
# Tuple of the description text for each menu option
MENU_STRINGS = (
"Number of targets",
"Number of obstacles",
"Ammo",
"Ticks per second",
"Force multiplier",
"Surface friction",
"Projectile elasticity",
"Gravity",
"Min obstacle width",
"Max obstacle width",
"Min obstacle length",
"Max obstacle length")
# Tuple of default values for each menu option
MENU_DEFAULTS = (
"3", "10", "10",
"90", "10", "0.15", "0.5", "9.8",
"10", "30", "70", "300")
# Number of columns in the menu (must be even)
MENU_COLUMN_NUMBER = 4
# Program entry point, opens window and displays options menu
def main(width, height):
win = GraphWin("PyBirds", width, height, autoflush=False)
menuInputs = intialiseMenuInputs()
while True:
menuInputs = menu(win, menuInputs)
# Run the game with the specified options
def play(win, options):
# Load options from menu
targetNumber = options[0]
obstacleNumber = options[1]
ammo = options[2]
physicsConstants = options[3:8]
obstacleDimensionRanges = options[8:12]
# Coordinate of top of catapult
topOfCatapult = Point(win.getWidth() / 10, win.getHeight() * 4 / 5)
# Draw constants and UI
drawBackdrop(win)
drawScenery(win, topOfCatapult)
menuButton = drawButton(win, [0, 20, 0, 80], "red", "Menu", "gold")
ammoDisplay = Text(Point(100, 10), ammo)
ammoDisplay.draw(win)
# Generate level
obstacles = shapeGen.genRandomObstacles(win, obstacleNumber, obstacleDimensionRanges)
targets = shapeGen.genRandomTargets(win, targetNumber, obstacles)
# Uncomment below for hard-coded level layout
#obstacles = shapeGen.setObstacles(win)
#targets = shapeGen.setTargets(win)
won = False
lost = False
# Game loop
while not won and not lost:
# Listen for user interaction
clickPos = getUserInput(win)
# Check if user clicked the menu button
if mouseOverrectangle(clickPos, menuButton):
return # End game and return to menu
# Update ammo
ammo = ammo - 1
ammoDisplay.setText(ammo)
drawCrosshair(win,clickPos)
physics.simulateProjectile(win, topOfCatapult, clickPos, obstacles, targets, physicsConstants)
# win when no targets are left
won = len(targets) == 0
# lose when ammo runs out
lost = ammo == 0
if won:
showMessage(win, "You win", "darkgreen", "yellow")
elif lost:
showMessage(win, "Out of ammo", "red", "orange")
# Clean up these objects, then return to the menu
undrawAll(targets)
undrawAll(obstacles)
return
# Wait for the user to click in a valid firing position
def getUserInput(win):
clickPos = win.getMouse()
# Ignore clicks that are out of bounds (right of catapult)
while clickPos.getX() > win.getWidth() / 10:
clickPos = win.getMouse()
return clickPos
def undrawAll(shapes):
for shape in shapes:
shape.undraw()
################################################################################
# Code for Options Menu
################################################################################
# Draws the options menu
def menu(win, menuInputs):
# Draw the background (i.e. clear the screen)
drawBackdrop(win)
# Create and draw the Play and Defaults buttons
playButton = drawButton(win, [0, 20, 0, 80], "darkgreen", "Play", "gold")
defaultsButton = drawButton(win, [0, 20, 80, 160], "gold", "Defaults", "red")
# Draw all the menu options input fields
for item in menuInputs:
item.draw(win)
# Draw the labels for each menu option
drawMenuItemLabels(win)
# Listen for click of Play or Defaults button by checking the mouse position
# each time the user clicks until it's within one of the buttons
clickPos = win.getMouse()
while not mouseOverrectangle(clickPos, playButton):
# Check if they clicked the Defaults button
if mouseOverrectangle(clickPos, defaultsButton):
# Redraw menu with default values
undrawAll(menuInputs)
menuInputs = intialiseMenuInputs()
for item in menuInputs:
item.draw(win)
# Wait for the next click
clickPos = win.getMouse()
# => play button has been clicked
# Validate menu option inputs, if any are invalid, returns defaults
options = []
for item in menuInputs:
try:
float(item.getText()) # Assumes all menu options are numeric
except ValueError:
for menuItem in menuInputs: # undraw menu
menuItem.undraw()
return menuInputs # return default values
# => value is valid
options.append(eval(item.getText())) # Accept value
item.undraw() # Remove option from menu
play(win, options)
return menuInputs # Return new values
# Determines whether a point is inside a rectangle
def mouseOverrectangle(mousePosition, rectangle):
top, bottom, left, right = physics.determineRectangleBounds(rectangle)
if mousePosition.getX() >= left \
and mousePosition.getX() <= right \
and mousePosition.getY() >= top \
and mousePosition.getY() <= bottom:
return True
else:
return False
################################################################################
# Code for drawing Options Menu items
################################################################################
# Draw the text label for each menu option
def drawMenuItemLabels(win):
for i in range(len(MENU_STRINGS)):
# Calculate position
x = i % MENU_COLUMN_NUMBER * 200 + 100
y = i // MENU_COLUMN_NUMBER * 80 + 50
# Draw label
Text(Point(x, y), MENU_STRINGS[i]).draw(win)
# Generates a list of interface components for modifying game settings
# using the default values provided
def intialiseMenuInputs():
# Create an empty list
menuInputs = []
# Create and add an input for each menu item
for i in range (len(MENU_DEFAULTS)):
# Calculate Position
x = i % MENU_COLUMN_NUMBER * 200 + 100
y = i // MENU_COLUMN_NUMBER * 80 + 80
# Create input using position and size
tb = Entry(Point(x, y), 10)
# Set value to default
tb.setText(MENU_DEFAULTS[i])
tb.setFill("white")
# Add to input list so we can find it later
menuInputs.append(tb)
return menuInputs
################################################################################
# Code for drawing
################################################################################
# Drawns an X at Point pos
def drawCrosshair(win, pos):
crosshair = Text(pos, "X")
crosshair.setSize(10)
crosshair.setTextColor("blue")
crosshair.draw(win)
# Draws a button with a solid fill colour
def drawButton(win, bounds, fillColour, text, textColour):
top, bottom, left, right = bounds
button = Rectangle(Point(left, top), Point(right, bottom))
button.setFill(fillColour)
button.draw(win)
buttonText = Text(Point((left + right) / 2, (top + bottom) / 2), text)
buttonText.setTextColor(textColour)
buttonText.draw(win)
return button
# Displays message in the middle of the screen with a coloured backdrop
def showMessage(win, message, textColour, bgColour):
# Position
center = Point(win.getWidth() / 2, win.getHeight() / 2)
topLeft = Point(center.getX()-150, center.getY()-40)
bottomRight = Point(center.getX() + 150, center.getY() + 40)
bg = Rectangle (topLeft, bottomRight)
txt = Text(center, message)
# Set display properties
txt.setSize(20)
bg.setFill(bgColour)
txt.setFill(textColour)
# Display and wait till clicked
bg.draw(win)
txt.draw(win)
win.getMouse()
# Fills the screen with a solid colour
def drawBackdrop(win):
topLeft = Point(0, 0)
bottomRight = Point(win.getWidth(), win.getHeight())
backdrop = Rectangle(topLeft, bottomRight)
backdrop.setFill("#D3D3D3")
backdrop.draw(win)
# Draws the game's static scenary
def drawScenery(win, topOfCatapult):
# Draw power rings
outerRingRadius = win.getWidth() / 8
# Draw a red ring at full size
redRing = Circle(topOfCatapult, outerRingRadius)
redRing.setFill("red")
redRing.draw(win)
# Draw a yellow ring on top, 2/3 the size of the red ring
yellowRing = Circle(topOfCatapult, outerRingRadius * 2 / 3)
yellowRing.setFill("yellow")
yellowRing.draw(win)
# Draw a green ring on top, 1/3 the size of the red ring
greenRing = Circle(topOfCatapult, outerRingRadius / 3)
greenRing.setFill("green")
greenRing.draw(win)
# Draw catapult
catapultBottomPos = Point(topOfCatapult.getX() - 5, win.getHeight())
catapult = Rectangle(topOfCatapult, catapultBottomPos)
catapult.setFill("brown")
catapult.draw(win)
# Draw the sky
skyTopLeft = Point(win.getWidth() / 10, 0)
skyBottomRight = Point(win.getWidth(), win.getHeight() + 10)
sky = Rectangle(skyTopLeft, skyBottomRight)
sky.setFill("lightblue")
sky.draw(win)
main(1200, 500)