-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtilemap.py
48 lines (37 loc) · 1.36 KB
/
tilemap.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
import pygame as pg
from settings import *
def collide_hit_rect(one, two):
return one.hit_rect.colliderect(two.rect)
S_WIDTH = 0
S_WIDTH = 0
class Map:
"""The Map class: the map of the game."""
def __init__(self, filename):
"""Initialize the Map and it's attributes."""
self.map_data = []
with open(filename, 'rt') as f:
for line in f:
self.map_data.append(line.strip())
self.tileWidth = len(self.map_data[0])
self.tileHeight = len(self.map_data)
self.width = self.tileWidth * TILESIZE
self.height = self.tileHeight * TILESIZE
S_WIDTH = self.tileWidth
S_HEIGHT = self.tileHeight
class Camera:
"""The Camera class: the camera that follows the player around."""
def __init__(self, width, height):
"""Initialize the Camera and it's attributes."""
self.camera = pg.Rect(0, 0, width, height)
self.width = width
self.height = height
def apply(self, entity):
return entity.rect.move(self.camera.topleft)
def update(self, target):
x = -target.rect.centerx + int(WIDTH/2)
y = -target.rect.centery + int(HEIGHT/2)
x = min(0, x)
y = min(0, y)
x = max(-(self.width - WIDTH), x)
y = max(-(self.height - HEIGHT), y)
self.camera = pg.Rect(x, y, self.width, self.height)