source: nagslang/screens/area.py@ 84:ef8e799477e0

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

Point pymunk at a screen surface, not the display to make things saner

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, 0)
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 mysurface = background.copy()
73 #pymunk.pygame_util.draw(surface, *self.walls)
74 self.protagonist.render(mysurface)
75 self.toy_switch.render(mysurface)
76 surface.blit(mysurface, (0, 0))
77
78 def tick_protagonist(self):
79 dx, dy = 0, 0
80 for key, tx, ty in [
81 (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1),
82 (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0)
83 ]:
84 if key in self.keys.keys_down:
85 dx += tx
86 dy += ty
87 self.protagonist.set_direction(dx, dy)
88
89 def tick(self, seconds):
90 self.tick_protagonist()
91 super(AreaScreen, self).tick(seconds)
Note: See TracBrowser for help on using the repository browser.