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
RevLine 
[18]1"""Display a game area."""
2
[20]3import pygame
[21]4import pymunk
5import pymunk.pygame_util
[20]6
[107]7from nagslang.constants import ZORDER_HIGH
[65]8from nagslang.events import ScreenChange
[140]9from nagslang.game_object import (
10 FloorSwitch, FloorLight, PuzzleGlue, Box, StateLogicalAndPuzzler)
[50]11from nagslang.level import Level
[65]12from nagslang.protagonist import Protagonist
13from nagslang.screens.base import Screen
[18]14
[83]15
[34]16class ControlKeys(object):
[100]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
[34]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
[100]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
[34]47
[107]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
[18]66class AreaScreen(Screen):
[21]67
[37]68 def setup(self):
[34]69 self.keys = ControlKeys()
[50]70 self._level = Level(self.name)
71 self._level.load()
[107]72 self._drawables = Drawables()
[37]73 self.add_walls()
74 self.add_protagonist()
[133]75 self._setup_demo_objects()
76
77 def _setup_demo_objects(self):
78 # TODO: Put this in a level instead
[106]79 glue = PuzzleGlue()
80 switch = FloorSwitch(self.space, (300, 400))
[140]81 switch2 = FloorSwitch(self.space, (300, 600))
[106]82 light = FloorLight(self.space, (300, 500), 'switch')
[140]83 light2 = FloorLight(self.space, (250, 500), 'both_switches')
[107]84 light2.zorder = ZORDER_HIGH
[106]85 glue.add_component('switch', switch.puzzler)
[140]86 glue.add_component('switch2', switch2.puzzler)
[106]87 glue.add_component('light', light.puzzler)
[107]88 glue.add_component('light2', light2.puzzler)
[140]89 glue.add_component(
90 'both_switches', StateLogicalAndPuzzler('switch', 'switch2'))
[107]91 self._drawables.add(switch)
[140]92 self._drawables.add(switch2)
[107]93 self._drawables.add(light)
94 self._drawables.add(light2)
[133]95 box = Box(self.space, (250, 350))
96 self._drawables.add(box)
[21]97
[37]98 def add_walls(self):
[21]99 self.walls = []
100 body = pymunk.Body()
[84]101 body.position = (0, 0)
[53]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
[21]111 self.space.add(*self.walls)
112
[37]113 def add_protagonist(self):
[93]114 self.protagonist = Protagonist(self.space, (350, 300))
[107]115 self._drawables.add(self.protagonist)
[21]116
[18]117 def handle_event(self, ev):
[20]118 if ev.type == pygame.locals.KEYDOWN:
119 if ev.key == pygame.locals.K_ESCAPE:
120 ScreenChange.post('menu')
[97]121 if ev.key == pygame.locals.K_c:
[47]122 self.protagonist.toggle_form()
[34]123 self.keys.handle_event(ev)
[18]124
[87]125 def _calc_viewport(self, level_surface, display_surface):
126 level_size = level_surface.get_size()
127 display_size = display_surface.get_size()
[93]128 protagnist_pos = self.protagonist.physicser.get_render_position(
[127]129 level_surface)
[87]130 x_wide = display_size[0] // 2
131 y_wide = display_size[1] // 2
[127]132 if display_size[0] > level_size[0]:
133 x = -(display_size[0] - level_size[0]) // 2
134 elif protagnist_pos[0] < x_wide:
[87]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
[127]140 if display_size[1] > level_size[1]:
141 y = -(display_size[1] - level_size[1]) // 2
142 elif protagnist_pos[1] < y_wide:
[87]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
[18]150 def render(self, surface):
[50]151 #surface.fill(pygame.color.Color(0, 0, 0))
152 background = self._level.get_background()
[84]153 mysurface = background.copy()
[105]154 for drawable in self._drawables:
155 drawable.render(mysurface)
[87]156 render_rect = self._calc_viewport(mysurface, surface)
157 surface.blit(mysurface, (0, 0), render_rect)
[34]158
159 def tick_protagonist(self):
[100]160 dx, dy = self.keys.get_direction()
[47]161 self.protagonist.set_direction(dx, dy)
[34]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.