source: nagslang/screens/area.py@ 61:a0399535bb7c

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

Fix import.

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