source: nagslang/screens/area.py@ 96:a9b564c38bef

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

Rectangular human protagonist shape, refactored physicsers.

File size: 3.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.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(self.space, (350, 300))
57 self.toy_switch = FloorSwitch(self.space, (300, 400))
58
59 def handle_event(self, ev):
60 if ev.type == pygame.locals.KEYDOWN:
61 if ev.key == pygame.locals.K_ESCAPE:
62 ScreenChange.post('menu')
63 if ev.key == pygame.locals.K_w:
64 self.protagonist.toggle_form()
65 self.keys.handle_event(ev)
66
67 def _calc_viewport(self, level_surface, display_surface):
68 level_size = level_surface.get_size()
69 display_size = display_surface.get_size()
70 protagnist_pos = self.protagonist.physicser.get_render_position(
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
88 def render(self, surface):
89 #surface.fill(pygame.color.Color(0, 0, 0))
90 background = self._level.get_background()
91 mysurface = background.copy()
92 self.toy_switch.render(mysurface)
93 self.protagonist.render(mysurface)
94 render_rect = self._calc_viewport(mysurface, surface)
95 surface.blit(mysurface, (0, 0), render_rect)
96
97 def tick_protagonist(self):
98 dx, dy = 0, 0
99 for key, tx, ty in [
100 (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1),
101 (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0)
102 ]:
103 if key in self.keys.keys_down:
104 dx += tx
105 dy += ty
106 self.protagonist.set_direction(dx, dy)
107
108 def tick(self, seconds):
109 self.tick_protagonist()
110 super(AreaScreen, self).tick(seconds)
Note: See TracBrowser for help on using the repository browser.