annotate nagslang/screens/area.py @ 52:b55f1783eb6e

Tweak wall thickness and human impulse.
author Simon Cross <hodgestar@gmail.com>
date Sun, 01 Sep 2013 18:34:38 +0200
parents 94d47bfcc7bb
children 39d346467052
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
9ecb1d222ee0 Screens.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
1 """Display a game area."""
9ecb1d222ee0 Screens.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
2
20
347667c941de Hook up area.
Simon Cross <hodgestar@gmail.com>
parents: 18
diff changeset
3 import pygame
21
1b048d2a8411 Fake area.
Simon Cross <hodgestar@gmail.com>
parents: 20
diff changeset
4 import pymunk
1b048d2a8411 Fake area.
Simon Cross <hodgestar@gmail.com>
parents: 20
diff changeset
5 import pymunk.pygame_util
20
347667c941de Hook up area.
Simon Cross <hodgestar@gmail.com>
parents: 18
diff changeset
6
18
9ecb1d222ee0 Screens.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
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
347667c941de Hook up area.
Simon Cross <hodgestar@gmail.com>
parents: 18
diff changeset
9 from nagslang.events import ScreenChange
18
9ecb1d222ee0 Screens.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
10
9ecb1d222ee0 Screens.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
11
34
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
12 class ControlKeys(object):
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
13 def __init__(self):
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
14 self.keys_down = set()
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
15
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
16 def key_down(self, key):
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
17 self.keys_down.add(key)
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
18
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
19 def key_up(self, key):
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
20 self.keys_down.discard(key)
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
21
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
22 def handle_event(self, ev):
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
23 if ev.type == pygame.locals.KEYDOWN:
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
24 self.key_down(ev.key)
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
25 elif ev.type == pygame.locals.KEYUP:
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
26 self.key_up(ev.key)
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
27
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
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']
52
b55f1783eb6e Tweak wall thickness and human impulse.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
65 self.impulse_factor = 500
47
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
9ecb1d222ee0 Screens.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
83 class AreaScreen(Screen):
21
1b048d2a8411 Fake area.
Simon Cross <hodgestar@gmail.com>
parents: 20
diff changeset
84
37
4140780c21bc Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents: 35
diff changeset
85 def setup(self):
34
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
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
1b048d2a8411 Fake area.
Simon Cross <hodgestar@gmail.com>
parents: 20
diff changeset
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
1b048d2a8411 Fake area.
Simon Cross <hodgestar@gmail.com>
parents: 20
diff changeset
93 self.walls = []
1b048d2a8411 Fake area.
Simon Cross <hodgestar@gmail.com>
parents: 20
diff changeset
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
1b048d2a8411 Fake area.
Simon Cross <hodgestar@gmail.com>
parents: 20
diff changeset
97 corner = corners[-1]
1b048d2a8411 Fake area.
Simon Cross <hodgestar@gmail.com>
parents: 20
diff changeset
98 for next_corner in corners:
52
b55f1783eb6e Tweak wall thickness and human impulse.
Simon Cross <hodgestar@gmail.com>
parents: 50
diff changeset
99 wall = pymunk.Segment(body, corner, next_corner, 5)
47
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
1b048d2a8411 Fake area.
Simon Cross <hodgestar@gmail.com>
parents: 20
diff changeset
102 corner = next_corner
1b048d2a8411 Fake area.
Simon Cross <hodgestar@gmail.com>
parents: 20
diff changeset
103 self.space.add(*self.walls)
1b048d2a8411 Fake area.
Simon Cross <hodgestar@gmail.com>
parents: 20
diff changeset
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
1b048d2a8411 Fake area.
Simon Cross <hodgestar@gmail.com>
parents: 20
diff changeset
108
18
9ecb1d222ee0 Screens.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
109 def handle_event(self, ev):
20
347667c941de Hook up area.
Simon Cross <hodgestar@gmail.com>
parents: 18
diff changeset
110 if ev.type == pygame.locals.KEYDOWN:
347667c941de Hook up area.
Simon Cross <hodgestar@gmail.com>
parents: 18
diff changeset
111 if ev.key == pygame.locals.K_ESCAPE:
347667c941de Hook up area.
Simon Cross <hodgestar@gmail.com>
parents: 18
diff changeset
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
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
115 self.keys.handle_event(ev)
18
9ecb1d222ee0 Screens.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
116
9ecb1d222ee0 Screens.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
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
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
123
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
124 def tick_protagonist(self):
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
125 dx, dy = 0, 0
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
126 for key, tx, ty in [
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
127 (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1),
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
128 (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0)
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
129 ]:
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
130 if key in self.keys.keys_down:
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
131 dx += tx
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
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
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
134
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
135 def tick(self, seconds):
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
136 self.tick_protagonist()
2995723e8ccf Move and hold.
Simon Cross <hodgestar@gmail.com>
parents: 27
diff changeset
137 super(AreaScreen, self).tick(seconds)