source: nagslang/screens/area.py@ 82:11b0017b5e4b

Last change on this file since 82:11b0017b5e4b was 82:11b0017b5e4b, checked in by davidsharpe@…, 8 years ago

Fix whitespace.

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