-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel.py
123 lines (101 loc) · 3.2 KB
/
level.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
import pygame
from settings import *
from tile import Tile
from player import Player
from support import *
from random import choice
from weapon import Weapon
from ui import UI
class Level:
def __init__(self):
# get the display surface
self.display_surface = pygame.display.get_surface()
# sprite group setup
self.visible_sprites = YSortCameraGroup()
self.obstacle_sprites = pygame.sprite.Group()
# attack sprites
self.current_attack = None
# sprite setup
self.create_map()
# user interface
self.ui = UI()
def create_map(self):
layouts = {
'boundary': import_csv_layout('assets/map/map_FloorBlocks.csv'),
'grass': import_csv_layout('assets/map/map_Grass.csv'),
'object': import_csv_layout('assets/map/map_Objects.csv'),
}
graphics = {
'grass': import_folder('assets/images/grass'),
'objects': import_folder('assets/images/objects')
}
for style, layout in layouts.items():
for row_index, row in enumerate(layout):
for col_index, col in enumerate(row):
if col != '-1':
x = col_index * TILE_SIZE
y = row_index * TILE_SIZE
if style == 'boundary':
Tile(
(x, y),
self.obstacle_sprites,
'invisible'
)
if style == 'grass':
# create a grass tile
random_grass_image = choice(graphics['grass'])
Tile(
(x, y),
[self.visible_sprites, self.obstacle_sprites],
'grass', random_grass_image
)
if style == 'object':
# create an object tile
surf = graphics['objects'][int(col)]
Tile(
(x, y),
[self.visible_sprites, self.obstacle_sprites],
'objects', surf
)
self.player = Player(
(2000, 1400),
self.visible_sprites,
self.obstacle_sprites,
self.create_attack,
self.destroy_attack
)
def create_attack(self):
self.current_attack = Weapon(self.player, self.visible_sprites)
def destroy_attack(self):
if self.current_attack:
self.current_attack.kill()
self.current_attack = None
def run(self):
# update and draw the game
self.visible_sprites.custom_draw(self.player)
self.visible_sprites.update()
self.ui.display(self.player)
# debug(self.player.direction)
# debug(self.player.status)
class YSortCameraGroup(pygame.sprite.Group):
def __init__(self):
# general setup
super().__init__()
self.display_surface = pygame.display.get_surface()
self.half_width = self.display_surface.get_size()[0] // 2
self.half_height = self.display_surface.get_size()[1] // 2
self.offset = pygame.math.Vector2()
# creating the floor
self.floor_surf = pygame.image.load('assets/images/map/ground.png').convert()
self.floor_rect = self.floor_surf.get_rect(topleft=(0, 0))
def custom_draw(self, player):
# getting the offset
self.offset.x = player.rect.centerx - self.half_width
self.offset.y = player.rect.centery - self.half_height
# drawing the floor
floor_offset_pos = self.floor_rect.topleft - self.offset
self.display_surface.blit(self.floor_surf, floor_offset_pos)
# for sprite in self.sprites():
for sprite in sorted(self.sprites(), key=lambda sprite: sprite.rect.centery):
offset_pos = sprite.rect.topleft - self.offset
self.display_surface.blit(sprite.image, offset_pos)