source: nagslang/screens/area.py@ 97:c177cdc41477

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

Add WASD controls, switch to "c" for form change.

File size: 3.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
[83]13
[34]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
[18]31class AreaScreen(Screen):
[21]32
[37]33 def setup(self):
[34]34 self.keys = ControlKeys()
[50]35 self._level = Level(self.name)
36 self._level.load()
[37]37 self.add_walls()
38 self.add_protagonist()
[21]39
[37]40 def add_walls(self):
[21]41 self.walls = []
42 body = pymunk.Body()
[84]43 body.position = (0, 0)
[53]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
[21]53 self.space.add(*self.walls)
54
[37]55 def add_protagonist(self):
[93]56 self.protagonist = Protagonist(self.space, (350, 300))
57 self.toy_switch = FloorSwitch(self.space, (300, 400))
[21]58
[18]59 def handle_event(self, ev):
[20]60 if ev.type == pygame.locals.KEYDOWN:
61 if ev.key == pygame.locals.K_ESCAPE:
62 ScreenChange.post('menu')
[97]63 if ev.key == pygame.locals.K_c:
[47]64 self.protagonist.toggle_form()
[34]65 self.keys.handle_event(ev)
[18]66
[87]67 def _calc_viewport(self, level_surface, display_surface):
68 level_size = level_surface.get_size()
69 display_size = display_surface.get_size()
[93]70 protagnist_pos = self.protagonist.physicser.get_render_position(
[87]71 level_surface)
72 x_wide = display_size[0] // 2
73 y_wide = display_size[1] // 2
74 if protagnist_pos[0] < x_wide:
75 x = 0
76 elif protagnist_pos[0] > level_size[0] - x_wide:
77 x = level_size[0] - display_size[0]
78 else:
79 x = protagnist_pos[0] - x_wide
80 if protagnist_pos[1] < y_wide:
81 y = 0
82 elif protagnist_pos[1] > level_size[1] - y_wide:
83 y = level_size[1] - display_size[1]
84 else:
85 y = protagnist_pos[1] - y_wide
86 return pygame.rect.Rect(x, y, display_size[0], display_size[1])
87
[18]88 def render(self, surface):
[50]89 #surface.fill(pygame.color.Color(0, 0, 0))
90 background = self._level.get_background()
[84]91 mysurface = background.copy()
[86]92 self.toy_switch.render(mysurface)
[84]93 self.protagonist.render(mysurface)
[87]94 render_rect = self._calc_viewport(mysurface, surface)
95 surface.blit(mysurface, (0, 0), render_rect)
[34]96
97 def tick_protagonist(self):
98 dx, dy = 0, 0
99 for key, tx, ty in [
[97]100 # Arrows
101 (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1),
102 (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0),
103 # WASD
104 (pygame.locals.K_w, 0, 1), (pygame.locals.K_s, 0, -1),
105 (pygame.locals.K_a, -1, 0), (pygame.locals.K_d, 1, 0),
[34]106 ]:
107 if key in self.keys.keys_down:
108 dx += tx
109 dy += ty
[47]110 self.protagonist.set_direction(dx, dy)
[34]111
112 def tick(self, seconds):
113 self.tick_protagonist()
114 super(AreaScreen, self).tick(seconds)
Note: See TracBrowser for help on using the repository browser.