source: nagslang/screens/area.py@ 63:7f038ee778ad

Last change on this file since 63:7f038ee778ad was 63:7f038ee778ad, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Put werewolf facing direction magic back.

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