-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel.py
More file actions
65 lines (53 loc) · 2.37 KB
/
level.py
File metadata and controls
65 lines (53 loc) · 2.37 KB
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
import pygame
from settings import *
from tile import Tile
from player import Player
from debug import debug
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()
#sprite setup
self.create_map()
def create_map(self):
for row_index,row in enumerate(WORLD_MAP):
for col_index,col in enumerate(row):
x = col_index * TILESIZE
y = row_index * TILESIZE
if col == 'X':
Tile((x,y),[self.visible_sprites,self.obstacle_sprites])
if col == 'p':
#We're creating the player and giving the obstacle sprites for the collisions
self.player = Player((x,y),[self.visible_sprites],self.obstacle_sprites)
def run(self):
#update and draw the game
self.visible_sprites.custom_draw(self.player)
self.visible_sprites.update()
#debug(self.player.direction)
#This class will be a camera that sorts the sprites by the Y coordinates
class YSortCameraGroup(pygame.sprite.Group):
def __init__(self):
#general setup
super().__init__()
self.display_surface = pygame.display.get_surface()
#getting half of width and height to have Player in the middle of the screen
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('./graphics/tilemap/ground.png').convert()
self.floor_rect = self.floor_surf.get_rect(topleft = (0,0))
def custom_draw(self,player):
#getting 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)