source: nagslang/screens/area.py@ 81:a1b4d09e6f23

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

Floor switch with horrible hackery.

File size: 2.7 KB
RevLine 
[18]1"""Display a game area."""
2
[20]3import pygame
[21]4import pymunk
5import pymunk.pygame_util
[20]6
[65]7from nagslang.events import ScreenChange
[81]8from nagslang.game_object import FloorSwitch
[50]9from nagslang.level import Level
[65]10from nagslang.protagonist import Protagonist
11from nagslang.screens.base import Screen
[18]12
[34]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
[18]30class AreaScreen(Screen):
[21]31
[37]32 def setup(self):
[34]33 self.keys = ControlKeys()
[50]34 self._level = Level(self.name)
35 self._level.load()
[37]36 self.add_walls()
37 self.add_protagonist()
[21]38
[37]39 def add_walls(self):
[21]40 self.walls = []
41 body = pymunk.Body()
[50]42 body.position = (0, -300)
[53]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
[21]52 self.space.add(*self.walls)
53
[37]54 def add_protagonist(self):
[47]55 self.protagonist = Protagonist((400, 300))
[59]56 self.protagonist.add_to_space(self.space)
[81]57 self.toy_switch = FloorSwitch((300, 400))
58 self.toy_switch.add_to_space(self.space)
[21]59
[18]60 def handle_event(self, ev):
[20]61 if ev.type == pygame.locals.KEYDOWN:
62 if ev.key == pygame.locals.K_ESCAPE:
63 ScreenChange.post('menu')
[47]64 if ev.key == pygame.locals.K_w:
65 self.protagonist.toggle_form()
[34]66 self.keys.handle_event(ev)
[18]67
68 def render(self, surface):
[50]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)
[47]73 self.protagonist.render(surface)
[81]74 self.toy_switch.render(surface)
[34]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
[47]85 self.protagonist.set_direction(dx, dy)
[34]86
87 def tick(self, seconds):
88 self.tick_protagonist()
89 super(AreaScreen, self).tick(seconds)
[81]90
Note: See TracBrowser for help on using the repository browser.