source: nagslang/screens/area.py@ 127:fe1f0bb4ecf0

Last change on this file since 127:fe1f0bb4ecf0 was 127:fe1f0bb4ecf0, checked in by Neil Muller <drnlmuller@…>, 8 years ago

Handle case of display larger than the level better

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