source: nagslang/screens/area.py@ 53:39d346467052

Last change on this file since 53:39d346467052 was 53:39d346467052, checked in by Simon Cross <hodgestar@…>, 8 years ago

Draw all the walls.

File size: 4.0 KB
Line 
1"""Display a game area."""
2
3import pygame
4import pymunk
5import pymunk.pygame_util
6
7from nagslang.screens.base import Screen
8from nagslang.level import Level
9from nagslang.events import ScreenChange
10
11
12class ControlKeys(object):
13 def __init__(self):
14 self.keys_down = set()
15
16 def key_down(self, key):
17 self.keys_down.add(key)
18
19 def key_up(self, key):
20 self.keys_down.discard(key)
21
22 def handle_event(self, ev):
23 if ev.type == pygame.locals.KEYDOWN:
24 self.key_down(ev.key)
25 elif ev.type == pygame.locals.KEYUP:
26 self.key_up(ev.key)
27
28
29class Protagonist(object):
30 def __init__(self, position):
31 self.body = pymunk.Body(10, 10000)
32 self.body.position = position
33 self.body.velocity_func = self.velocity_func
34
35 self.shape = pymunk.Circle(self.body, 30)
36 self.shape.elasticity = 1.0
37 self.shape.friction = 10.0
38
39 self.go_human()
40
41 def add_space(self, space):
42 space.add(self.body, self.shape)
43
44 def velocity_func(self, body, gravity, damping, dt):
45 return pymunk.Body.update_velocity(body, gravity, self.damping, dt)
46
47 def render(self, surface):
48 pymunk.pygame_util.draw(surface, self.shape)
49
50 def go_werewolf(self):
51 self.werewolf = True
52 self.body.mass = 100
53 self.body.moment = 10000
54 self.body.velocity_limit = 1000
55 self.shape.color = pygame.color.THECOLORS['red']
56 self.impulse_factor = 4000
57 self.damping = 0.9
58
59 def go_human(self):
60 self.werewolf = False
61 self.body.mass = 10
62 self.body.moment = 1000
63 self.body.velocity_limit = 1000
64 self.shape.color = pygame.color.THECOLORS['blue']
65 self.impulse_factor = 500
66 self.damping = 0.8
67
68 def set_direction(self, dx, dy):
69 if (dx, dy) == (0, 0):
70 return
71 vec = pymunk.Vec2d((dx, dy))
72 self.body.angle = vec.angle
73 self.body.apply_impulse(
74 (dx * self.impulse_factor, dy * self.impulse_factor))
75
76 def toggle_form(self):
77 if self.werewolf:
78 self.go_human()
79 else:
80 self.go_werewolf()
81
82
83class AreaScreen(Screen):
84
85 def setup(self):
86 self.keys = ControlKeys()
87 self._level = Level(self.name)
88 self._level.load()
89 self.add_walls()
90 self.add_protagonist()
91
92 def add_walls(self):
93 self.walls = []
94 body = pymunk.Body()
95 body.position = (0, -300)
96 walls = self._level.get_walls()
97 for wall in walls:
98 corners = wall
99 corner = corners[-1]
100 for next_corner in corners:
101 wall = pymunk.Segment(body, corner, next_corner, 5)
102 wall.elasticity = 1.0
103 self.walls.append(wall)
104 corner = next_corner
105 self.space.add(*self.walls)
106
107 def add_protagonist(self):
108 self.protagonist = Protagonist((400, 300))
109 self.protagonist.add_space(self.space)
110
111 def handle_event(self, ev):
112 if ev.type == pygame.locals.KEYDOWN:
113 if ev.key == pygame.locals.K_ESCAPE:
114 ScreenChange.post('menu')
115 if ev.key == pygame.locals.K_w:
116 self.protagonist.toggle_form()
117 self.keys.handle_event(ev)
118
119 def render(self, surface):
120 #surface.fill(pygame.color.Color(0, 0, 0))
121 background = self._level.get_background()
122 surface.blit(background, (0, 0))
123 #pymunk.pygame_util.draw(surface, *self.walls)
124 self.protagonist.render(surface)
125
126 def tick_protagonist(self):
127 dx, dy = 0, 0
128 for key, tx, ty in [
129 (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1),
130 (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0)
131 ]:
132 if key in self.keys.keys_down:
133 dx += tx
134 dy += ty
135 self.protagonist.set_direction(dx, dy)
136
137 def tick(self, seconds):
138 self.tick_protagonist()
139 super(AreaScreen, self).tick(seconds)
Note: See TracBrowser for help on using the repository browser.