-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
92 lines (76 loc) · 3.58 KB
/
generator.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
from game_state import State
from constants import *
from sprites import *
import random
class Generator:
def __init__(self):
self.road_next_x = 0
self.part_next_x = 0
self.obstacle_next_x = 0
def update(self, state):
self.road_next_x -= state.scroll_length
if self.road_next_x <= 0:
self.add_road(state)
self.road_next_x = WINDOW_SIZE[0]+self.road_next_x
self.obstacle_next_x -= state.scroll_length
if self.obstacle_next_x <= 0:
w = self.add_obstacle(state)
self.obstacle_next_x = w+self.obstacle_next_x+PLAYER_SIZE[0]*(1+random.random())
def add_road(self, state: State):
r = AnimationSprite(IMG_ROAD, (WINDOW_SIZE[0]+self.road_next_x+state.scroll_length, 0))
state.all_units.add(r)
state.scroll_objects.add(r)
state.graphic.add(r)
state.graphic.change_layer(r, BACKGROUND_LAYER)
def add_obstacle(self, state: State):
obj = random.choices([self.get_hazard(Hazard.FAN),
self.get_hazard(Hazard.CASTLE),
self.get_hazard(Hazard.RIVER)],
weights=[4, 1, 1])[0]
state.all_units.add(obj)
state.scroll_objects.add(obj)
state.obstacles.add(obj)
state.graphic.add(obj)
state.graphic.change_layer(obj, OBSTACLE_LAYER)
return obj.rect.width
def get_hazard(self, hazard: Hazard, version=0) -> AnimationSprite:
if version < 0 or version > 3:
raise AttributeError("Version number needs to be 0 (random) or 1-3")
if version == 0:
version = random.randint(1, 3)
if hazard == Hazard.FAN:
y = LANE_START_Y + (version-.5)*LANE_HEIGHT - PLAYER_SIZE[1]//2
return AnimationSprite(IMG_FAN,
(WINDOW_SIZE[0], y),
pygame.rect.Rect((1000, 1000), (140, 100)),
type=hazard)
if hazard == Hazard.CASTLE:
return AnimationSprite(IMG_CASTLE,
(WINDOW_SIZE[0], 0),
[pygame.rect.Rect((1000, 1000), (400, 600)),
pygame.rect.Rect((1000, 1000), (400, 400)),
pygame.rect.Rect((1000, 1000), (400, 000))],
[(0, 0), (0, -200), (0, 0)],
type=hazard)
if hazard == Hazard.RIVER:
if version == 1:
image = IMG_RIVER1
past_hitbox = pygame.rect.Rect((1000, 1000), (400, 600))
past_offset = (0,0)
elif version == 2:
image = IMG_RIVER2
past_hitbox = [pygame.rect.Rect((1000, 1000), (400, 400)),pygame.rect.Rect((1000, 1000), (400, 400))]
past_offset = [(0,0),(0,-600)]
elif version == 3:
image = IMG_RIVER3
past_hitbox = pygame.rect.Rect((1000, 1000), (400, 600))
past_offset = (0,-400)
return AnimationSprite(image,
(WINDOW_SIZE[0], 0),
[past_hitbox,
pygame.rect.Rect((1000, 1000), (200, 1000)),
pygame.rect.Rect((1000, 1000), (200, 1000))],
[past_offset, (0, 0), (0, 0)],
type=hazard)
def test_sprite(self, size):
return pygame.transform.scale(IMG_TEST, size)