forked from adamjhawley/HedgeMazeGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze V0.7.7.py
322 lines (281 loc) · 9.73 KB
/
Maze V0.7.7.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
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
321
322
#-------------------------------------------------------------------------------
# Name: Hedge BEV Game
# Purpose:
#
# Author: ahawley
#
# Created: 08/01/2014
# Copyright: (c) ahawley 2014
# Licence: <your licence>
#-------------------------------------------------------------------------------
import pygame, time, sys
from pygame.locals import *
import random
#Player start cords
x=0
y=0
#Amount of heges in level 1
nofhedges = 50
lvl = 0
FPS = 15
WINDOWWIDTH = 800
WINDOWHEIGHT = 600
CELLSIZE = 20
assert WINDOWWIDTH % CELLSIZE == 0
assert WINDOWHEIGHT % CELLSIZE == 0
CELLWIDTH = int(WINDOWWIDTH / CELLSIZE)
CELLHEIGHT = int(WINDOWHEIGHT / CELLSIZE)
COLLISIONMESSAGE = "Woops! You just walked into a hedge; you're stuck! Level Restarting..."
WINMESSAGE = "You did it; you won! Congrats!"
# R G B
WHITE = (255, 255, 255)
BLACK = ( 0, 0, 0)
RED = (255, 0, 0)
DARKPINK = (255, 20, 147)
GREEN = ( 0, 255, 0)
DARKGREEN = ( 0, 155, 0)
ORANGE = (255, 153, 18)
DARKGRAY = ( 40, 40, 40)
YELLOW = (255, 255, 0)
BLUE = ( 0, 0, 255)
KHAKI = (139, 134, 78)
BGCOLOR = BLACK
UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'
def main():
pygame.display.init()
global FPSCLOCK, wn, BASICFONT, level
level = 1
pygame.init()
FPSCLOCK = pygame.time.Clock()
wn = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
BASICFONT = pygame.font.SysFont('ActionIsShaded', 24)
pygame.display.set_caption('Hedge Maze')
## print("Pre-show start screen: success")
showStartScreen()
runGame()
def runGame():
print("Key Pressed")
clearScreen()
levelStartScreen()
time.sleep(5)
loadlevel(nofhedges, lvl)
def showStartScreen():
global titleFont, instFont
titleFont = pygame.font.SysFont('ActionIsShaded', 100)
instFont = pygame.font.SysFont('ActionIsShaded', 20)
titleSurf1 = titleFont.render('Hedge Maze', True, WHITE, RED)
degrees1 = 0
## print("Pre-loop show start screen: success")
while True:
wn.fill(BGCOLOR)
rotatedSurf1 = pygame.transform.rotate(titleSurf1, degrees1)
rotatedRect1 = rotatedSurf1.get_rect()
rotatedRect1.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)
wn.blit(rotatedSurf1, rotatedRect1)
drawPressKeyMsg()
if checkForKeyPress():
pygame.event.get()
return
pygame.display.update()
degrees1 += 1
## print("End of showstartscreen loop: success")
time.sleep(0.02)
def levelStartScreen():
levelNameSurf1 = titleFont.render('Loading', True, RED, BLACK)
levelNameRect1 = levelNameSurf1.get_rect()
levelNameRect1.center = (WINDOWWIDTH / 2, WINDOWHEIGHT * 0.25)
levelDescr = instFont.render('Use the arrow keys to navigate your way to the golden square(watch out for the hedges)!', True, BLUE, BLACK)
levelDescrRect = levelDescr.get_rect()
levelDescrRect.center = (WINDOWWIDTH/2, WINDOWHEIGHT*0.75)
wn.blit(levelDescr, levelDescrRect)
wn.blit(levelNameSurf1, levelNameRect1)
pygame.display.update()
def drawPressKeyMsg():
pressKeySurf = BASICFONT.render('Press a key to play.', True, DARKGRAY)
pressKeyRect = pressKeySurf.get_rect()
pressKeyRect.topleft = (WINDOWWIDTH - 200, WINDOWHEIGHT - 30)
wn.blit(pressKeySurf, pressKeyRect)
def endScreen():
scoreMessage = titleFont.render('Score: '+ str((time.clock()-15)), True, BLUE, BLACK)
scoreMessageRect = scoreMessage.get_rect()
wn.blit(scoreMessage, scoreMessageRect)
pygame.display.update()
time.sleep(3)
terminate()
def terminate():
pygame.quit()
def checkForKeyPress():
if len(pygame.event.get(QUIT)) > 0:
terminate()
keyUpEvents = pygame.event.get(KEYUP)
if len(keyUpEvents) == 0:
return None
if keyUpEvents[0].key == K_ESCAPE:
terminate()
return keyUpEvents[0].key
def drawGrid():
global vertline
for x in range(0, WINDOWWIDTH, CELLSIZE): # draw vertical lines
pygame.draw.line(wn, WHITE, (x, 0), (x, WINDOWHEIGHT))
for y in range(0, WINDOWHEIGHT, CELLSIZE): # draw horizontal lines
vertline = pygame.draw.line(wn, WHITE, (0, y), (WINDOWWIDTH, y))
pygame.display.update()
def clearScreen():
wn.fill(BGCOLOR)
pygame.display.update()
#Classes - Hedge, Player & Path
class hedgeTexture:
def __init__(self):
self.y = 0
self.x = 0
def loadTexture(self):
self.texture = pygame.image.load("hedge texture.gif")
self.texturerect = self.texture.get_rect()
self.coords = (random.randrange(CELLSIZE, WINDOWWIDTH, CELLSIZE), random.randrange(CELLSIZE, WINDOWHEIGHT, CELLSIZE))
self.texturerect.move_ip(self.coords)
wn.blit(self.texture, self.texturerect)
pygame.display.flip()
def loadTextureRow(self, x, y, r):
while x<r:
self.texture = pygame.image.load("hedge texture.gif")
self.texturerect = self.texture.get_rect()
self.coords = (x, y)
self.texturerect.move_ip(self.coords)
wn.blit(self.texture, self.texturerect)
x += CELLSIZE
def loadTextureCol(self, x, y, r):
while y<r:
self.texture = pygame.image.load("hedge texture.gif")
self.texturerect = self.texture.get_rect()
self.hedgecoords = (x, y)
self.texturerect.move_ip(self.coords)
wn.blit(self.texture, self.texturerect)
y += CELLSIZE
class playerTexture:
def __init__(self):
self.y = 10
self.x = 10
def loadplayer(self, x, y):
self.texture = pygame.image.load("You texture.gif")
self.texturerect = self.texture.get_rect()
self.coords = (x, y)
self.texturerect.move_ip(self.coords)
wn.blit(self.texture, self.texturerect)
pygame.display.flip()
class pathTexture:
def __init__(self):
self.y = 0
self.x = 0
def loadTexture(self, x, y):
self.texture = pygame.image.load("Path Texture.gif")
self.texturerect = self.texture.get_rect()
self.coords = (x, y)
self.texturerect.move_ip(self.coords)
wn.blit(self.texture, self.texturerect)
pygame.display.flip()
class goalTexture:
def __init__(self):
self.y = 0
self.x = 0
def loadTexture(self):
self.texture = pygame.image.load("Goal Texture.gif")
self.texturerect = self.texture.get_rect()
self.coords = (((WINDOWWIDTH/2)-CELLSIZE), ((WINDOWHEIGHT/2)-CELLSIZE))
self.texturerect.move_ip(self.coords)
wn.blit(self.texture, self.texturerect)
pygame.display.flip()
#Level Create
def loadlevel(nofhedges, lvl):
CurrentTime = 0
count = 0
hedges = 0
global hedge1, hedgecoords, goalCoords, goal1
clearScreen()
#Level Check - Ie. If they still have another turn.
if lvl == 5:
endScreen()
drawGrid()
player1 = playerTexture()
player1.loadplayer(x, y)
goals = 0
goalCoords = []
hedgecoords = []
while hedges<nofhedges:
hedge1 = hedgeTexture()
hedge1.loadTexture()
hedgecoords.insert(0, hedge1.coords)
if ((WINDOWWIDTH/2)-(0.5*CELLSIZE), ((WINDOWHEIGHT/2)-(0.5*CELLSIZE))) in hedgecoords:
hedges-1
elif ((WINDOWWIDTH/2)+(0.5*CELLSIZE), ((WINDOWHEIGHT/2)+(0.5*CELLSIZE))):
hedges-1
elif ((WINDOWWIDTH/2)-(0.5*CELLSIZE), ((WINDOWHEIGHT/2)+(0.5*CELLSIZE))):
hedges-1
elif ((WINDOWWIDTH/2)+(0.5*CELLSIZE), ((WINDOWHEIGHT/2)-(0.5*CELLSIZE))):
hedges-1
hedges+=1
count +=1
#Goal Comes Last
while goals<1:
goal1 = goalTexture()
goal1.loadTexture()
goals += 1
goalCoords.insert(0, goal1.coords)
CurrentTime = time.clock()
while True:
direction(x, y, nofhedges, lvl)
def direction(x, y, nofhedges, lvl):
direction = RIGHT
while True: # main game loop
for event in pygame.event.get():
if event.type == QUIT:
terminate()
elif event.type == KEYDOWN:
if (event.key == K_LEFT or event.key == K_a):
direction = LEFT
## print("Left")
checkPosition(x, y, nofhedges, lvl)
if x>CELLSIZE-1:
x-= CELLSIZE
elif (event.key == K_RIGHT or event.key == K_d):
direction = RIGHT
## print("Right")
checkPosition(x, y ,nofhedges, lvl)
if x<(WINDOWWIDTH-(2*CELLSIZE))+1:
x+=CELLSIZE
elif (event.key == K_UP or event.key == K_w):
direction = UP
## print("Up")
checkPosition(x, y ,nofhedges, lvl)
if y>CELLSIZE-1:
y-=CELLSIZE
elif (event.key == K_DOWN or event.key == K_s):
direction = DOWN
## print("Down")
checkPosition(x, y ,nofhedges, lvl)
if y<(WINDOWHEIGHT-(2*CELLSIZE))+1:
y+=CELLSIZE
elif event.key == K_ESCAPE:
terminate()
player1 = playerTexture()
player1.loadplayer(x, y)
def finishLevel(nofhedges, lvl):
## print(WINMESSAGE + "You took " + str(time.clock())+" seconds.")
time.sleep(3)
nofhedges=nofhedges+50
lvl += 1
loadlevel(nofhedges, lvl)
def failLevel(nofhedges, lvl):
print(COLLISIONMESSAGE)
time.sleep(2)
loadlevel(nofhedges, lvl)
def checkPosition(x, y, nofhedges, lvl):
if (x,y) in goalCoords:
finishLevel(nofhedges, lvl)
elif (x,y) in hedgecoords:
failLevel(nofhedges, lvl)
elif x>CELLSIZE-1:
x-=CELLSIZE
main()