source: nagslang/screens/area.py@ 59:b412704a6737

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

Start of game object stuff.

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