source: nagslang/screens/area.py@ 147:7d8e1682587f

Last change on this file since 147:7d8e1682587f was 147:7d8e1682587f, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Fix imports.

File size: 4.6 KB
Line 
1"""Display a game area."""
2
3import pygame
4import pymunk
5import pymunk.pygame_util
6
7from nagslang.events import ScreenChange
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 self.add_protagonist()
72 self.add_game_objects()
73
74 def add_walls(self):
75 self.walls = []
76 body = pymunk.Body()
77 body.position = (0, 0)
78 walls = self._level.get_walls()
79 for wall in walls:
80 corners = wall
81 corner = corners[-1]
82 for next_corner in corners:
83 wall = pymunk.Segment(body, corner, next_corner, 5)
84 wall.elasticity = 1.0
85 self.walls.append(wall)
86 corner = next_corner
87 self.space.add(*self.walls)
88
89 def add_game_objects(self):
90 for drawable in self._level.get_drawables():
91 self._drawables.add(drawable)
92
93 def add_protagonist(self):
94 self.protagonist = Protagonist(self.space, (350, 300))
95 self._drawables.add(self.protagonist)
96
97 def handle_event(self, ev):
98 if ev.type == pygame.locals.KEYDOWN:
99 if ev.key == pygame.locals.K_ESCAPE:
100 ScreenChange.post('menu')
101 if ev.key == pygame.locals.K_c:
102 self.protagonist.toggle_form()
103 self.keys.handle_event(ev)
104
105 def _calc_viewport(self, level_surface, display_surface):
106 level_size = level_surface.get_size()
107 display_size = display_surface.get_size()
108 protagnist_pos = self.protagonist.physicser.get_render_position(
109 level_surface)
110 x_wide = display_size[0] // 2
111 y_wide = display_size[1] // 2
112 if display_size[0] > level_size[0]:
113 x = -(display_size[0] - level_size[0]) // 2
114 elif protagnist_pos[0] < x_wide:
115 x = 0
116 elif protagnist_pos[0] > level_size[0] - x_wide:
117 x = level_size[0] - display_size[0]
118 else:
119 x = protagnist_pos[0] - x_wide
120 if display_size[1] > level_size[1]:
121 y = -(display_size[1] - level_size[1]) // 2
122 elif protagnist_pos[1] < y_wide:
123 y = 0
124 elif protagnist_pos[1] > level_size[1] - y_wide:
125 y = level_size[1] - display_size[1]
126 else:
127 y = protagnist_pos[1] - y_wide
128 return pygame.rect.Rect(x, y, display_size[0], display_size[1])
129
130 def render(self, surface):
131 #surface.fill(pygame.color.Color(0, 0, 0))
132 background = self._level.get_background()
133 mysurface = background.copy()
134 for drawable in self._drawables:
135 drawable.render(mysurface)
136 render_rect = self._calc_viewport(mysurface, surface)
137 surface.blit(mysurface, (0, 0), render_rect)
138
139 def tick_protagonist(self):
140 dx, dy = self.keys.get_direction()
141 self.protagonist.set_direction(dx, dy)
142
143 def tick(self, seconds):
144 self.tick_protagonist()
145 for drawable in self._drawables:
146 drawable.animate()
147 super(AreaScreen, self).tick(seconds)
Note: See TracBrowser for help on using the repository browser.