source: nagslang/screens/area.py@ 58:cee0b845dedc

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

Centre the wolf on its body

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[0] - wolf.get_width() / 2,
60 pos[1] - wolf.get_height() / 2))
61 else:
62 pymunk.pygame_util.draw(surface, self.shape)
63
64 def go_werewolf(self):
65 self.werewolf = True
66 self.body.mass = 100
67 self.body.moment = 10000
68 self.body.velocity_limit = 1000
69 self.shape.color = pygame.color.THECOLORS['red']
70 self.impulse_factor = 4000
71 self.damping = 0.9
72
73 def go_human(self):
74 self.werewolf = False
75 self.body.mass = 10
76 self.body.moment = 1000
77 self.body.velocity_limit = 1000
78 self.shape.color = pygame.color.THECOLORS['blue']
79 self.impulse_factor = 500
80 self.damping = 0.8
81
82 def set_direction(self, dx, dy):
83 if (dx, dy) == (0, 0):
84 return
85 vec = pymunk.Vec2d((dx, dy))
86 self.body.angle = vec.angle
87 self.body.apply_impulse(
88 (dx * self.impulse_factor, dy * self.impulse_factor))
89
90 def toggle_form(self):
91 if self.werewolf:
92 self.go_human()
93 else:
94 self.go_werewolf()
95
96
97class AreaScreen(Screen):
98
99 def setup(self):
100 self.keys = ControlKeys()
101 self._level = Level(self.name)
102 self._level.load()
103 self.add_walls()
104 self.add_protagonist()
105
106 def add_walls(self):
107 self.walls = []
108 body = pymunk.Body()
109 body.position = (0, -300)
110 walls = self._level.get_walls()
111 for wall in walls:
112 corners = wall
113 corner = corners[-1]
114 for next_corner in corners:
115 wall = pymunk.Segment(body, corner, next_corner, 5)
116 wall.elasticity = 1.0
117 self.walls.append(wall)
118 corner = next_corner
119 self.space.add(*self.walls)
120
121 def add_protagonist(self):
122 self.protagonist = Protagonist((400, 300))
123 self.protagonist.add_space(self.space)
124
125 def handle_event(self, ev):
126 if ev.type == pygame.locals.KEYDOWN:
127 if ev.key == pygame.locals.K_ESCAPE:
128 ScreenChange.post('menu')
129 if ev.key == pygame.locals.K_w:
130 self.protagonist.toggle_form()
131 self.keys.handle_event(ev)
132
133 def render(self, surface):
134 #surface.fill(pygame.color.Color(0, 0, 0))
135 background = self._level.get_background()
136 surface.blit(background, (0, 0))
137 #pymunk.pygame_util.draw(surface, *self.walls)
138 self.protagonist.render(surface)
139
140 def tick_protagonist(self):
141 dx, dy = 0, 0
142 for key, tx, ty in [
143 (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1),
144 (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0)
145 ]:
146 if key in self.keys.keys_down:
147 dx += tx
148 dy += ty
149 self.protagonist.set_direction(dx, dy)
150
151 def tick(self, seconds):
152 self.tick_protagonist()
153 super(AreaScreen, self).tick(seconds)
Note: See TracBrowser for help on using the repository browser.