source: nagslang/screens/area.py@ 146:fff8e14858b4

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

Remove hardcoded level hackery.

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