source: nagslang/screens/area.py@ 65:a99ac95a2940

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

Move protagonist object to the right place.

File size: 2.5 KB
Line 
1"""Display a game area."""
2
3import pygame
4import pymunk
5import pymunk.pygame_util
6
7from nagslang.events import ScreenChange
8from nagslang.level import Level
9from nagslang.protagonist import Protagonist
10from nagslang.screens.base import Screen
11
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
58 def handle_event(self, ev):
59 if ev.type == pygame.locals.KEYDOWN:
60 if ev.key == pygame.locals.K_ESCAPE:
61 ScreenChange.post('menu')
62 if ev.key == pygame.locals.K_w:
63 self.protagonist.toggle_form()
64 self.keys.handle_event(ev)
65
66 def render(self, surface):
67 #surface.fill(pygame.color.Color(0, 0, 0))
68 background = self._level.get_background()
69 surface.blit(background, (0, 0))
70 #pymunk.pygame_util.draw(surface, *self.walls)
71 self.protagonist.render(surface)
72
73 def tick_protagonist(self):
74 dx, dy = 0, 0
75 for key, tx, ty in [
76 (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1),
77 (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0)
78 ]:
79 if key in self.keys.keys_down:
80 dx += tx
81 dy += ty
82 self.protagonist.set_direction(dx, dy)
83
84 def tick(self, seconds):
85 self.tick_protagonist()
86 super(AreaScreen, self).tick(seconds)
Note: See TracBrowser for help on using the repository browser.