Mercurial > nagslang
annotate nagslang/screens/area.py @ 93:d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Mon, 02 Sep 2013 11:10:50 +0200 |
parents | 4c7e85906453 |
children | c177cdc41477 |
rev | line source |
---|---|
18 | 1 """Display a game area.""" |
2 | |
20 | 3 import pygame |
21 | 4 import pymunk |
5 import pymunk.pygame_util | |
20 | 6 |
65
a99ac95a2940
Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents:
64
diff
changeset
|
7 from nagslang.events import ScreenChange |
81
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
65
diff
changeset
|
8 from nagslang.game_object import FloorSwitch |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
9 from nagslang.level import Level |
65
a99ac95a2940
Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents:
64
diff
changeset
|
10 from nagslang.protagonist import Protagonist |
a99ac95a2940
Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents:
64
diff
changeset
|
11 from nagslang.screens.base import Screen |
18 | 12 |
83 | 13 |
34 | 14 class 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 | |
18 | 31 class AreaScreen(Screen): |
21 | 32 |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
33 def setup(self): |
34 | 34 self.keys = ControlKeys() |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
35 self._level = Level(self.name) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
36 self._level.load() |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
37 self.add_walls() |
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
38 self.add_protagonist() |
21 | 39 |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
40 def add_walls(self): |
21 | 41 self.walls = [] |
42 body = pymunk.Body() | |
84
ef8e799477e0
Point pymunk at a screen surface, not the display to make things saner
Neil Muller <drnlmuller@gmail.com>
parents:
83
diff
changeset
|
43 body.position = (0, 0) |
53 | 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 | |
21 | 53 self.space.add(*self.walls) |
54 | |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
55 def add_protagonist(self): |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
92
diff
changeset
|
56 self.protagonist = Protagonist(self.space, (350, 300)) |
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
92
diff
changeset
|
57 self.toy_switch = FloorSwitch(self.space, (300, 400)) |
21 | 58 |
18 | 59 def handle_event(self, ev): |
20 | 60 if ev.type == pygame.locals.KEYDOWN: |
61 if ev.key == pygame.locals.K_ESCAPE: | |
62 ScreenChange.post('menu') | |
47
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
63 if ev.key == pygame.locals.K_w: |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
64 self.protagonist.toggle_form() |
34 | 65 self.keys.handle_event(ev) |
18 | 66 |
87 | 67 def _calc_viewport(self, level_surface, display_surface): |
68 level_size = level_surface.get_size() | |
69 display_size = display_surface.get_size() | |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
92
diff
changeset
|
70 protagnist_pos = self.protagonist.physicser.get_render_position( |
87 | 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 | |
18 | 88 def render(self, surface): |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
89 #surface.fill(pygame.color.Color(0, 0, 0)) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
90 background = self._level.get_background() |
84
ef8e799477e0
Point pymunk at a screen surface, not the display to make things saner
Neil Muller <drnlmuller@gmail.com>
parents:
83
diff
changeset
|
91 mysurface = background.copy() |
86 | 92 self.toy_switch.render(mysurface) |
84
ef8e799477e0
Point pymunk at a screen surface, not the display to make things saner
Neil Muller <drnlmuller@gmail.com>
parents:
83
diff
changeset
|
93 self.protagonist.render(mysurface) |
87 | 94 render_rect = self._calc_viewport(mysurface, surface) |
95 surface.blit(mysurface, (0, 0), render_rect) | |
34 | 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 | |
47
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
106 self.protagonist.set_direction(dx, dy) |
34 | 107 |
108 def tick(self, seconds): | |
109 self.tick_protagonist() | |
110 super(AreaScreen, self).tick(seconds) |