Mercurial > nagslang
annotate nagslang/screens/area.py @ 64:972142c543ef
Unused import.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sun, 01 Sep 2013 19:18:45 +0200 |
parents | 7f038ee778ad |
children | a99ac95a2940 |
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 |
59
b412704a6737
Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
58
diff
changeset
|
7 from nagslang.game_object import ( |
63
7f038ee778ad
Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents:
61
diff
changeset
|
8 GameObject, SingleShapePhysicser, FacingImageRenderer) |
18 | 9 from nagslang.screens.base import Screen |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
10 from nagslang.level import Level |
20 | 11 from nagslang.events import ScreenChange |
56 | 12 from nagslang.resources import resources |
13 from nagslang.mutators import FLIP_H | |
18 | 14 |
15 | |
34 | 16 class ControlKeys(object): |
17 def __init__(self): | |
18 self.keys_down = set() | |
19 | |
20 def key_down(self, key): | |
21 self.keys_down.add(key) | |
22 | |
23 def key_up(self, key): | |
24 self.keys_down.discard(key) | |
25 | |
26 def handle_event(self, ev): | |
27 if ev.type == pygame.locals.KEYDOWN: | |
28 self.key_down(ev.key) | |
29 elif ev.type == pygame.locals.KEYUP: | |
30 self.key_up(ev.key) | |
31 | |
32 | |
59
b412704a6737
Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
58
diff
changeset
|
33 class Protagonist(GameObject): |
47
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
34 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
|
35 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
|
36 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
|
37 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
|
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.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
|
40 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
|
41 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
|
42 |
59
b412704a6737
Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
58
diff
changeset
|
43 super(Protagonist, self).__init__( |
63
7f038ee778ad
Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents:
61
diff
changeset
|
44 FacingImageRenderer( |
7f038ee778ad
Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents:
61
diff
changeset
|
45 resources.get_image('creatures', 'werewolf_1.png'), |
7f038ee778ad
Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents:
61
diff
changeset
|
46 resources.get_image( |
7f038ee778ad
Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents:
61
diff
changeset
|
47 'creatures', 'werewolf_1.png', transforms=(FLIP_H,))), |
59
b412704a6737
Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
58
diff
changeset
|
48 SingleShapePhysicser(self.shape)) |
47
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
49 |
59
b412704a6737
Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
58
diff
changeset
|
50 self.go_human() |
47
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
51 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
52 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
|
53 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
|
54 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 self.shape.color = pygame.color.THECOLORS['blue'] |
52
b55f1783eb6e
Tweak wall thickness and human impulse.
Simon Cross <hodgestar@gmail.com>
parents:
50
diff
changeset
|
70 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
|
71 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
|
72 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
73 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
|
74 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
|
75 return |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
76 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
|
77 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
|
78 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
|
79 (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
|
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 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
|
82 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
|
83 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
|
84 else: |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
85 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
|
86 |
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
87 |
18 | 88 class AreaScreen(Screen): |
21 | 89 |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
90 def setup(self): |
34 | 91 self.keys = ControlKeys() |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
92 self._level = Level(self.name) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
93 self._level.load() |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
94 self.add_walls() |
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
95 self.add_protagonist() |
21 | 96 |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
97 def add_walls(self): |
21 | 98 self.walls = [] |
99 body = pymunk.Body() | |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
100 body.position = (0, -300) |
53 | 101 walls = self._level.get_walls() |
102 for wall in walls: | |
103 corners = wall | |
104 corner = corners[-1] | |
105 for next_corner in corners: | |
106 wall = pymunk.Segment(body, corner, next_corner, 5) | |
107 wall.elasticity = 1.0 | |
108 self.walls.append(wall) | |
109 corner = next_corner | |
21 | 110 self.space.add(*self.walls) |
111 | |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
112 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
|
113 self.protagonist = Protagonist((400, 300)) |
59
b412704a6737
Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
58
diff
changeset
|
114 self.protagonist.add_to_space(self.space) |
21 | 115 |
18 | 116 def handle_event(self, ev): |
20 | 117 if ev.type == pygame.locals.KEYDOWN: |
118 if ev.key == pygame.locals.K_ESCAPE: | |
119 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
|
120 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
|
121 self.protagonist.toggle_form() |
34 | 122 self.keys.handle_event(ev) |
18 | 123 |
124 def render(self, surface): | |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
125 #surface.fill(pygame.color.Color(0, 0, 0)) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
126 background = self._level.get_background() |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
127 surface.blit(background, (0, 0)) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
128 #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
|
129 self.protagonist.render(surface) |
34 | 130 |
131 def tick_protagonist(self): | |
132 dx, dy = 0, 0 | |
133 for key, tx, ty in [ | |
134 (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1), | |
135 (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0) | |
136 ]: | |
137 if key in self.keys.keys_down: | |
138 dx += tx | |
139 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
|
140 self.protagonist.set_direction(dx, dy) |
34 | 141 |
142 def tick(self, seconds): | |
143 self.tick_protagonist() | |
144 super(AreaScreen, self).tick(seconds) |