Mercurial > nagslang
annotate nagslang/screens/area.py @ 34:2995723e8ccf
Move and hold.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sun, 01 Sep 2013 16:50:06 +0200 |
parents | 3e4d8091268c |
children | 457280af5f3a |
rev | line source |
---|---|
18 | 1 """Display a game area.""" |
2 | |
27
3e4d8091268c
A werewolf always knows where she's going.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
3 import math |
3e4d8091268c
A werewolf always knows where she's going.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
4 |
20 | 5 import pygame |
21 | 6 import pymunk |
7 import pymunk.pygame_util | |
20 | 8 |
18 | 9 from nagslang.screens.base import Screen |
20 | 10 from nagslang.events import ScreenChange |
18 | 11 |
12 | |
34 | 13 class ControlKeys(object): |
14 def __init__(self): | |
15 self.keys_down = set() | |
16 | |
17 def key_down(self, key): | |
18 self.keys_down.add(key) | |
19 | |
20 def key_up(self, key): | |
21 self.keys_down.discard(key) | |
22 | |
23 def handle_event(self, ev): | |
24 if ev.type == pygame.locals.KEYDOWN: | |
25 self.key_down(ev.key) | |
26 elif ev.type == pygame.locals.KEYUP: | |
27 self.key_up(ev.key) | |
28 | |
29 | |
18 | 30 class AreaScreen(Screen): |
21 | 31 |
32 def setup(self, world): | |
34 | 33 self.keys = ControlKeys() |
21 | 34 self.add_walls(world) |
35 self.add_protagonist(world) | |
36 | |
37 def add_walls(self, world): | |
38 self.walls = [] | |
39 body = pymunk.Body() | |
40 body.position = (400, 300) | |
41 corners = [(-200, -150), (-200, 150), (200, 150), (200, -150)] | |
42 corner = corners[-1] | |
43 for next_corner in corners: | |
44 self.walls.append(pymunk.Segment(body, corner, next_corner, 5)) | |
45 corner = next_corner | |
46 self.space.add(*self.walls) | |
47 | |
48 def add_protagonist(self, world): | |
25
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
49 self.protagonist_body = pymunk.Body(100, 1000) |
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
50 self.protagonist_body.position = (400, 300) |
34 | 51 self.protagonist_body.velocity_limit = 5000 |
25
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
52 self.protagonist = pymunk.Circle(self.protagonist_body, 30) |
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
53 self.space.add(self.protagonist_body, self.protagonist) |
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
54 |
34 | 55 def protagonist_move(self, dx, dy): |
56 vec = pymunk.Vec2d((dx, dy)) | |
57 self.protagonist_body.angle = vec.angle | |
58 self.protagonist_body.apply_impulse((dx * 1000, dy * 1000)) | |
21 | 59 |
18 | 60 def handle_event(self, ev): |
20 | 61 if ev.type == pygame.locals.KEYDOWN: |
62 if ev.key == pygame.locals.K_ESCAPE: | |
63 ScreenChange.post('menu') | |
34 | 64 self.keys.handle_event(ev) |
18 | 65 |
66 def render(self, surface): | |
20 | 67 surface.fill(pygame.color.Color(0, 0, 0)) |
21 | 68 pymunk.pygame_util.draw(surface, *self.walls) |
69 pymunk.pygame_util.draw(surface, self.protagonist) | |
34 | 70 |
71 def tick_protagonist(self): | |
72 dx, dy = 0, 0 | |
73 for key, tx, ty in [ | |
74 (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1), | |
75 (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0) | |
76 ]: | |
77 if key in self.keys.keys_down: | |
78 dx += tx | |
79 dy += ty | |
80 self.protagonist_move(dx, dy) | |
81 | |
82 def tick(self, seconds): | |
83 self.tick_protagonist() | |
84 super(AreaScreen, self).tick(seconds) |