source: nagslang/screens/area.py@ 56:b9430b4a48da

Last change on this file since 56:b9430b4a48da was 56:b9430b4a48da, checked in by Stefano Rivera <stefano@…>, 8 years ago

Now with a werewolf

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