source: nagslang/screens/area.py@ 180:026297a03963

Last change on this file since 180:026297a03963 was 180:026297a03963, checked in by Neil Muller <drnlmuller@…>, 7 years ago

Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door

File size: 5.2 KB
Line 
1"""Display a game area."""
2
3import pygame
4import pymunk
5import pymunk.pygame_util
6
7from nagslang.events import ScreenChange, DoorEvent
8from nagslang.level import Level
9from nagslang.protagonist import Protagonist
10from nagslang.screens.base import Screen
11
12
13class ControlKeys(object):
14 direction_keys = {
15 (0, 1): set([pygame.locals.K_UP, pygame.locals.K_w]),
16 (0, -1): set([pygame.locals.K_DOWN, pygame.locals.K_s]),
17 (-1, 0): set([pygame.locals.K_LEFT, pygame.locals.K_a]),
18 (1, 0): set([pygame.locals.K_RIGHT, pygame.locals.K_d]),
19 }
20
21 def __init__(self):
22 self.keys_down = set()
23
24 def key_down(self, key):
25 self.keys_down.add(key)
26
27 def key_up(self, key):
28 self.keys_down.discard(key)
29
30 def handle_event(self, ev):
31 if ev.type == pygame.locals.KEYDOWN:
32 self.key_down(ev.key)
33 elif ev.type == pygame.locals.KEYUP:
34 self.key_up(ev.key)
35
36 def get_direction(self):
37 dx, dy = 0, 0
38 for (tx, ty), keys in self.direction_keys.iteritems():
39 if self.keys_down & keys:
40 dx += tx
41 dy += ty
42 return (dx, dy)
43
44
45class Drawables(object):
46 def __init__(self):
47 self._drawables = {}
48
49 def add(self, drawable):
50 self._drawables.setdefault(drawable.zorder, []).append(drawable)
51
52 def remove(self, drawable):
53 self._drawables[drawable.zorder].remove(drawable)
54
55 def get_drawables(self):
56 for zorder in sorted(self._drawables):
57 for drawable in self._drawables[zorder]:
58 yield drawable
59
60 __iter__ = get_drawables
61
62
63class AreaScreen(Screen):
64
65 def setup(self):
66 self.keys = ControlKeys()
67 self._level = Level(self.name)
68 self._level.load(self.space)
69 self._drawables = Drawables()
70 self.add_walls()
71 if self.protagonist is not None:
72 # We do things this way to avoid extra pymunk
73 # juggling to move objects between spaces
74 old_protagonist = self.protagonist
75 self.add_protagonist()
76 self.protagonist.copy_state(old_protagonist)
77 else:
78 self.add_protagonist()
79 self.add_game_objects()
80
81 def add_walls(self):
82 self.walls = []
83 body = pymunk.Body()
84 body.position = (0, 0)
85 walls = self._level.get_walls()
86 for wall in walls:
87 corners = wall
88 corner = corners[-1]
89 for next_corner in corners:
90 wall = pymunk.Segment(body, corner, next_corner, 5)
91 wall.elasticity = 1.0
92 self.walls.append(wall)
93 corner = next_corner
94 self.space.add(*self.walls)
95
96 def add_game_objects(self):
97 for drawable in self._level.get_drawables():
98 self._drawables.add(drawable)
99
100 def add_protagonist(self):
101 self.protagonist = Protagonist(self.space, (350, 300))
102 self._drawables.add(self.protagonist)
103
104 def handle_event(self, ev):
105 if ev.type == pygame.locals.KEYDOWN:
106 if ev.key == pygame.locals.K_ESCAPE:
107 ScreenChange.post('menu')
108 if ev.key == pygame.locals.K_c:
109 self.protagonist.toggle_form()
110 elif DoorEvent.matches(ev):
111 self.protagonist.set_position(ev.dest_pos)
112 if ev.destination != self.name:
113 # Go to anther screen
114 ScreenChange.post(ev.destination, self.protagonist)
115 # else we're teleporting within the screen, and just the
116 # position change is enough
117 self.keys.handle_event(ev)
118
119 def _calc_viewport(self, level_surface, display_surface):
120 level_size = level_surface.get_size()
121 display_size = display_surface.get_size()
122 protagnist_pos = self.protagonist.physicser.get_render_position(
123 level_surface)
124 x_wide = display_size[0] // 2
125 y_wide = display_size[1] // 2
126 if display_size[0] > level_size[0]:
127 x = -(display_size[0] - level_size[0]) // 2
128 elif protagnist_pos[0] < x_wide:
129 x = 0
130 elif protagnist_pos[0] > level_size[0] - x_wide:
131 x = level_size[0] - display_size[0]
132 else:
133 x = protagnist_pos[0] - x_wide
134 if display_size[1] > level_size[1]:
135 y = -(display_size[1] - level_size[1]) // 2
136 elif protagnist_pos[1] < y_wide:
137 y = 0
138 elif protagnist_pos[1] > level_size[1] - y_wide:
139 y = level_size[1] - display_size[1]
140 else:
141 y = protagnist_pos[1] - y_wide
142 return pygame.rect.Rect(x, y, display_size[0], display_size[1])
143
144 def render(self, surface):
145 #surface.fill(pygame.color.Color(0, 0, 0))
146 background = self._level.get_background()
147 mysurface = background.copy()
148 for drawable in self._drawables:
149 drawable.render(mysurface)
150 render_rect = self._calc_viewport(mysurface, surface)
151 surface.blit(mysurface, (0, 0), render_rect)
152
153 def tick_protagonist(self):
154 dx, dy = self.keys.get_direction()
155 self.protagonist.set_direction(dx, dy)
156
157 def tick(self, seconds):
158 self.tick_protagonist()
159 for drawable in self._drawables:
160 drawable.animate()
161 super(AreaScreen, self).tick(seconds)
Note: See TracBrowser for help on using the repository browser.