Mercurial > nagslang
annotate nagslang/screens/area.py @ 50:94d47bfcc7bb
Approximate levels and walls
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sun, 01 Sep 2013 18:28:33 +0200 |
parents | 82036437ebf6 |
children | b55f1783eb6e |
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 |
18 | 7 from nagslang.screens.base import Screen |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
8 from nagslang.level import Level |
20 | 9 from nagslang.events import ScreenChange |
18 | 10 |
11 | |
34 | 12 class ControlKeys(object): |
13 def __init__(self): | |
14 self.keys_down = set() | |
15 | |
16 def key_down(self, key): | |
17 self.keys_down.add(key) | |
18 | |
19 def key_up(self, key): | |
20 self.keys_down.discard(key) | |
21 | |
22 def handle_event(self, ev): | |
23 if ev.type == pygame.locals.KEYDOWN: | |
24 self.key_down(ev.key) | |
25 elif ev.type == pygame.locals.KEYUP: | |
26 self.key_up(ev.key) | |
27 | |
28 | |
47
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
29 class Protagonist(object): |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
30 def __init__(self, position): |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
31 self.body = pymunk.Body(10, 10000) |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
32 self.body.position = position |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
33 self.body.velocity_func = self.velocity_func |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
34 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
35 self.shape = pymunk.Circle(self.body, 30) |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
36 self.shape.elasticity = 1.0 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
37 self.shape.friction = 10.0 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
38 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
39 self.go_human() |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
40 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
41 def add_space(self, space): |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
42 space.add(self.body, self.shape) |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
43 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
44 def velocity_func(self, body, gravity, damping, dt): |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
45 return pymunk.Body.update_velocity(body, gravity, self.damping, dt) |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
46 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
47 def render(self, surface): |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
48 pymunk.pygame_util.draw(surface, self.shape) |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
49 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
50 def go_werewolf(self): |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
51 self.werewolf = True |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
52 self.body.mass = 100 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
53 self.body.moment = 10000 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
54 self.body.velocity_limit = 1000 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
55 self.shape.color = pygame.color.THECOLORS['red'] |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
56 self.impulse_factor = 4000 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
57 self.damping = 0.9 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
58 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
59 def go_human(self): |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
60 self.werewolf = False |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
61 self.body.mass = 10 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
62 self.body.moment = 1000 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
63 self.body.velocity_limit = 1000 |
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.shape.color = pygame.color.THECOLORS['blue'] |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
65 self.impulse_factor = 200 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
66 self.damping = 0.8 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
67 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
68 def set_direction(self, dx, dy): |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
69 if (dx, dy) == (0, 0): |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
70 return |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
71 vec = pymunk.Vec2d((dx, dy)) |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
72 self.body.angle = vec.angle |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
73 self.body.apply_impulse( |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
74 (dx * self.impulse_factor, dy * self.impulse_factor)) |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
75 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
76 def toggle_form(self): |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
77 if self.werewolf: |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
78 self.go_human() |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
79 else: |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
80 self.go_werewolf() |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
81 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
82 |
18 | 83 class AreaScreen(Screen): |
21 | 84 |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
85 def setup(self): |
34 | 86 self.keys = ControlKeys() |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
87 self._level = Level(self.name) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
88 self._level.load() |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
89 self.add_walls() |
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
90 self.add_protagonist() |
21 | 91 |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
92 def add_walls(self): |
21 | 93 self.walls = [] |
94 body = pymunk.Body() | |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
95 body.position = (0, -300) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
96 corners = self._level.get_walls() |
21 | 97 corner = corners[-1] |
98 for next_corner in corners: | |
47
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
99 wall = pymunk.Segment(body, corner, next_corner, 50) |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
100 wall.elasticity = 1.0 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
101 self.walls.append(wall) |
21 | 102 corner = next_corner |
103 self.space.add(*self.walls) | |
104 | |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
105 def add_protagonist(self): |
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 = Protagonist((400, 300)) |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
107 self.protagonist.add_space(self.space) |
21 | 108 |
18 | 109 def handle_event(self, ev): |
20 | 110 if ev.type == pygame.locals.KEYDOWN: |
111 if ev.key == pygame.locals.K_ESCAPE: | |
112 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
|
113 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
|
114 self.protagonist.toggle_form() |
34 | 115 self.keys.handle_event(ev) |
18 | 116 |
117 def render(self, surface): | |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
118 #surface.fill(pygame.color.Color(0, 0, 0)) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
119 background = self._level.get_background() |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
120 surface.blit(background, (0, 0)) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
121 #pymunk.pygame_util.draw(surface, *self.walls) |
47
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
122 self.protagonist.render(surface) |
34 | 123 |
124 def tick_protagonist(self): | |
125 dx, dy = 0, 0 | |
126 for key, tx, ty in [ | |
127 (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1), | |
128 (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0) | |
129 ]: | |
130 if key in self.keys.keys_down: | |
131 dx += tx | |
132 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
|
133 self.protagonist.set_direction(dx, dy) |
34 | 134 |
135 def tick(self, seconds): | |
136 self.tick_protagonist() | |
137 super(AreaScreen, self).tick(seconds) |