-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.py
97 lines (86 loc) · 4.96 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
import constants
import pygame
from Character import Character, HealthBar
from ItemBox import ItemBox
class World():
def __init__(self, level):
# TODO the same for enemy types ?
self.obstacle_list = []
# TODO move to outside so no need to load m ulti time same images
self.img_list = []
for x in range(constants.TILE_TYPES):
img = pygame.image.load(f'images/tiles/{level}/{x}.png') # TODO level different folder because different map?
img = pygame.transform.scale(img, (constants.TILE_SIZE, constants.TILE_SIZE))
self.img_list.append(img)
def process_data(self, data, enemy_group, item_box_group, water_group, decoration_group, exit_group, saved_coin):
self.level_length = len(data[0])
# iterate through each value in level file
for y, row in enumerate(data):
for x, tile in enumerate(row):
# no process -1
if tile >= 0:
img = self.img_list[tile] # TODO move img_list
img_rect = img.get_rect()
img_rect.x = x * constants.TILE_SIZE
img_rect.y = y * constants.TILE_SIZE
tile_data = (img, img_rect)
# TODO CONSTANT TILE TYPES
if tile >= 0 and tile <= 8:
self.obstacle_list.append(tile_data)
elif tile >= 9 and tile <= 12:
water = Water(img, x * constants.TILE_SIZE, y * constants.TILE_SIZE)
water_group.add(water)
elif tile >= 13 and tile <= 14:
decoration = Decoration(img, x * constants.TILE_SIZE, y * constants.TILE_SIZE)
decoration_group.add(decoration)
elif tile == 15: #create player
player = Character(x * constants.TILE_SIZE, y * constants.TILE_SIZE, 1, 'player', 'moona', constants.PLAYER_SPEED, constants.PLAYER_HP, constants.GRENADE_NUMBER)
health_bar = HealthBar(10, 10, player.health, player.max_health)
player.coin = saved_coin
elif tile == 16: #create enemy (TODO types upgrade later)
enemy = Character(x * constants.TILE_SIZE, y * constants.TILE_SIZE, 0.8, 'enemy', 'udin', constants.ENEMY_UDIN_SPEED, constants.ENEMY_UDIN_HP, 0) # no grenade for enemy
# enemy_group.add(enemy2)
enemy_group.add(enemy)
elif tile == 17: # create item box
item_box_hp = ItemBox(constants.ITEM_BOX_NAME_HEALTH, x * constants.TILE_SIZE, y * constants.TILE_SIZE) # TODO aim manually lol
item_box_group.add(item_box_hp)
elif tile == 18: # create grenade box
item_box_grenade = ItemBox(constants.ITEM_BOX_NAME_GRENADE, x * constants.TILE_SIZE, y * constants.TILE_SIZE)
item_box_group.add(item_box_grenade)
elif tile == 19:
item_box_coin = ItemBox(constants.ITEM_BOX_NAME_COIN, x * constants.TILE_SIZE, y * constants.TILE_SIZE)
item_box_group.add(item_box_coin)
elif tile == 20: #create exit
exit = Exit(img, x * constants.TILE_SIZE, y * constants.TILE_SIZE)
exit_group.add(exit)
return player, health_bar
def draw(self, screen, screen_scroll):
for tile in self.obstacle_list:
tile[1][0] += screen_scroll
screen.blit(tile[0], tile[1])
# TODO should combine all these classes?
class Decoration(pygame.sprite.Sprite):
def __init__(self, img, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = img
self.rect = self.image.get_rect()
self.rect.midtop = (x + constants.TILE_SIZE // 2, y + (constants.TILE_SIZE - self.image.get_height()))
def update(self, screen_scroll):
self.rect.x += screen_scroll
class Water(pygame.sprite.Sprite): # Or any effective tile
def __init__(self, img, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = img
self.rect = self.image.get_rect()
self.rect.midtop = (x + constants.TILE_SIZE // 2, y + (constants.TILE_SIZE - self.image.get_height()))
def update(self, screen_scroll):
self.rect.x += screen_scroll
# TODO handle dialouge before or after
class Exit(pygame.sprite.Sprite): # Go to next level
def __init__(self, img, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = img
self.rect = self.image.get_rect()
self.rect.midtop = (x + constants.TILE_SIZE // 2, y + (constants.TILE_SIZE - self.image.get_height()))
def update(self, screen_scroll):
self.rect.x += screen_scroll