source: nagslang/screens/area.py@ 83:d7beca6b6762

Last change on this file since 83:d7beca6b6762 was 83:d7beca6b6762, checked in by davidsharpe@…, 8 years ago

Fix whitespace again.

File size: 2.7 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
9from nagslang.level import Level
10from nagslang.protagonist import Protagonist
11from nagslang.screens.base import Screen
12
13
14class ControlKeys(object):
15 def __init__(self):
16 self.keys_down = set()
17
18 def key_down(self, key):
19 self.keys_down.add(key)
20
21 def key_up(self, key):
22 self.keys_down.discard(key)
23
24 def handle_event(self, ev):
25 if ev.type == pygame.locals.KEYDOWN:
26 self.key_down(ev.key)
27 elif ev.type == pygame.locals.KEYUP:
28 self.key_up(ev.key)
29
30
31class AreaScreen(Screen):
32
33 def setup(self):
34 self.keys = ControlKeys()
35 self._level = Level(self.name)
36 self._level.load()
37 self.add_walls()
38 self.add_protagonist()
39
40 def add_walls(self):
41 self.walls = []
42 body = pymunk.Body()
43 body.position = (0, -300)
44 walls = self._level.get_walls()
45 for wall in walls:
46 corners = wall
47 corner = corners[-1]
48 for next_corner in corners:
49 wall = pymunk.Segment(body, corner, next_corner, 5)
50 wall.elasticity = 1.0
51 self.walls.append(wall)
52 corner = next_corner
53 self.space.add(*self.walls)
54
55 def add_protagonist(self):
56 self.protagonist = Protagonist((400, 300))
57 self.protagonist.add_to_space(self.space)
58 self.toy_switch = FloorSwitch((300, 400))
59 self.toy_switch.add_to_space(self.space)
60
61 def handle_event(self, ev):
62 if ev.type == pygame.locals.KEYDOWN:
63 if ev.key == pygame.locals.K_ESCAPE:
64 ScreenChange.post('menu')
65 if ev.key == pygame.locals.K_w:
66 self.protagonist.toggle_form()
67 self.keys.handle_event(ev)
68
69 def render(self, surface):
70 #surface.fill(pygame.color.Color(0, 0, 0))
71 background = self._level.get_background()
72 surface.blit(background, (0, 0))
73 #pymunk.pygame_util.draw(surface, *self.walls)
74 self.protagonist.render(surface)
75 self.toy_switch.render(surface)
76
77 def tick_protagonist(self):
78 dx, dy = 0, 0
79 for key, tx, ty in [
80 (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1),
81 (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0)
82 ]:
83 if key in self.keys.keys_down:
84 dx += tx
85 dy += ty
86 self.protagonist.set_direction(dx, dy)
87
88 def tick(self, seconds):
89 self.tick_protagonist()
90 super(AreaScreen, self).tick(seconds)
Note: See TracBrowser for help on using the repository browser.