source: nagslang/screens/area.py@ 105:0131e4606e1a

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

List of drawables in area.

File size: 3.8 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._drawables = []
53 self.add_walls()
54 self.add_protagonist()
55 # TODO: Put this in a level instead:
56 self._drawables.append(FloorSwitch(self.space, (300, 400)))
57
58 def add_walls(self):
59 self.walls = []
60 body = pymunk.Body()
61 body.position = (0, 0)
62 walls = self._level.get_walls()
63 for wall in walls:
64 corners = wall
65 corner = corners[-1]
66 for next_corner in corners:
67 wall = pymunk.Segment(body, corner, next_corner, 5)
68 wall.elasticity = 1.0
69 self.walls.append(wall)
70 corner = next_corner
71 self.space.add(*self.walls)
72
73 def add_protagonist(self):
74 self.protagonist = Protagonist(self.space, (350, 300))
75 self._drawables.append(self.protagonist)
76
77 def handle_event(self, ev):
78 if ev.type == pygame.locals.KEYDOWN:
79 if ev.key == pygame.locals.K_ESCAPE:
80 ScreenChange.post('menu')
81 if ev.key == pygame.locals.K_c:
82 self.protagonist.toggle_form()
83 self.keys.handle_event(ev)
84
85 def _calc_viewport(self, level_surface, display_surface):
86 level_size = level_surface.get_size()
87 display_size = display_surface.get_size()
88 protagnist_pos = self.protagonist.physicser.get_render_position(
89 level_surface)
90 x_wide = display_size[0] // 2
91 y_wide = display_size[1] // 2
92 if protagnist_pos[0] < x_wide:
93 x = 0
94 elif protagnist_pos[0] > level_size[0] - x_wide:
95 x = level_size[0] - display_size[0]
96 else:
97 x = protagnist_pos[0] - x_wide
98 if protagnist_pos[1] < y_wide:
99 y = 0
100 elif protagnist_pos[1] > level_size[1] - y_wide:
101 y = level_size[1] - display_size[1]
102 else:
103 y = protagnist_pos[1] - y_wide
104 return pygame.rect.Rect(x, y, display_size[0], display_size[1])
105
106 def render(self, surface):
107 #surface.fill(pygame.color.Color(0, 0, 0))
108 background = self._level.get_background()
109 mysurface = background.copy()
110 for drawable in self._drawables:
111 drawable.render(mysurface)
112 render_rect = self._calc_viewport(mysurface, surface)
113 surface.blit(mysurface, (0, 0), render_rect)
114
115 def tick_protagonist(self):
116 dx, dy = self.keys.get_direction()
117 self.protagonist.set_direction(dx, dy)
118
119 def tick(self, seconds):
120 self.tick_protagonist()
121 super(AreaScreen, self).tick(seconds)
Note: See TracBrowser for help on using the repository browser.