1 | """Display a game area."""
|
---|
2 |
|
---|
3 | import pygame
|
---|
4 | import pymunk
|
---|
5 | import pymunk.pygame_util
|
---|
6 |
|
---|
7 | from nagslang.screens.base import Screen
|
---|
8 | from nagslang.level import Level
|
---|
9 | from nagslang.events import ScreenChange
|
---|
10 |
|
---|
11 |
|
---|
12 | class 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 |
|
---|
29 | class 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 |
|
---|
83 | class 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 | corners = self._level.get_walls()
|
---|
97 | corner = corners[-1]
|
---|
98 | for next_corner in corners:
|
---|
99 | wall = pymunk.Segment(body, corner, next_corner, 5)
|
---|
100 | wall.elasticity = 1.0
|
---|
101 | self.walls.append(wall)
|
---|
102 | corner = next_corner
|
---|
103 | self.space.add(*self.walls)
|
---|
104 |
|
---|
105 | def add_protagonist(self):
|
---|
106 | self.protagonist = Protagonist((400, 300))
|
---|
107 | self.protagonist.add_space(self.space)
|
---|
108 |
|
---|
109 | def handle_event(self, ev):
|
---|
110 | if ev.type == pygame.locals.KEYDOWN:
|
---|
111 | if ev.key == pygame.locals.K_ESCAPE:
|
---|
112 | ScreenChange.post('menu')
|
---|
113 | if ev.key == pygame.locals.K_w:
|
---|
114 | self.protagonist.toggle_form()
|
---|
115 | self.keys.handle_event(ev)
|
---|
116 |
|
---|
117 | def render(self, surface):
|
---|
118 | #surface.fill(pygame.color.Color(0, 0, 0))
|
---|
119 | background = self._level.get_background()
|
---|
120 | surface.blit(background, (0, 0))
|
---|
121 | #pymunk.pygame_util.draw(surface, *self.walls)
|
---|
122 | self.protagonist.render(surface)
|
---|
123 |
|
---|
124 | def tick_protagonist(self):
|
---|
125 | dx, dy = 0, 0
|
---|
126 | for key, tx, ty in [
|
---|
127 | (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1),
|
---|
128 | (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0)
|
---|
129 | ]:
|
---|
130 | if key in self.keys.keys_down:
|
---|
131 | dx += tx
|
---|
132 | dy += ty
|
---|
133 | self.protagonist.set_direction(dx, dy)
|
---|
134 |
|
---|
135 | def tick(self, seconds):
|
---|
136 | self.tick_protagonist()
|
---|
137 | super(AreaScreen, self).tick(seconds)
|
---|