-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld.py
More file actions
executable file
·63 lines (51 loc) · 1.82 KB
/
world.py
File metadata and controls
executable file
·63 lines (51 loc) · 1.82 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
import pygame
from pygame.locals import *
from sprites import Goomba
from globals import *
class Wall:
def __init__(self, rect, char):
self.rect = rect
self.type = get_type(char)
def collide(self, other_rect):
return self.rect.colliderect(other_rect)
def get_type(value):
if value == ' ':
return SKY
elif value == '#':
return LAND
elif value == '$':
return SHRINK
elif value == 'L':
return LEDGE
elif value == '%':
return GROW
class Map:
def __init__(self, filename):
self.load_map(filename)
def load_map(self, filename):
self.grid = []
self.walls = []
self.enemies = []
row = 0
for line in open(filename, 'r'):
# if line[0] == "#":
# continue
tile_row = []
for col, char in enumerate(line.strip()):
if char == '1':
self.start_position = col * BLOCK_SIZE, row * BLOCK_SIZE
char = ' '
elif char == 'G':
self.enemies.append(Goomba((col * BLOCK_SIZE, row * BLOCK_SIZE)))
char = ' '
tile = Wall(pygame.Rect(col * BLOCK_SIZE, row * BLOCK_SIZE,BLOCK_SIZE, BLOCK_SIZE), char)
tile_row.append(tile)
if tile.type != SKY:
self.walls.append(tile)
self.grid.append(tile_row)
row += 1
self.height, self.width = len(self.grid) * BLOCK_SIZE, len(self.grid[0]) * BLOCK_SIZE
def nearby_walls(self, rect):
x1, x2 = rect.x / BLOCK_SIZE, (rect.x + rect.width) / BLOCK_SIZE + 1
y1, y2 = rect.y / BLOCK_SIZE, (rect.y + rect.height) / BLOCK_SIZE + 1
return [tile for row in self.grid[y1 : y2] for tile in row[x1 : x2] if tile.type != SKY]