source: nagslang/screens/area.py@ 133:79e1888573d3

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

A box.

File size: 5.1 KB
RevLine 
[18]1"""Display a game area."""
2
[20]3import pygame
[21]4import pymunk
5import pymunk.pygame_util
[20]6
[107]7from nagslang.constants import ZORDER_HIGH
[65]8from nagslang.events import ScreenChange
[133]9from nagslang.game_object import FloorSwitch, FloorLight, PuzzleGlue, Box
[50]10from nagslang.level import Level
[65]11from nagslang.protagonist import Protagonist
12from nagslang.screens.base import Screen
[18]13
[83]14
[34]15class ControlKeys(object):
[100]16 direction_keys = {
17 (0, 1): set([pygame.locals.K_UP, pygame.locals.K_w]),
18 (0, -1): set([pygame.locals.K_DOWN, pygame.locals.K_s]),
19 (-1, 0): set([pygame.locals.K_LEFT, pygame.locals.K_a]),
20 (1, 0): set([pygame.locals.K_RIGHT, pygame.locals.K_d]),
21 }
22
[34]23 def __init__(self):
24 self.keys_down = set()
25
26 def key_down(self, key):
27 self.keys_down.add(key)
28
29 def key_up(self, key):
30 self.keys_down.discard(key)
31
32 def handle_event(self, ev):
33 if ev.type == pygame.locals.KEYDOWN:
34 self.key_down(ev.key)
35 elif ev.type == pygame.locals.KEYUP:
36 self.key_up(ev.key)
37
[100]38 def get_direction(self):
39 dx, dy = 0, 0
40 for (tx, ty), keys in self.direction_keys.iteritems():
41 if self.keys_down & keys:
42 dx += tx
43 dy += ty
44 return (dx, dy)
45
[34]46
[107]47class Drawables(object):
48 def __init__(self):
49 self._drawables = {}
50
51 def add(self, drawable):
52 self._drawables.setdefault(drawable.zorder, []).append(drawable)
53
54 def remove(self, drawable):
55 self._drawables[drawable.zorder].remove(drawable)
56
57 def get_drawables(self):
58 for zorder in sorted(self._drawables):
59 for drawable in self._drawables[zorder]:
60 yield drawable
61
62 __iter__ = get_drawables
63
64
[18]65class AreaScreen(Screen):
[21]66
[37]67 def setup(self):
[34]68 self.keys = ControlKeys()
[50]69 self._level = Level(self.name)
70 self._level.load()
[107]71 self._drawables = Drawables()
[37]72 self.add_walls()
73 self.add_protagonist()
[133]74 self._setup_demo_objects()
75
76 def _setup_demo_objects(self):
77 # TODO: Put this in a level instead
[106]78 glue = PuzzleGlue()
79 switch = FloorSwitch(self.space, (300, 400))
80 light = FloorLight(self.space, (300, 500), 'switch')
[107]81 light2 = FloorLight(self.space, (250, 500), 'switch')
82 light2.zorder = ZORDER_HIGH
[106]83 glue.add_component('switch', switch.puzzler)
84 glue.add_component('light', light.puzzler)
[107]85 glue.add_component('light2', light2.puzzler)
86 self._drawables.add(switch)
87 self._drawables.add(light)
88 self._drawables.add(light2)
[133]89 box = Box(self.space, (250, 350))
90 self._drawables.add(box)
[21]91
[37]92 def add_walls(self):
[21]93 self.walls = []
94 body = pymunk.Body()
[84]95 body.position = (0, 0)
[53]96 walls = self._level.get_walls()
97 for wall in walls:
98 corners = wall
99 corner = corners[-1]
100 for next_corner in corners:
101 wall = pymunk.Segment(body, corner, next_corner, 5)
102 wall.elasticity = 1.0
103 self.walls.append(wall)
104 corner = next_corner
[21]105 self.space.add(*self.walls)
106
[37]107 def add_protagonist(self):
[93]108 self.protagonist = Protagonist(self.space, (350, 300))
[107]109 self._drawables.add(self.protagonist)
[21]110
[18]111 def handle_event(self, ev):
[20]112 if ev.type == pygame.locals.KEYDOWN:
113 if ev.key == pygame.locals.K_ESCAPE:
114 ScreenChange.post('menu')
[97]115 if ev.key == pygame.locals.K_c:
[47]116 self.protagonist.toggle_form()
[34]117 self.keys.handle_event(ev)
[18]118
[87]119 def _calc_viewport(self, level_surface, display_surface):
120 level_size = level_surface.get_size()
121 display_size = display_surface.get_size()
[93]122 protagnist_pos = self.protagonist.physicser.get_render_position(
[127]123 level_surface)
[87]124 x_wide = display_size[0] // 2
125 y_wide = display_size[1] // 2
[127]126 if display_size[0] > level_size[0]:
127 x = -(display_size[0] - level_size[0]) // 2
128 elif protagnist_pos[0] < x_wide:
[87]129 x = 0
130 elif protagnist_pos[0] > level_size[0] - x_wide:
131 x = level_size[0] - display_size[0]
132 else:
133 x = protagnist_pos[0] - x_wide
[127]134 if display_size[1] > level_size[1]:
135 y = -(display_size[1] - level_size[1]) // 2
136 elif protagnist_pos[1] < y_wide:
[87]137 y = 0
138 elif protagnist_pos[1] > level_size[1] - y_wide:
139 y = level_size[1] - display_size[1]
140 else:
141 y = protagnist_pos[1] - y_wide
142 return pygame.rect.Rect(x, y, display_size[0], display_size[1])
143
[18]144 def render(self, surface):
[50]145 #surface.fill(pygame.color.Color(0, 0, 0))
146 background = self._level.get_background()
[84]147 mysurface = background.copy()
[105]148 for drawable in self._drawables:
149 drawable.render(mysurface)
[87]150 render_rect = self._calc_viewport(mysurface, surface)
151 surface.blit(mysurface, (0, 0), render_rect)
[34]152
153 def tick_protagonist(self):
[100]154 dx, dy = self.keys.get_direction()
[47]155 self.protagonist.set_direction(dx, dy)
[34]156
157 def tick(self, seconds):
158 self.tick_protagonist()
159 super(AreaScreen, self).tick(seconds)
Note: See TracBrowser for help on using the repository browser.