source: nagslang/screens/area.py@ 100:96bdfadeb461

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

Cleaner direction key management.

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