Mercurial > nagslang
annotate nagslang/screens/area.py @ 37:4140780c21bc
Give screens a name and a world.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sun, 01 Sep 2013 17:06:06 +0200 |
parents | 457280af5f3a |
children | 82036437ebf6 |
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 | |
18 | 28 class AreaScreen(Screen): |
21 | 29 |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
30 def setup(self): |
34 | 31 self.keys = ControlKeys() |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
32 self.add_walls() |
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
33 self.add_protagonist() |
21 | 34 |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
35 def add_walls(self): |
21 | 36 self.walls = [] |
37 body = pymunk.Body() | |
38 body.position = (400, 300) | |
39 corners = [(-200, -150), (-200, 150), (200, 150), (200, -150)] | |
40 corner = corners[-1] | |
41 for next_corner in corners: | |
42 self.walls.append(pymunk.Segment(body, corner, next_corner, 5)) | |
43 corner = next_corner | |
44 self.space.add(*self.walls) | |
45 | |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
46 def add_protagonist(self): |
25
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
47 self.protagonist_body = pymunk.Body(100, 1000) |
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
48 self.protagonist_body.position = (400, 300) |
34 | 49 self.protagonist_body.velocity_limit = 5000 |
25
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
50 self.protagonist = pymunk.Circle(self.protagonist_body, 30) |
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
51 self.space.add(self.protagonist_body, self.protagonist) |
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
52 |
34 | 53 def protagonist_move(self, dx, dy): |
54 vec = pymunk.Vec2d((dx, dy)) | |
55 self.protagonist_body.angle = vec.angle | |
56 self.protagonist_body.apply_impulse((dx * 1000, dy * 1000)) | |
21 | 57 |
18 | 58 def handle_event(self, ev): |
20 | 59 if ev.type == pygame.locals.KEYDOWN: |
60 if ev.key == pygame.locals.K_ESCAPE: | |
61 ScreenChange.post('menu') | |
34 | 62 self.keys.handle_event(ev) |
18 | 63 |
64 def render(self, surface): | |
20 | 65 surface.fill(pygame.color.Color(0, 0, 0)) |
21 | 66 pymunk.pygame_util.draw(surface, *self.walls) |
67 pymunk.pygame_util.draw(surface, self.protagonist) | |
34 | 68 |
69 def tick_protagonist(self): | |
70 dx, dy = 0, 0 | |
71 for key, tx, ty in [ | |
72 (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1), | |
73 (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0) | |
74 ]: | |
75 if key in self.keys.keys_down: | |
76 dx += tx | |
77 dy += ty | |
78 self.protagonist_move(dx, dy) | |
79 | |
80 def tick(self, seconds): | |
81 self.tick_protagonist() | |
82 super(AreaScreen, self).tick(seconds) |