-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
287 lines (248 loc) · 11.6 KB
/
main.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
import pygame as pg
import sys
from os import path
from settings import *
from sprites import *
from tilemap import *
class Game:
def __init__(self):
pg.init()
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption(TITLE)
self.clock = pg.time.Clock()
pg.key.set_repeat(500,100)
self.introduced = False
self.load_data()
def draw_text(self, text, font_name, size, color, x, y, align = "nw"):
font = pg.font.Font(font_name, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
if align == "nw":
text_rect.topleft = (x, y)
if align == "ne":
text_rect.topright = (x, y)
if align == "sw":
text_rect.bottomleft = (x, y)
if align == "se":
text_rect.bottomright = (x, y)
if align == "n":
text_rect.midtop = (x, y)
if align == "s":
text_rect.midbottom = (x, y)
if align == "e":
text_rect.midright = (x, y)
if align == "w":
text_rect.midleft = (x, y)
if align == "center":
text_rect.center = (round(x), round(y))
self.screen.blit(text_surface, text_rect)
def load_data(self):
game_folder = path.dirname(__file__)
img_folder = path.join(game_folder, 'img')
self.pause_font = path.join(game_folder, 'Rixel.otf')
self.dim_screen = pg.Surface(self.screen.get_size()).convert_alpha()
self.dim_screen.fill((0, 0, 0, 100))
self.map = TiledMap(path.join(game_folder, 'map.tmx'))
self.map_img = self.map.make_map()
self.map_rect = self.map_img.get_rect()
self.player_img = pg.image.load(path.join(img_folder, PLAYER_IMG)).convert_alpha()
self.mob_img = pg.image.load(path.join(img_folder, MOB_IMG)).convert_alpha()
# lighting
self.fog = pg.Surface((WIDTH, HEIGHT))
self.fog.fill(NIGHT_COLOR)
self.light_mask_img = pg.image.load(path.join(img_folder, LIGHT_MASK)).convert_alpha()
self.light_mask = pg.transform.scale(self.light_mask_img, LIGHT_RADIUS)
self.light_rect = self.light_mask.get_rect()
self.item_images = {}
for item in ITEM_IMAGES:
self.item_images[item] = pg.image.load(path.join(img_folder, ITEM_IMAGES[item])).convert_alpha()
self.path1 = ['','','','']
self.path2 = ['','','','']
self.path3 = ['','','','']
def new(self):
# init all var and do all the setup for a new game
self.all_sprites = pg.sprite.Group()
self.walls = pg.sprite.Group()
self.mobs = pg.sprite.Group()
self.boats = pg.sprite.Group()
self.items = pg.sprite.Group()
for tile_object in self.map.tmxdata.objects:
if tile_object.name == 'player':
self.player = Player(self,tile_object.x, tile_object.y)
if tile_object.name == 'wall':
Obstacle(self, tile_object.x, tile_object.y, tile_object.width, tile_object.height)
if tile_object.name == 'win':
Boat(self, tile_object.x, tile_object.y, tile_object.width, tile_object.height)
if tile_object.name == 'enemy path 11':
self.path1[0] = (tile_object.x, tile_object.y)
if tile_object.name == 'enemy path 12':
self.path1[1] = (tile_object.x, tile_object.y)
if tile_object.name == 'enemy path 13':
self.path1[2] = (tile_object.x, tile_object.y)
if tile_object.name == 'enemy path 14':
self.path1[3] = (tile_object.x, tile_object.y)
if tile_object.name == 'enemy path 21':
self.path2[0] = (tile_object.x, tile_object.y)
if tile_object.name == 'enemy path 22':
self.path2[1] = (tile_object.x, tile_object.y)
if tile_object.name == 'enemy path 23':
self.path2[2] = (tile_object.x, tile_object.y)
if tile_object.name == 'enemy path 24':
self.path2[3] = (tile_object.x, tile_object.y)
if tile_object.name == 'enemy path 31':
self.path3[0] = (tile_object.x, tile_object.y)
if tile_object.name == 'enemy path 32':
self.path3[1] = (tile_object.x, tile_object.y)
if tile_object.name == 'enemy path 33':
self.path3[2] = (tile_object.x, tile_object.y)
if tile_object.name == 'enemy path 34':
self.path3[3] = (tile_object.x, tile_object.y)
if tile_object.name in ['torch']:
Item(self,(tile_object.x, tile_object.y), tile_object.name)
if tile_object.name in ['feather']:
Item(self,(tile_object.x, tile_object.y), tile_object.name)
for tile_object in self.map.tmxdata.objects:
if tile_object.name == 'enemy 1':
Mob(self, tile_object.x, tile_object.y, self.path1)
# print("Mob1 added")
if tile_object.name == 'enemy 2':
Mob(self, tile_object.x, tile_object.y, self.path2)
# print("Mob2 added")
if tile_object.name == 'enemy 3':
Mob(self, tile_object.x, tile_object.y, self.path3)
# print("Mob3 added")
self.camera = Camera(self.map.width, self.map.height)
self.night = True
self.torch = False
self.paused = False
self.winner = False
self.start = False
self.feather = False
self.itemscreen_torch = False
self.itemscreen_feather = False
def intro(self):
if not self.introduced:
self.start = True
self.introduced = True
else:
print("I feel dizzy...")
def run(self):
# game loop - set self.playing = False to end the game
self.playing = True
self.intro()
while self.playing:
self.dt = self.clock.tick(FPS)/1000
if not self.paused:
if not self.winner:
self.update()
self.events()
self.draw()
def quit(self):
pg.quit()
sys.exit()
def update(self):
# Enemy hits player
hits = pg.sprite.spritecollide(self.player, self.mobs, False)
if (hits):
# print("hit")
pg.time.wait(1500)
self.playing = False
#win condition
win = pg.sprite.spritecollide(self.player, self.boats, False)
if(win):
print("you win")
self.winner = True
hits = pg.sprite.spritecollide(self.player, self.items, True)
for hit in hits:
if (hit.type == 'torch'):
self.torch = True
self.itemscreen_torch = True
if (hit.type == 'feather'):
self.feather = True
self.itemscreen_feather = True
# update portion of the game loop
self.all_sprites.update()
self.camera.update(self.player)
if self.feather:
self.player.player_speed = 600
def draw_grid(self):
for x in range(0,WIDTH, TILESIZE):
pg.draw.line(self.screen, LIGHTGREY, (x,0), (x, HEIGHT))
for y in range(0,HEIGHT, TILESIZE):
pg.draw.line(self.screen, LIGHTGREY, (0,y), (WIDTH, y))
def render_fog(self, light_depth):
# draw the light mask (gradient) onto fog image
self.fog.fill(NIGHT_COLOR)
self.light_mask = pg.transform.scale(self.light_mask_img, light_depth)
self.light_rect = self.light_mask.get_rect()
self.light_rect.center = self.camera.apply(self.player).center
self.fog.blit(self.light_mask, self.light_rect)
self.screen.blit(self.fog, (0,0), special_flags = pg.BLEND_MULT)
def draw(self):
self.screen.blit(self.map_img, self.camera.apply_rect(self.map_rect))
# self.screen.fill(BGCOLOR)
# self.draw_grid()
for sprite in self.all_sprites:
self.screen.blit(sprite.image, self.camera.apply(sprite))
# if self.night:
# self.render_fog()
if self.torch:
self.render_fog(TORCH_RADIUS)
else:
self.render_fog(LIGHT_RADIUS)
if self.paused:
self.screen.blit(self.dim_screen, (0, 0))
self.draw_text("Paused", self.pause_font, 90, WHITE, WIDTH/2, HEIGHT/4, align = "center")
if self.start:
self.screen.blit(self.dim_screen, (0, 0))
self.draw_text("THE LOAST COAST", self.pause_font, 90, WHITE, WIDTH/2, HEIGHT/4, align = "center")
self.draw_text("You're Abhiroop.", self.pause_font, 30, WHITE, WIDTH/2, HEIGHT/2, align = "center")
self.draw_text("You've been stranded on the island haunted", self.pause_font, 30, WHITE, WIDTH/2, HEIGHT/2+32, align="center")
self.draw_text("by Salty Sam's ghost. You must escape from", self.pause_font, 30, WHITE, WIDTH/2, HEIGHT/2+64, align="center")
self.draw_text("this island before he finds you and resets", self.pause_font, 30, WHITE, WIDTH/2, HEIGHT/2+96, align="center")
self.draw_text("you back to spawn. Press any key to", self.pause_font, 30, WHITE, WIDTH/2, HEIGHT/2+128, align="center")
self.draw_text("continue.", self.pause_font, 30, WHITE, WIDTH/2, HEIGHT/2+160, align="center")
self.draw_text("(Pro tip: WASD to move, P to Pause, Esc to Quit)", self.pause_font, 20, WHITE, WIDTH/2, HEIGHT-100, align="center")
if self.winner:
self.screen.blit(self.dim_screen,(0,0))
self.draw_text("You Win!", self.pause_font, 90, WHITE, WIDTH/2, HEIGHT/4, align = "center")
self.draw_text("Press R to reset", self.pause_font, 30, WHITE, WIDTH/2, HEIGHT/2, align="center")
self.draw_text("Press Esc to quit", self.pause_font, 30, WHITE, WIDTH/2, HEIGHT/2+32, align="center")
if self.itemscreen_torch:
self.screen.blit(self.dim_screen, (0, 0))
self.draw_text("By the virtue of the glasses of Neelak,", self.pause_font, 30, WHITE, WIDTH/2, HEIGHT/2, align = "center")
self.draw_text("You can now see a little better.", self.pause_font, 30, WHITE, WIDTH/2, HEIGHT/2+32, align="center")
self.draw_text("Press Q to continue.", self.pause_font, 30, WHITE, WIDTH/2, HEIGHT/2+64, align="center")
if self.itemscreen_feather:
self.screen.blit(self.dim_screen, (0, 0))
self.draw_text("By the virtue of Monty's Porsche in", self.pause_font, 30, WHITE, WIDTH/2, HEIGHT/2, align = "center")
self.draw_text("the States, you now run faster.", self.pause_font, 30, WHITE, WIDTH/2, HEIGHT/2+32, align="center")
self.draw_text("Press Q to continue.", self.pause_font, 30, WHITE, WIDTH/2, HEIGHT/2+64, align="center")
pg.display.flip()
def events(self):
# catch all events here
for event in pg.event.get():
if event.type == pg.QUIT:
self.quit()
if event.type == pg.KEYDOWN:
self.start = False
if event.key == pg.K_ESCAPE:
self.quit()
if event.key == pg.K_p and self.winner != True:
self.paused = not self.paused
if event.key == pg.K_r and self.winner == True:
self.playing = False
if event.key == pg.K_q:
self.itemscreen_torch = False
self.itemscreen_feather = False
def show_start_screen(self):
pass
def show_go_screen(self):
pass
# create the game object
g = Game()
g.show_start_screen()
while True:
g.new()
g.run()
g.show_go_screen()