-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.py
114 lines (102 loc) · 4.42 KB
/
map.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
from brick import Brick
from goomba import Goomba
from koopa import Koopa
from upgrade import Upgrade
class Map:
"""Implements Maze"""
BRICK_SIZE = 40
def __init__(self, screen, settings, bricks, pipes, mario, enemies, ground, upgrades, stats, secret_bricks):
self.screen = screen
self.settings = settings
self.stats = stats
self.bricks = bricks
self.secret_bricks = secret_bricks
self.pipes = pipes
self.mario = mario
self.upgrades = upgrades
self.enemies = enemies
self.ground = ground # +
self.main_level = 'images/level_loc.txt'
self.secret_level = 'images/Underground_Level.txt'
# Checks if it is main level or secret level and opens corresponding file
if not self.stats.activate_secret:
with open(self.main_level, 'r') as f:
self.rows = f.readlines()
if self.stats.return_main_level:
# Puts Mario at the pipe near the end
self.mario.rect.x = 7210
self.mario.rect.y = 500
if self.stats.activate_secret:
with open(self.secret_level, 'r') as f:
self.rows = f.readlines()
# Has Mario fall into secret level
self.mario.rect.x = 100
self.mario.rect.y = 100
self.brick = None
self.goomba = None
self.koopa = None
self.coin = None
self.deltax = self.deltay = Map.BRICK_SIZE
def build_brick(self):
dx, dy = self.deltax, self.deltay
for nrow in range(len(self.rows)):
row = self.rows[nrow]
for ncol in range(len(row)):
col = row[ncol]
# Checks for specific characters and calls corresponding function
if col == 'B':
Map.create_brick(self, ncol*dx, nrow*dy, 0)
if col == '?':
Map.create_brick(self, ncol * dx, nrow * dy, 1)
if col == 'X':
Map.create_brick(self, ncol * dx, nrow * dy, 3)
if col == 'M':
Map.create_brick(self, ncol * dx, nrow * dy, 2)
if col == 'R':
Map.create_brick(self, ncol * dx, nrow * dy, 4)
if col == 'S':
Map.create_brick(self, ncol*dx, nrow*dy, 5)
if col == 'I':
Map.create_brick(self, ncol*dx, nrow*dy, 6)
if col == 'G':
Map.create_enemy(self, ncol*dx, nrow*dy, 0)
if col == 'K':
Map.create_enemy(self, ncol*dx, nrow*dy, 1)
if col == 'A':
Map.create_secret_brick(self, ncol * dx, nrow * dy, 7)
if col == 'N':
Map.create_secret_brick(self, ncol * dx, nrow * dy, 8)
if col == 'C':
Map.create_coin(self, ncol * dx, nrow * dy, 4)
if col == 'L':
Map.create_brick(self, ncol*dx, nrow*dy, 9)
def blitme(self):
"""Draws the bricks into the window"""
for rect in self.bricks:
self.screen.blit(self.brick.image, rect)
def create_brick(self, x, y, num):
self.brick = Brick(self.screen, self.settings, num)
self.brick.rect.x = x
self.brick.rect.y = y
self.bricks.add(self.brick)
if num == 4: # +
self.ground.add(self.brick) # +
def create_secret_brick(self, x, y, num):
self.brick = Brick(self.screen, self.settings, num)
self.brick.rect.x = x
self.brick.rect.y = y
self.secret_bricks.add(self.brick)
def create_coin(self, x, y, num):
self.coin = Upgrade(self.screen, self.settings, self.pipes, self.bricks, x, y, num)
self.upgrades.add(self.coin)
def create_enemy(self, x, y, num):
if num == 0:
self.goomba = Goomba(self.screen, self.settings, self.pipes, self.bricks, self.ground) # +
self.goomba.x = x
self.goomba.y = y
self.enemies.add(self.goomba)
if num == 1:
self.koopa = Koopa(self.screen, self.settings, self.pipes, self.bricks, self.enemies, self.mario)
self.koopa.x = x
self.koopa.y = y - 1
self.enemies.add(self.koopa)