source: nagslang/screens/area.py@ 140:f36a7075d9a0

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

Two switch puzzle!

File size: 5.4 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()
72 self._drawables = Drawables()
73 self.add_walls()
74 self.add_protagonist()
75 self._setup_demo_objects()
76
77 def _setup_demo_objects(self):
78 # TODO: Put this in a level instead
79 glue = PuzzleGlue()
80 switch = FloorSwitch(self.space, (300, 400))
81 switch2 = FloorSwitch(self.space, (300, 600))
82 light = FloorLight(self.space, (300, 500), 'switch')
83 light2 = FloorLight(self.space, (250, 500), 'both_switches')
84 light2.zorder = ZORDER_HIGH
85 glue.add_component('switch', switch.puzzler)
86 glue.add_component('switch2', switch2.puzzler)
87 glue.add_component('light', light.puzzler)
88 glue.add_component('light2', light2.puzzler)
89 glue.add_component(
90 'both_switches', StateLogicalAndPuzzler('switch', 'switch2'))
91 self._drawables.add(switch)
92 self._drawables.add(switch2)
93 self._drawables.add(light)
94 self._drawables.add(light2)
95 box = Box(self.space, (250, 350))
96 self._drawables.add(box)
97
98 def add_walls(self):
99 self.walls = []
100 body = pymunk.Body()
101 body.position = (0, 0)
102 walls = self._level.get_walls()
103 for wall in walls:
104 corners = wall
105 corner = corners[-1]
106 for next_corner in corners:
107 wall = pymunk.Segment(body, corner, next_corner, 5)
108 wall.elasticity = 1.0
109 self.walls.append(wall)
110 corner = next_corner
111 self.space.add(*self.walls)
112
113 def add_protagonist(self):
114 self.protagonist = Protagonist(self.space, (350, 300))
115 self._drawables.add(self.protagonist)
116
117 def handle_event(self, ev):
118 if ev.type == pygame.locals.KEYDOWN:
119 if ev.key == pygame.locals.K_ESCAPE:
120 ScreenChange.post('menu')
121 if ev.key == pygame.locals.K_c:
122 self.protagonist.toggle_form()
123 self.keys.handle_event(ev)
124
125 def _calc_viewport(self, level_surface, display_surface):
126 level_size = level_surface.get_size()
127 display_size = display_surface.get_size()
128 protagnist_pos = self.protagonist.physicser.get_render_position(
129 level_surface)
130 x_wide = display_size[0] // 2
131 y_wide = display_size[1] // 2
132 if display_size[0] > level_size[0]:
133 x = -(display_size[0] - level_size[0]) // 2
134 elif protagnist_pos[0] < x_wide:
135 x = 0
136 elif protagnist_pos[0] > level_size[0] - x_wide:
137 x = level_size[0] - display_size[0]
138 else:
139 x = protagnist_pos[0] - x_wide
140 if display_size[1] > level_size[1]:
141 y = -(display_size[1] - level_size[1]) // 2
142 elif protagnist_pos[1] < y_wide:
143 y = 0
144 elif protagnist_pos[1] > level_size[1] - y_wide:
145 y = level_size[1] - display_size[1]
146 else:
147 y = protagnist_pos[1] - y_wide
148 return pygame.rect.Rect(x, y, display_size[0], display_size[1])
149
150 def render(self, surface):
151 #surface.fill(pygame.color.Color(0, 0, 0))
152 background = self._level.get_background()
153 mysurface = background.copy()
154 for drawable in self._drawables:
155 drawable.render(mysurface)
156 render_rect = self._calc_viewport(mysurface, surface)
157 surface.blit(mysurface, (0, 0), render_rect)
158
159 def tick_protagonist(self):
160 dx, dy = self.keys.get_direction()
161 self.protagonist.set_direction(dx, dy)
162
163 def tick(self, seconds):
164 self.tick_protagonist()
165 super(AreaScreen, self).tick(seconds)
Note: See TracBrowser for help on using the repository browser.