source: nagslang/screens/area.py@ 133:79e1888573d3

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

A box.

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