-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevels.py
More file actions
87 lines (66 loc) · 2.68 KB
/
levels.py
File metadata and controls
87 lines (66 loc) · 2.68 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import pygame, os
import constants
import platforms
class Level():
""" This is a generic super-class used to define a level.
Create a child class for each level with level-specific
info. """
# Lists of sprites used in all levels. Add or remove
# lists as needed for your game. """
platform_list = None
enemy_list = None
# Background image
background = None
# How far this world has been scrolled left/right
world_shift = 0
level_limit = -1000
def __init__(self, player):
""" Constructor. Pass in a handle to player. Needed for when moving platforms
collide with the player. """
self.platform_list = pygame.sprite.Group()
self.enemy_list = pygame.sprite.Group()
self.player = player
# Update everythign on this level
def update(self):
""" Update everything in this level."""
self.platform_list.update()
self.enemy_list.update()
def draw(self, screen):
""" Draw everything on this level. """
# Draw the background
# We don't shift the background as much as the sprites are shifted
# to give a feeling of depth.
screen.fill(constants.BLUE)
screen.blit(self.background,(self.world_shift // 3,0))
# Draw all the sprite lists that we have
self.platform_list.draw(screen)
self.enemy_list.draw(screen)
def shift_world(self, shift_x):
""" When the user moves left/right and we need to scroll everything: """
# Keep track of the shift amount
self.world_shift += shift_x
# Go through all the sprite lists and shift
for platform in self.platform_list:
platform.rect.x += shift_x
for enemy in self.enemy_list:
enemy.rect.x += shift_x
# Create platforms for the level
class Level_01(Level):
""" Definition for level 1. """
def __init__(self, player):
""" Create level 1. """
# Call the parent constructor
Level.__init__(self, player)
self.background = pygame.image.load(os.path.join("levels","town","background.png")).convert()
#self.background.set_colorkey(constants.WHITE)
self.level_limit = -2500
with open(os.path.join("levels","town","town.txt")) as platfile:
platlist = platfile.readlines()
for i, line in enumerate(platlist):
for j, c in enumerate(line):
if ( c > "@" ):
block = platforms.Platform((70*(ord(c)-ord('A')),0,70,70))
block.rect.x = j*70
block.rect.y = i*70
block.player = self.player
self.platform_list.add(block)