-
Notifications
You must be signed in to change notification settings - Fork 3
/
mini1_level.py
95 lines (71 loc) · 3.32 KB
/
mini1_level.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
import pygame
from settings import *
from tile import *
from mini1_player import Gabe1
class mini1_Level:
def __init__(self):
# level setup
self.display_surface = pygame.display.get_surface()
# sprite group setup
self.visible_sprites = CameraGroup()
self.active_sprites = pygame.sprite.Group()
self.collision_sprites = pygame.sprite.Group()
self.setup_level()
def setup_level(self):
for row_index,row in enumerate(LEVEL_MAP):
for col_index,col in enumerate(row):
x = col_index * TILE_SIZE
y = row_index * TILE_SIZE
if col == 'X':
Grass((x,y),[self.visible_sprites,self.collision_sprites])
if col == 'D':
Tile((x,y),[self.visible_sprites,self.collision_sprites])
if col == 'M':
Manure((x,y), [self.visible_sprites, self.collision_sprites])
p_counter.x += 1
# if col == 'C':
# Cow((x,y),[self.visible_sprites])
if col == 'P':
self.player = Gabe1((x,y),[self.visible_sprites,self.active_sprites],self.collision_sprites)
def run(self):
# run the entire game (level)
self.active_sprites.update()
self.visible_sprites.custom_draw(self.player)
class CameraGroup(pygame.sprite.Group):
def __init__(self):
super().__init__()
self.display_surface = pygame.display.get_surface()
self.offset = pygame.math.Vector2(100,300)
# camera
cam_left = CAMERA_BORDERS['left']
cam_top = CAMERA_BORDERS['top']
cam_width = self.display_surface.get_size()[0] - (cam_left + CAMERA_BORDERS['right'])
cam_height = self.display_surface.get_size()[1] - (cam_top + CAMERA_BORDERS['bottom'])
self.camera_rect = pygame.Rect(cam_left,cam_top,cam_width,cam_height)
def custom_draw(self,player):
# getting the camera position
if player.rect.left < self.camera_rect.left:
self.camera_rect.left = player.rect.left
if player.rect.right > self.camera_rect.right:
self.camera_rect.right = player.rect.right
if player.rect.top < self.camera_rect.top:
self.camera_rect.top = player.rect.top
if player.rect.bottom > self.camera_rect.bottom:
self.camera_rect.bottom = player.rect.bottom
# stop the camera from moving too far
if (self.camera_rect.right > (len(LEVEL_MAP[1])-4) * TILE_SIZE):
self.camera_rect.right = (len(LEVEL_MAP[1])-4) * TILE_SIZE
if self.camera_rect.left < 100:
self.camera_rect.left = 100
# stop player from moving too far
if player.rect.right > (len(LEVEL_MAP[1])-1) * TILE_SIZE:
player.rect.right = (len(LEVEL_MAP[1])-1) * TILE_SIZE
if player.rect.left < 0:
player.rect.left = 0
# camera offset
self.offset = pygame.math.Vector2(
self.camera_rect.left - CAMERA_BORDERS['left'],
self.camera_rect.top - CAMERA_BORDERS['top'])
for sprite in self.sprites():
offset_pos = sprite.rect.topleft - self.offset
self.display_surface.blit(sprite.image,offset_pos)