-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.py
251 lines (201 loc) · 6.7 KB
/
world.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
import pickle
import pygame
from enemies import Ghost
from enemies import Zombie
NUM_TILES = 61
TILE_SIZE = 16
img_list = []
for index in range(1, NUM_TILES+1):
img = pygame.image.load(f'Tiles/{index}.png')
img_list.append(img)
class World:
def __init__(self, objects_group):
self.objects_group = objects_group
self.ground_list = []
self.rock_list = []
self.decor_list = []
def generate_world(self, data, win):
for y, row in enumerate(data):
for x, tile in enumerate(row):
if tile >= 0:
img = img_list[tile-1]
rect = img.get_rect()
rect.x = x * TILE_SIZE
rect.y = y * TILE_SIZE
tile_data = (img, rect)
if tile in (0, 1, 2, 3, 4, 5, 6, 11):
self.ground_list.append(tile_data)
if tile in (7, 14, 18, 19, 20, 21, 25, 26, 27, 28, 32, 33, 34, 35, 42, 43, 44, 45):
self.rock_list.append(tile_data)
if tile in (8, 9, 10, 13, 15, 16, 17, 23, 24, 30, 31, 37, 38, 39, 40, 46, 47, 48, 49, 50, 51):
self.decor_list.append(tile_data)
if tile == 12:
exit = Exit(x*TILE_SIZE, y*TILE_SIZE, tile_data)
self.objects_group[4].add(exit)
if tile == 41:
water = Water(x*TILE_SIZE, y*TILE_SIZE, tile_data)
self.objects_group[0].add(water)
if tile in (52, 53, 56, 57):
diamond = Diamond(x*TILE_SIZE, y*TILE_SIZE, tile_data)
self.objects_group[1].add(diamond)
if tile in (54, 55, 58, 59):
potion = Potion(x*TILE_SIZE, y*TILE_SIZE, tile_data)
self.objects_group[2].add(potion)
if tile == 60:
enemy = Ghost(x*TILE_SIZE, y*TILE_SIZE, win)
self.objects_group[3].add(enemy)
if tile == 61:
mushroom = Mushroom(x*TILE_SIZE, y*TILE_SIZE, tile_data)
self.objects_group[5].add(mushroom)
if tile == 62:
ruby = Ruby(x*TILE_SIZE, y*TILE_SIZE, tile_data)
self.objects_group[6].add(ruby)
if tile == 63:
enemy = Zombie(x*TILE_SIZE, y*TILE_SIZE, win)
self.objects_group[7].add(enemy)
def draw_world(self, win, screen_scroll):
for tile in self.ground_list:
tile[1][0] += screen_scroll
win.blit(tile[0], tile[1])
for tile in self.rock_list:
tile[1][0] += screen_scroll
win.blit(tile[0], tile[1])
for tile in self.decor_list:
tile[1][0] += screen_scroll
win.blit(tile[0], tile[1])
class Ladder(pygame.sprite.Sprite):
def __init__(self, x, y, tile_data):
super(Ladder, self).__init__()
self.image = tile_data[0]
self.rect = tile_data[1]
self.rect.x = x
self.rect.y = y
def update(self, screen_scroll):
self.rect.x += screen_scroll
def draw(self, win):
win.blit(self.image, self.rect)
class Water(pygame.sprite.Sprite):
def __init__(self, x, y, tile_data):
super(Water, self).__init__()
self.image = tile_data[0]
self.rect = tile_data[1]
self.rect.x = x
self.rect.y = y
def update(self, screen_scroll):
self.rect.x += screen_scroll
def draw(self, win):
win.blit(self.image, self.rect)
class Diamond(pygame.sprite.Sprite):
def __init__(self, x, y, tile_data):
super(Diamond, self).__init__()
self.image = tile_data[0]
self.rect = tile_data[1]
self.rect.x = x
self.rect.y = y
def update(self, screen_scroll):
self.rect.x += screen_scroll
def draw(self, win):
win.blit(self.image, self.rect)
class Potion(pygame.sprite.Sprite):
def __init__(self, x, y, tile_data):
super(Potion, self).__init__()
self.image = tile_data[0]
self.rect = tile_data[1]
self.rect.x = x
self.rect.y = y
def update(self, screen_scroll):
self.rect.x += screen_scroll
def draw(self, win):
win.blit(self.image, self.rect)
class Mushroom(pygame.sprite.Sprite):
def __init__(self, x, y, tile_data):
super(Mushroom, self).__init__()
self.image = tile_data[0]
self.rect = tile_data[1]
self.rect.x = x
self.rect.y = y
def update(self, screen_scroll):
self.rect.x += screen_scroll
def draw(self, win):
win.blit(self.image, self.rect)
class Ruby(pygame.sprite.Sprite):
def __init__(self, x, y, tile_data):
super(Ruby, self).__init__()
self.image = tile_data[0]
self.rect = tile_data[1]
self.rect.x = x
self.rect.y = y
def update(self, screen_scroll):
self.rect.x += screen_scroll
def draw(self, win):
win.blit(self.image, self.rect)
class Antidote(pygame.sprite.Sprite):
def __init__(self, x, y, tile_data):
super(Antidote, self).__init__()
self.image = tile_data[0]
self.rect = tile_data[1]
self.rect.x = x
self.rect.y = y
def update(self, screen_scroll):
self.rect.x += screen_scroll
def draw(self, win):
win.blit(self.image, self.rect)
class Exit(pygame.sprite.Sprite):
def __init__(self, x, y, tile_data):
super(Exit, self).__init__()
self.image = pygame.transform.scale(tile_data[0], (24,24))
self.rect = tile_data[1]
self.rect.x = x
self.rect.y = y - 8
def update(self, screen_scroll):
self.rect.x += screen_scroll
def draw(self, win):
win.blit(self.image, self.rect)
def load_level(level):
file = f'Levels/level{level}_data'
with open(file, 'rb') as f:
data = pickle.load(f)
for y in range(len(data)):
for x in range(len(data[0])):
if data[y][x] >= 0:
data[y][x] += 1
return data, len(data[0])
#class that fixes player collisions with object platforms
class ColumnCollisionWithPlayer():
def __init__(self, player, object_group):
self.player = player
self.object_group = object_group
def update(self):
#check for collision with objects
collision_list = pygame.sprite.spritecollide(self.player, self.object_group, False)
for obj in collision_list:
#check if player is above object
if self.player.rect.bottom > obj.rect.top and self.player.rect.bottom < obj.rect.bottom:
self.player.rect.bottom = obj.rect.top
self.player.y_vel = 0
self.player.jumping = False
#check if player is below object
elif self.player.rect.top < obj.rect.bottom:
self.player.rect.top = obj.rect.bottom
self.player.y_vel = 0
#check if player is right of object
if self.player.rect.right > obj.rect.left and self.player.rect.right < obj.rect.right:
self.player.rect.right = obj.rect.left
#check if player is left of object
elif self.player.rect.left < obj.rect.right:
self.player.rect.left = obj.rect.right
for playrer in obj(self.player):
if playrer.rect.bottom > obj.rect.top and playrer.rect.bottom < obj.rect.bottom:
playrer.rect.bottom = obj.rect.top
playrer.y_vel = 0
playrer.jumping = False
#check if player is below object
elif playrer.rect.top < obj.rect.bottom:
playrer.rect.top = obj.rect.bottom
playrer.y_vel = 0
#check if player is right of object
if playrer.rect.right > obj.rect.left and playrer.rect.right < obj.rect.right:
playrer.rect.right = obj.rect.left
#check if player is left of object
elif playrer.rect.left < obj.rect.right:
playrer.rect.left = obj.rect.right