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