source: nagslang/screens/area.py@ 145:0c49627920eb

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

Load game objects from level.

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