source: nagslang/screens/area.py@ 92:4c7e85906453

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

Start next to the wall, not in it.

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((350, 300))
57 self.protagonist.add_to_space(self.space)
58 self.toy_switch = FloorSwitch((300, 400))
59 self.toy_switch.add_to_space(self.space)
60
61 def handle_event(self, ev):
62 if ev.type == pygame.locals.KEYDOWN:
63 if ev.key == pygame.locals.K_ESCAPE:
64 ScreenChange.post('menu')
65 if ev.key == pygame.locals.K_w:
66 self.protagonist.toggle_form()
67 self.keys.handle_event(ev)
68
69 def _calc_viewport(self, level_surface, display_surface):
70 level_size = level_surface.get_size()
71 display_size = display_surface.get_size()
72 protagnist_pos = self.protagonist.physicser.render_position(
73 level_surface)
74 x_wide = display_size[0] // 2
75 y_wide = display_size[1] // 2
76 if protagnist_pos[0] < x_wide:
77 x = 0
78 elif protagnist_pos[0] > level_size[0] - x_wide:
79 x = level_size[0] - display_size[0]
80 else:
81 x = protagnist_pos[0] - x_wide
82 if protagnist_pos[1] < y_wide:
83 y = 0
84 elif protagnist_pos[1] > level_size[1] - y_wide:
85 y = level_size[1] - display_size[1]
86 else:
87 y = protagnist_pos[1] - y_wide
88 return pygame.rect.Rect(x, y, display_size[0], display_size[1])
89
90 def render(self, surface):
91 #surface.fill(pygame.color.Color(0, 0, 0))
92 background = self._level.get_background()
93 mysurface = background.copy()
94 self.toy_switch.render(mysurface)
95 self.protagonist.render(mysurface)
96 render_rect = self._calc_viewport(mysurface, surface)
97 surface.blit(mysurface, (0, 0), render_rect)
98
99 def tick_protagonist(self):
100 dx, dy = 0, 0
101 for key, tx, ty in [
102 (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1),
103 (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0)
104 ]:
105 if key in self.keys.keys_down:
106 dx += tx
107 dy += ty
108 self.protagonist.set_direction(dx, dy)
109
110 def tick(self, seconds):
111 self.tick_protagonist()
112 super(AreaScreen, self).tick(seconds)
Note: See TracBrowser for help on using the repository browser.