-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
315 lines (261 loc) · 9.79 KB
/
main.py
File metadata and controls
315 lines (261 loc) · 9.79 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
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
from cmu_cs3_graphics import *
import pygame
import math
from PIL import Image
from mazeGeneration import maze
from pathfinding import pathSolution
from raycasting import rayCasting
def onAppStart(app):
app.page = 'home'
app.gameMode = 'easy'
app.mazeSize = 3
# player and map setup
app.playerx = 40
app.playery = 40
app.playerAngle = 0
app.mapSize = app.mazeSize*2+3
app.cellSize = app.width/50
app.map = []
# Set starting point and winning location
app.winSurrounding = []
# Finding a winning location that is the farthest
app.end = []
app.solution = []
# Images
# https://www.google.com/url?sa=i&url=https%3A%2F%2Fmedium.com%2F%40jpviergutz007%2F6-ways-to-wake-up-earlier-and-beat-the-alarm-clock-b23b1173af17&psig=AOvVaw3BTymhEy9pFcPxOuODZXlA&ust=1651099455684000&source=images&cd=vfe&ved=0CAwQjRxqFwoTCNDS8bnnsvcCFQAAAAAdAAAAABAJ
app.homeUnedited = Image.open('images/homeUnedited.png')
app.homeUnedited = CMUImage(app.homeUnedited.resize((480,400)))
app.home = Image.open('images/home.png')
app.home = CMUImage(app.home.resize((1280,720)))
app.select = Image.open('images/select.png')
app.select = CMUImage(app.select.resize((1280,720)))
# https://www.webnots.com/download-free-keyboard-key-images-in-black/
app.guide = Image.open('images/guide.png')
app.guide = CMUImage(app.guide.resize((480,270)))
# https://icons8.com/icons/set/settings
app.setting = Image.open('images/setting.png')
app.setting = CMUImage(app.setting.resize((50,50)))
# Raycasting setup
app.FOV= math.pi/2
app.castedRays = 200
app.stepAngle = app.FOV/app.castedRays
app.maxDepth = int(app.width)
app.scale = app.width/app.castedRays
app.moveSpeed = 3
# For mouse and jumping
app.prevx = 0
app.prevy = 0
app.playerAngleY = 0
app.playerWin = False
app.playerHeight = 1
app.playerJump = False
app.showGuide = True
app.showMap = False
app.hint = 0
app.hints = list()
# Sounds
app.stepsPerSecond = 10
app.step = 39
pygame.mixer.init()
# https://djlunatique.com/iphone-alarm-radar-sound-effect/
app.alarm = Sound("sounds/alarm.mp3")
app.endPlay = True
# https://www.myinstants.com/instant/rap-battle-ooohhhh/
app.turnedOff = pygame.mixer.Sound("sounds/ohhh.mp3")
app.startPlay = True
# https://www.fesliyanstudios.com/royalty-free-music/downloads-c/peaceful-and-relaxing-music/22
app.quietTime = pygame.mixer.Sound("sounds/quietTime.mp3")
def mazeSetup(app):
app.mapSize = app.mazeSize*2+3
app.map = maze(app.mazeSize, app.mazeSize)
while len(app.map) == 0:
app.map = maze(app.mazeSize, app.mazeSize)
# Finding a winning location that is the farthest
app.end = findEnd(app)
# In case there is no solution
app.solution = pathSolution(app.map,(1,1),app.end)
if app.solution == None:
mazeSetup(app)
for (r,c) in app.solution:
app.map[r][c] = 5
# Mark the surroundings of the goal location
for (row, col) in app.winSurrounding:
app.map[row][col] = 6
# Initialize end position
app.map[app.end[0]][app.end[1]] = 3
# player starting angle
if app.map[2][1] == 1:
app.playerAngle = 30
# This returns the row and col for the end
def findEnd(app):
directions = [(0,1),(1,0),(-1,0),(0,-1),(1,1),(-1,-1)]
for col in range(app.mapSize-1,-1,-1):
for row in range(app.mapSize-1,-1,-1):
if (app.map[row][col] == 0):
count = 0
app.winSurrounding = []
for (r,c) in directions:
newRow = row + r
newCol = col + c
if (newRow >= 1 and newRow < app.mapSize-1 and newCol >= 1 and newCol < app.mapSize-1):
if app.map[newRow][newCol] == 1:
direction = [(0,1),(1,0),(-1,0),(0,-1)]
if (r,c) in direction:
app.winSurrounding.append((newRow,newCol))
count+=1
if count == 5:
return (row,col)
return None
class Sound(object):
def __init__(self, path):
self.path = path
self.loops = 1
pygame.mixer.music.load(path)
# Loops = number of times to loop the sound.
def start(self, loops=1):
self.loops = loops
pygame.mixer.music.play(loops=loops)
# Stops the current sound from playing
def stop(self):
pygame.mixer.music.stop()
#draw map
def drawMap(app):
for row in range(app.mapSize):
for col in range(app.mapSize):
if (row,col) in app.hints:
drawRect(col*app.cellSize,row*app.cellSize,app.cellSize, app.cellSize, fill='green')
elif app.map[row][col] == 1 or app.map[row][col] == 6:
drawRect(col*app.cellSize,row*app.cellSize,app.cellSize, app.cellSize, fill='lightgrey')
else:
drawRect(col*app.cellSize,row*app.cellSize, app.cellSize,app.cellSize, fill='grey')
# Checks if hit wall
def checkCollision(app, direction):
col = int(app.playerx / app.cellSize)
row = int(app.playery / app.cellSize)
# Undo if hit wall, based on direction
if app.map[row][col] == 1 or app.map[row][col] == 6:
if direction == 'up':
app.playerx -= -math.sin(app.playerAngle) * app.moveSpeed
app.playery -= math.cos(app.playerAngle) * app.moveSpeed
else:
app.playerx += -math.sin(app.playerAngle) * app.moveSpeed
app.playery += math.cos(app.playerAngle) * app.moveSpeed
def onStep(app):
if app.page == 'gaming':
if app.startPlay == False:
pygame.mixer.pause()
app.startPlay = True
app.step += 1
if app.step == 40:
app.alarm.start()
app.step%=40
distance = math.dist((app.playerx,app.playery),(app.end[0]*app.cellSize,app.end[1]*app.cellSize))
volume = (app.mapSize)/(distance) * app.mazeSize
pygame.mixer.music.set_volume(volume)
app.moveSpeed = 3
c = int(app.playerx/app.cellSize)
r = int(app.playery/app.cellSize)
if app.map[r][c] == 3:
app.playerWin = True
app.alarm.stop()
if app.endPlay:
app.turnedOff.play()
app.endPlay = False
# Gives the ability to jump
if app.playerJump:
app.playerHeight *= 1.5
else:
if app.playerHeight > 1:
app.playerHeight *= 0.7
elif app.playerHeight < 1:
app.playerHeight = 1
if app.playerHeight > 100:
app.playerJump = False
else:
if app.startPlay:
app.quietTime.play()
app.startPlay = False
def onMousePress(app,mouseX,mouseY):
if app.page == 'home':
if 525<mouseX<823 and 499<mouseY<564:
app.page = 'select'
elif app.page == 'select':
if 582<mouseX<770 and 400<mouseY<469:
app.gameMode = 'easy'
app.mazeSize = 4
mazeSetup(app)
app.page = 'gaming'
elif 525<mouseX<823 and 498<mouseY<565:
app.gameMode = 'medium'
app.mazeSize = 8
mazeSetup(app)
app.page = 'gaming'
elif 581<mouseX<790 and 591<mouseY<657:
app.gameMode = 'hard'
app.mazeSize = 12
mazeSetup(app)
app.page = 'gaming'
elif app.page == 'gaming':
if 1200<mouseX<1250 and 30<mouseY<72:
app.showGuide = not app.showGuide
if app.playerWin:
if 608<mouseX<757 and 496<mouseY<542:
onAppStart(app)
def onKeyPress(app,key):
if app.page == 'gaming':
app.showGuide = False
if app.playerWin == False:
if key == 'space':
if app.playerHeight == 1:
app.playerJump = True
elif key == 'e':
app.showMap = not app.showMap
elif key == 'h':
if app.hint < len(app.solution):
app.hints.append(app.solution[app.hint])
app.hint+=1
def onKeyHold(app, keys):
if app.page == 'gaming':
if app.playerWin == False:
if 'a' in keys:
app.playerAngle -= 0.1
if 'd' in keys:
app.playerAngle += 0.1
if 'w' in keys:
app.playerx += -math.sin(app.playerAngle) * app.moveSpeed
app.playery += math.cos(app.playerAngle) * app.moveSpeed
checkCollision(app, 'up')
if 's' in keys:
app.playerx -= -math.sin(app.playerAngle) * app.moveSpeed
app.playery -= math.cos(app.playerAngle) * app.moveSpeed
checkCollision(app,'down')
def gaming(app):
#update background: top and bottom
drawRect(0,0,app.width,app.height/2,fill='peru')
drawRect(0,app.height/2,app.width,app.height/2,fill='sienna')
#raycasting
rayCasting(app)
#draw Map
if app.showMap:
drawMap(app)
#draw player on map
drawCircle(app.playerx,app.playery,4,fill='yellow')
def redrawAll(app):
if app.page == 'home':
drawImage(app.home,0,0)
elif app.page == 'select':
drawImage(app.select,0,0)
elif app.page == 'gaming':
gaming(app)
drawImage(app.setting,1200,30)
if app.showGuide:
drawImage(app.guide,app.width/2-200,app.height/2-100)
if app.playerWin:
drawImage(app.homeUnedited,app.width/2-200,app.height/2-200)
if app.playerJump:
drawLabel('Jump won\'t help, you are too short!',app.width/2,app.height/2,
font='orbitron',size=20)
def main():
runApp(width=1280, height=720)
if __name__ == '__main__':
main()