Mercurial > nagslang
annotate nagslang/screens/area.py @ 82:11b0017b5e4b
Fix whitespace.
author | davidsharpe@185.4.16.172.in-addr.arpa |
---|---|
date | Sun, 01 Sep 2013 22:56:26 +0200 |
parents | a1b4d09e6f23 |
children | d7beca6b6762 |
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 |
65
a99ac95a2940
Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents:
64
diff
changeset
|
7 from nagslang.events import ScreenChange |
81
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
65
diff
changeset
|
8 from nagslang.game_object import FloorSwitch |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
9 from nagslang.level import Level |
65
a99ac95a2940
Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents:
64
diff
changeset
|
10 from nagslang.protagonist import Protagonist |
a99ac95a2940
Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents:
64
diff
changeset
|
11 from nagslang.screens.base import Screen |
18 | 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 |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
32 def setup(self): |
34 | 33 self.keys = ControlKeys() |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
34 self._level = Level(self.name) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
35 self._level.load() |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
36 self.add_walls() |
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
37 self.add_protagonist() |
21 | 38 |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
39 def add_walls(self): |
21 | 40 self.walls = [] |
41 body = pymunk.Body() | |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
42 body.position = (0, -300) |
53 | 43 walls = self._level.get_walls() |
44 for wall in walls: | |
45 corners = wall | |
46 corner = corners[-1] | |
47 for next_corner in corners: | |
48 wall = pymunk.Segment(body, corner, next_corner, 5) | |
49 wall.elasticity = 1.0 | |
50 self.walls.append(wall) | |
51 corner = next_corner | |
21 | 52 self.space.add(*self.walls) |
53 | |
37
4140780c21bc
Give screens a name and a world.
Simon Cross <hodgestar@gmail.com>
parents:
35
diff
changeset
|
54 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
|
55 self.protagonist = Protagonist((400, 300)) |
59
b412704a6737
Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
58
diff
changeset
|
56 self.protagonist.add_to_space(self.space) |
81
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
65
diff
changeset
|
57 self.toy_switch = FloorSwitch((300, 400)) |
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
65
diff
changeset
|
58 self.toy_switch.add_to_space(self.space) |
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') | |
47
82036437ebf6
Better movement and swap between werewolf and human form with 'w' (hodgestar, decoy).
Simon Cross <hodgestar@gmail.com>
parents:
37
diff
changeset
|
64 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
|
65 self.protagonist.toggle_form() |
34 | 66 self.keys.handle_event(ev) |
18 | 67 |
68 def render(self, surface): | |
50
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
69 #surface.fill(pygame.color.Color(0, 0, 0)) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
70 background = self._level.get_background() |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
71 surface.blit(background, (0, 0)) |
94d47bfcc7bb
Approximate levels and walls
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
72 #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
|
73 self.protagonist.render(surface) |
81
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
65
diff
changeset
|
74 self.toy_switch.render(surface) |
34 | 75 |
76 def tick_protagonist(self): | |
77 dx, dy = 0, 0 | |
78 for key, tx, ty in [ | |
79 (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1), | |
80 (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0) | |
81 ]: | |
82 if key in self.keys.keys_down: | |
83 dx += tx | |
84 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
|
85 self.protagonist.set_direction(dx, dy) |
34 | 86 |
87 def tick(self, seconds): | |
88 self.tick_protagonist() | |
89 super(AreaScreen, self).tick(seconds) |