Mercurial > nagslang
annotate nagslang/screens/area.py @ 27:3e4d8091268c
A werewolf always knows where she's going.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sun, 01 Sep 2013 16:03:04 +0200 |
parents | e93eac7cf8c2 |
children | 2995723e8ccf |
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 | |
13 class AreaScreen(Screen): | |
21 | 14 |
15 def setup(self, world): | |
16 self.add_walls(world) | |
17 self.add_protagonist(world) | |
18 | |
19 def add_walls(self, world): | |
20 self.walls = [] | |
21 body = pymunk.Body() | |
22 body.position = (400, 300) | |
23 corners = [(-200, -150), (-200, 150), (200, 150), (200, -150)] | |
24 corner = corners[-1] | |
25 for next_corner in corners: | |
26 self.walls.append(pymunk.Segment(body, corner, next_corner, 5)) | |
27 corner = next_corner | |
28 self.space.add(*self.walls) | |
29 | |
30 def add_protagonist(self, world): | |
25
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
31 self.protagonist_body = pymunk.Body(100, 1000) |
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
32 self.protagonist_body.position = (400, 300) |
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
33 self.protagonist = pymunk.Circle(self.protagonist_body, 30) |
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
34 self.space.add(self.protagonist_body, self.protagonist) |
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
35 |
27
3e4d8091268c
A werewolf always knows where she's going.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
36 def protagonist_move(self, dx, dy, angle): |
3e4d8091268c
A werewolf always knows where she's going.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
37 self.protagonist_body.angle = angle |
25
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
38 self.protagonist_body.apply_impulse((dx * 2000, dy * 2000)) |
21 | 39 |
18 | 40 def handle_event(self, ev): |
20 | 41 if ev.type == pygame.locals.KEYDOWN: |
42 if ev.key == pygame.locals.K_ESCAPE: | |
43 ScreenChange.post('menu') | |
25
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
44 elif ev.key == pygame.locals.K_UP: |
27
3e4d8091268c
A werewolf always knows where she's going.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
45 self.protagonist_move(0, 1, math.pi / 2) |
25
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
46 elif ev.key == pygame.locals.K_DOWN: |
27
3e4d8091268c
A werewolf always knows where she's going.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
47 self.protagonist_move(0, -1, -math.pi / 2) |
25
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
48 elif ev.key == pygame.locals.K_LEFT: |
27
3e4d8091268c
A werewolf always knows where she's going.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
49 self.protagonist_move(-1, 0, math.pi) |
25
e93eac7cf8c2
Consider a spherical werewolf.
Simon Cross <hodgestar@gmail.com>
parents:
21
diff
changeset
|
50 elif ev.key == pygame.locals.K_RIGHT: |
27
3e4d8091268c
A werewolf always knows where she's going.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
51 self.protagonist_move(1, 0, 0) |
18 | 52 |
53 def render(self, surface): | |
20 | 54 surface.fill(pygame.color.Color(0, 0, 0)) |
21 | 55 pymunk.pygame_util.draw(surface, *self.walls) |
56 pymunk.pygame_util.draw(surface, self.protagonist) |