-
Notifications
You must be signed in to change notification settings - Fork 0
/
encounter.py
171 lines (139 loc) · 6.1 KB
/
encounter.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import pygame
from pygame.color import Color
from pygame.constants import RLEACCEL
from controls import BTN_SUBMIT, BTN_UP, BTN_DOWN, BTN_LEFT, BTN_RIGHT
from scene import Scene, SCN_WORLD
from utils import load_spritesheet_flat
GRID_SIZE_PX = 16 # in pixels.
N_GRID_CELLS = 16 # grid of 16x16 cells
class EncounterScene(Scene):
def __init__(self, scene_manager):
Scene.__init__(self, scene_manager)
# load sprites
self.front_sprites = load_spritesheet_flat('assets/monsters_front.png',
4 * GRID_SIZE_PX, True)
self.back_sprites = load_spritesheet_flat('assets/player_back.png',
4 * GRID_SIZE_PX)
self.icon_sprites = load_spritesheet_flat('assets/icons.png',
1 * GRID_SIZE_PX)
self.sprites = pygame.sprite.LayeredDirty()
# build menu struct
self.menus = [ ['Scare', 'Bait'] ]
self.cur_menu = (0, 0)
# make bg
self.bg = self.make_bg()
# add monster and player sprites
monster_id = 5
monster_img = self.front_sprites[monster_id]
EncounterSprite(monster_img, [self.sprites], (11, 1))
player_img = self.back_sprites[0]
EncounterSprite(player_img, [self.sprites], (1, 5))
# add menu selector arrow
arrow_img = self.icon_sprites[0]
coords = (1, 10)
self.cursor_spr = EncounterSprite(arrow_img, [self.sprites], coords)
# needed?
for char in self.sprites:
self.sprites.change_layer(char, 1)
# self.resume()
def make_bg(self):
""" Build all background elements. Return image.
"""
bg = pygame.Surface((N_GRID_CELLS * GRID_SIZE_PX, N_GRID_CELLS * GRID_SIZE_PX))
bg.fill((255, 255, 255))
# debugging grid
for x in range(1, N_GRID_CELLS):
start = x * GRID_SIZE_PX, 0
end = x * GRID_SIZE_PX, N_GRID_CELLS * GRID_SIZE_PX
pygame.draw.line(bg, (200, 200, 200), start, end)
for y in range(1, N_GRID_CELLS):
start = 0, y * GRID_SIZE_PX
end = N_GRID_CELLS * GRID_SIZE_PX, y * GRID_SIZE_PX
pygame.draw.line(bg, (200, 200, 200), start, end)
# top HUD
start = (0, 1 * GRID_SIZE_PX)
end = (N_GRID_CELLS * GRID_SIZE_PX, 1 * GRID_SIZE_PX)
pygame.draw.line(bg, (0, 0, 0), start, end, 2)
# bottom HUD
start = (0, 9 * GRID_SIZE_PX)
end = (N_GRID_CELLS * GRID_SIZE_PX, 9 * GRID_SIZE_PX)
pygame.draw.line(bg, (0, 0, 0), start, end, 2)
# menu choices
# TODO: render via game font instead
font = pygame.font.SysFont("monospace", GRID_SIZE_PX)
menu_item_area = (0, 0, 5 * GRID_SIZE_PX, GRID_SIZE_PX)
surf = font.render(self.menus[0][0], 1, (0, 0, 0))
bg.blit(surf, (2 * GRID_SIZE_PX, 10 * GRID_SIZE_PX), menu_item_area)
surf = font.render(self.menus[0][1], 1, (0, 0, 0))
bg.blit(surf, (10 * GRID_SIZE_PX, 10 * GRID_SIZE_PX), menu_item_area)
return bg
def process_input(self, action):
""" Return the name of the mode to execute next.
Return None if mode is unchanged.
"""
y, x = self.cur_menu
if action == BTN_SUBMIT:
choice = self.menus[y][x]
if choice == 'Scare':
return SCN_WORLD
if action == BTN_UP:
y = (y - 1) % len(self.menus)
elif action == BTN_DOWN:
y = (y + 1) % len(self.menus)
elif action == BTN_LEFT:
x = (x - 1) % len(self.menus[0])
elif action == BTN_RIGHT:
x = (x + 1) % len(self.menus[0])
self.cur_menu = y, x
pos = (1 + x * 8, 10 + y * 2)
self.cursor_spr.pos = pos
return None
def resume(self):
self._screen.fill((0, 0, 0))
self._screen.blit(self.bg, (0, 0))
pygame.display.flip()
for char in self.sprites:
char.dirty = 1
def tick(self, fps):
self.sprites.clear(self._screen, self.bg)
self.sprites.update(fps)
changed_rects = self.sprites.draw(self._screen)
pygame.display.update(changed_rects)
class EncounterSprite(pygame.sprite.DirtySprite):
def _get_pos(self):
return self.x, self.y
def _set_pos(self, pos):
x, y = pos
self.x, self.y = x, y
self.dirty = 1
self.rect = pygame.Rect(x * GRID_SIZE_PX, y * GRID_SIZE_PX, GRID_SIZE_PX, GRID_SIZE_PX)
pos = property(_get_pos, _set_pos)
def __init__(self, image, containers, pos=(0, 0)):
# containers: list of sprite groups to join
pygame.sprite.DirtySprite.__init__(self, *containers)
self.image = image
self.pos = pos
def update(self, fps):
pass
if __name__ == "__main__":
from pygame.constants import QUIT, KEYDOWN, K_ESCAPE, \
K_UP, K_DOWN, K_LEFT, K_RIGHT, K_RETURN, K_w, K_s, K_a, K_d, K_KP_ENTER
pygame.init()
_key_map = { K_UP: BTN_UP, K_DOWN: BTN_DOWN, K_LEFT: BTN_LEFT,
K_RIGHT: BTN_RIGHT, K_w: BTN_UP, K_s: BTN_DOWN, K_a: BTN_LEFT,
K_d: BTN_RIGHT, K_RETURN: BTN_SUBMIT, K_KP_ENTER: BTN_SUBMIT }
screen_res = N_GRID_CELLS * GRID_SIZE_PX
screen = pygame.display.set_mode((screen_res, screen_res))
fps = 60
clock = pygame.time.Clock()
em = EncounterScene(screen)
game_over = False
while not game_over:
for e in pygame.event.get():
if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
game_over = True
elif (e.type == KEYDOWN and e.key in _key_map.keys()):
if em.process_input(_key_map[e.key]) == SCN_WORLD:
game_over = True
em.tick(fps)
clock.tick(fps)