source: nagslang/screens/area.py@ 106:bce9cd8a4a8c

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

FloorLight, linked to a FloorSwitch.

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