-
Notifications
You must be signed in to change notification settings - Fork 0
/
saved code
executable file
·477 lines (440 loc) · 15.7 KB
/
saved code
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
__________________________________________________________________________
map1 = """
wwwww www www ww ww ww
w w w
w w
w wwwwww wwwww www
www w w w
w w w w
w wwwww wwwwwww ww
w wwwwww w w w
w w w www wwww w
w w w w w
w wwwwww www wwww w w
w w ww w w
w w
wwwwwwwwwwwwwwwwwwwwwwwwwwwww"""
map1 = map1.splitlines()
for y, line in enumerate(map1):
for x, c in enumerate(line):
if c == "w":
screen.blit(tile, (x * 32, y * 32))
__________________________________________________________________________
if not reached and targetX >= 0 and targetY >= 0:
radians = math.atan2(targetY - playerY, targetX - playerX)
dx = math.cos(radians)
dy = math.sin(radians)
bgXChange = dx * 2
bgYChange = dy * 2
bgY -= bgYChange
bgX -= bgXChange
if bgX >= 0 or bgY >= 0:
playerX += bgXChange
playerY += bgYChange
else:
targetX -= bgXChange
targetY -= bgYChange
__________________________________________________________________________
if playerX > 515 and playerY > 240:
bgY -= bgYChange
bgX -= bgXChange
targetX -= bgXChange
targetY -= bgYChange
else:
playerX += bgXChange
playerY += bgYChange
print("Called")
__________________________________________________________________________
if bgX <= 0 and bgY <= 0:
bgX -= bgXChange
bgY -= bgYChange
targetX -= bgXChange
targetY -= bgYChange
print("Done")
else:
playerX += bgXChange
playerY += bgYChange
__________________________________________________________________________
bgX -= bgXChange
bgY -= bgYChange
if bgX < 0:
targetX -= bgXChange
if bgY < 0:
targetY -= bgYChange
if bgX >= 0 or bgY >= 0:
playerX += bgXChange
playerY += bgYChange
elif playerX < 390:
bgX += bgXChange
playerX += bgXChange
targetX += bgXChange
elif playerY < 270:
bgY += bgYChange
playerY += bgYChange
targetY += bgYChange
__________________________________________________________________________
bgX -= bgXChange
bgY -= bgYChange
if bgX < 0:
targetX -= bgXChange
if bgY < 0:
targetY -= bgYChange
if bgX >= 0 or bgY >= 0:
playerX += bgXChange
playerY += bgYChange
elif playerX < 390:
bgX += bgXChange
playerX += bgXChange
targetX += bgXChange
elif playerY < 200:
bgY += bgXChange
playerY += bgXChange
targetY += bgXChange
elif playerX > 570:
bgX += bgXChange
playerX += bgXChange
targetX += bgXChange
elif playerY > 340:
bgY += bgXChange
playerY += bgXChange
targetY += bgXChange
__________________________________________________________________________
if playerX + abs(bgX) <= spawnX + 5 and playerY + abs(bgY) <= spawnY + 5:
playerX += bgXChange
playerY += bgYChange
print(1)
elif playerX + abs(bgX) <= spawnX + 5:
playerX += bgXChange
bgY -= bgYChange
targetY -= bgYChange
print(2)
elif playerY + abs(bgY) <= spawnY + 5:
playerY += bgYChange
bgX -= bgXChange
targetX -= bgXChange
print(3)
else:
bgX -= bgXChange
bgY -= bgYChange
targetX -= bgXChange
targetY -= bgYChange
print(4)
__________________________________________________________________________
# Imports
import pygame
import math
import os
from pygame import mixer
from shapely.geometry import LineString
from shapely.geometry import Point
# Init
pygame.init()
os.environ["SDL_VIDEO_CENTERED"] = "1"
boundsX = 960
boundsY = 540
clock = pygame.time.Clock()
screen = pygame.display.set_mode((boundsX, boundsY))
pygame.display.set_caption("Lighthouse Keeper and the Shadow Pirates")
icon = pygame.image.load("LightHouse/Sprites/icon.png")
pygame.display.set_icon(icon)
# Images
gamebg = pygame.image.load("LightHouse/Backgrounds/sea_background1.png")
lighthouse = pygame.image.load("LightHouse/Lighthouse/0.png")
player_down_left = pygame.image.load("LightHouse/Player/ship_down_left.png")
player_down_right = pygame.image.load("LightHouse/Player/ship_down_right.png")
player_up_left = pygame.image.load("LightHouse/Player/ship_up_left.png")
player_up_right = pygame.image.load("LightHouse/Player/ship_up_right.png")
player = player_down_right
title = pygame.image.load("LightHouse/Sprites/title.png")
gameover = pygame.image.load("LightHouse/Sprites/gameover.png")
menubg = pygame.image.load("LightHouse/Backgrounds/menubg.png")
settingsbg = pygame.image.load("LightHouse/Backgrounds/settingsbg.png")
gameoverbg = pygame.image.load("LightHouse/Backgrounds/gameoverbg.png")
pausebg = pygame.image.load("LightHouse/Backgrounds/pausebg.png")
button = pygame.image.load("LightHouse/Sprites/0.png")
buttonsel = pygame.image.load("LightHouse/Sprites/1.png")
arrow = pygame.image.load("LightHouse/Player/arrow.png")
ball = pygame.image.load("LightHouse/Sprites/target.png")
invalidball = pygame.image.load("LightHouse/Sprites/invalidtarget.png")
tile = pygame.image.load("LightHouse/Sprites/10.png")
# Music and sounds
bgmusic = mixer.music.load("LightHouse/Music/menumusic.wav") # using music and not sound
mixer.music.play(-1)
click = mixer.Sound("LightHouse/Music/click.wav")
# Variables
bgwidth = 6704
bgheight = 3944
targetX = -1
targetY = -1
mouseX = 0
mouseY = 0
lightX = 225
lightY = 218
spawnX = 460
spawnY = 250
playerX = spawnX
playerY = spawnY
pointX = 0
pointY = 0
bgX = -2875
bgY = -1575
m = False
s = False
gamerunning = True
menurunning = True
settingsrunning = True
reached = False
last = 0
dec = False
inc = True
bg = 0
dx = 0
dy = 0
bgXCopy = 0
bgYCopy = 0
uplimit = False
downlimit = False
edgeleft = False
edgeright = False
def blitMenuButtons(x, y):
if 407 < x < 576 and 248 < y < 312:
screen.blit(buttonsel, (407, 248))
else:
screen.blit(button, (407, 248))
if 407 < x < 576 and 323 < y < 390:
screen.blit(buttonsel, (407, 323))
else:
screen.blit(button, (407, 323))
screen.blit(title, (262, 28))
def showMenuText():
font = pygame.font.Font("LightHouse/Fonts/slkscr.ttf", 32)
play = font.render("Play", True, (255, 255, 255))
settings = font.render("Settings", True, (255, 255, 255))
screen.blit(play, (445, 260))
screen.blit(settings, (410, 332))
def blitSettingsButtons(x, y):
if 406 < x < 575 and 209 < y < 259:
screen.blit(buttonsel, (406, 209))
else:
screen.blit(button, (406, 209))
if 406 < x < 575 and 267 < y < 317:
screen.blit(buttonsel, (406, 267))
else:
screen.blit(button, (406, 267))
if 406 < x < 575 and 473 < y < 523:
screen.blit(buttonsel, (406, 473))
else:
screen.blit(button, (406, 473))
def showSettingsText():
font = pygame.font.Font("LightHouse/Fonts/slkscr.ttf", 32)
if m:
music = font.render("Music", True, (63, 51, 51))
else:
music = font.render("Music", True, (255, 255, 255))
if s:
soundfx = font.render("Soundfx", True, (63, 51, 51))
else:
soundfx = font.render("Soundfx", True, (255, 255, 255))
settings = font.render("Settings", True, (255, 255, 255))
back = font.render("Back", True, (255, 255, 255))
screen.blit(music, (435, 220))
screen.blit(soundfx, (410, 280))
screen.blit(settings, (410, 50))
screen.blit(back, (448, 483))
def rotateShip():
global player, last, dec, inc
if last > 10:
dec = True
inc = False
elif last < -5:
inc = True
dec = False
if inc:
last += 0.3
elif dec:
last -= 0.3
player = pygame.transform.rotate(player, last)
return player
def showMenu():
global menurunning, targetX, targetY, gamerunning, settingsrunning
while menurunning:
screen.blit(menubg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
menurunning = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
menurunning = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1 and 407 < targetX < 576 and 248 < targetY < 312:
gamerunning = True
targetX = -1
targetY = -1
showGame()
if event.button == 1 and 407 < targetX < 576 and 323 < targetY < 390:
settingsrunning = True
showSettings()
targetX, targetY = pygame.mouse.get_pos()
blitMenuButtons(targetX, targetY)
showMenuText()
pygame.display.update()
def redrawGameWindow():
global player, reached, last
# Direction
if math.sqrt(((targetX - playerX) ** 2) + ((targetY - playerY) ** 2)) < 10:
if targetX < playerX:
player = player_down_left
else:
player = player_down_right
reached = True
rotateShip()
else:
reached = False
last = 0
if targetX > playerX and targetY < playerY:
player = player_up_right
elif targetX < playerX and targetY < playerY:
player = player_up_left
elif targetX > playerX and targetY > playerY:
player = player_down_right
elif targetX < playerX and targetY > playerY:
player = player_down_left
blitArrow()
screen.blit(player, (int(playerX), int(playerY)))
clock.tick(60)
pygame.display.update()
def blitArrow():
global pointX, pointY
# Finding point
point = Point(int(playerX + 12), int(playerY + 15))
circle = point.buffer(20).boundary
line = LineString([(mouseX, mouseY), (int(playerX + 12), int(playerY + 15))])
intersect = circle.intersection(line)
try:
pointX = intersect.x
pointY = intersect.y
except AttributeError:
pass
# Rotation
arrow_copy = arrow.copy()
rel_x, rel_y = mouseX - pointX, mouseY - pointY
angle = (180 / math.pi) * -math.atan2(rel_y, rel_x)
arrow_copy = pygame.transform.rotate(arrow_copy, angle)
if angle > 90:
angle = 180 - angle
rad = angle * math.pi / 180
perp = abs(41 * math.sin(rad))
base = abs(41 * math.cos(rad))
if mouseX < pointX and mouseY < pointY:
screen.blit(arrow_copy, (int(pointX - base), int(pointY - perp)))
elif mouseX < pointX and mouseY > pointY:
screen.blit(arrow_copy, (int(pointX - base), int(pointY)))
elif mouseX > pointX and mouseY < pointY:
screen.blit(arrow_copy, (int(pointX), int(pointY - perp)))
else:
screen.blit(arrow_copy, (int(pointX), int(pointY)))
def checkBounds():
global playerX, bgX, bgY, targetX, targetY, bgXCopy, bgYCopy, uplimit, downlimit, edgeleft, edgeright
# Background Bounds
if bgY > 0:
bgY = 0
uplimit = True
if bgY < -(bgheight - boundsY):
bgY = -(bgheight - boundsY)
downlimit = True
# Player Bounds
distance = math.sqrt(((2875 - abs(bgX)) ** 2) + ((1575 - abs(bgY)) ** 2))
print(distance)
if distance >= 2500:
bgX = bgXCopy
bgY = bgYCopy
print("Out of bounds")
if abs(bgX) > 2875:
edgeright = True
else:
edgeleft = True
else:
bgXCopy = bgX
bgYCopy = bgY
moveShip()
def moveShip():
global reached, targetX, targetY, bgX, bgY, dx, dy, uplimit, downlimit, edgeleft, edgeright
if not reached and targetX >= 0 and targetY >= 0 and not (edgeleft or edgeright):
radians = math.atan2(targetY - playerY, targetX - playerX)
dx = math.cos(radians)
dy = math.sin(radians)
bgXChange = dx * 10
bgYChange = dy * 10
if not (uplimit or downlimit or edgeleft or edgeright):
bgX -= bgXChange
bgY -= bgYChange
targetX -= bgXChange
targetY -= bgYChange
screen.blit(ball, (int(targetX) - 13, int(targetY) - 12))
else:
screen.blit(invalidball, (int(targetX) - 13, int(targetY) - 12))
if uplimit and targetY - 12 > playerY:
uplimit = False
if downlimit and targetY - 12 < playerY:
downlimit = False
if edgeleft and targetX - 13 > playerX:
edgeleft = False
if edgeright and targetX - 13 < playerX:
edgeright = False
def showGame():
global gamerunning, menurunning, settingsrunning, reached, lightX, lightY, playerX, playerY, player, last, targetX, targetY, pointX, pointY, bg,\
boundsX, boundsY, bgX, bgY, mouseX, mouseY
while gamerunning:
screen.blit(gamebg, (int(bgX), int(bgY)))
for event in pygame.event.get():
if event.type == pygame.QUIT:
gamerunning = False
menurunning = False
settingsrunning = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
gamerunning = False
menurunning = False
settingsrunning = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
targetX, targetY = pygame.mouse.get_pos()
mouseX, mouseY = pygame.mouse.get_pos()
# moveShip()
checkBounds()
redrawGameWindow()
def showSettings():
global settingsrunning, menurunning, gamerunning, targetX, targetY, s, m
while settingsrunning:
screen.blit(settingsbg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
settingsrunning = False
menurunning = False
gamerunning = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
settingsrunning = False
menurunning = False
gamerunning = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1 and 406 < targetX < 575 and 209 < targetY < 259:
if m:
m = False
pygame.mixer.music.unpause()
else:
m = True
pygame.mixer.music.pause()
if event.button == 1 and 406 < targetX < 575 and 267 < targetY < 317:
if s:
s = False
click.play()
else:
s = True
if event.button == 1 and 406 < targetX < 575 and 473 < targetY < 523:
settingsrunning = False
targetX, targetY = pygame.mouse.get_pos()
blitSettingsButtons(targetX, targetY)
showSettingsText()
pygame.display.update()
showMenu()