source: nagslang/screens/area.py@ 64:972142c543ef

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

Unused import.

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