source: nagslang/screens/area.py@ 261:db7c8e74efb4

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

(really rubbish) bullets

File size: 7.6 KB
RevLine 
[18]1"""Display a game area."""
2
[20]3import pygame
[21]4import pymunk
5import pymunk.pygame_util
[20]6
[258]7from nagslang.constants import COLLISION_TYPE_PLAYER, CALLBACK_COLLIDERS, \
8 COLLISION_TYPE_ENEMY
[261]9from nagslang.events import ScreenChange, DoorEvent, FireEvent
[50]10from nagslang.level import Level
[65]11from nagslang.protagonist import Protagonist
12from nagslang.screens.base import Screen
[261]13from nagslang.game_object import Bullet
[18]14
[83]15
[34]16class ControlKeys(object):
[100]17 direction_keys = {
18 (0, 1): set([pygame.locals.K_UP, pygame.locals.K_w]),
19 (0, -1): set([pygame.locals.K_DOWN, pygame.locals.K_s]),
20 (-1, 0): set([pygame.locals.K_LEFT, pygame.locals.K_a]),
21 (1, 0): set([pygame.locals.K_RIGHT, pygame.locals.K_d]),
22 }
23
[34]24 def __init__(self):
25 self.keys_down = set()
26
27 def key_down(self, key):
28 self.keys_down.add(key)
29
30 def key_up(self, key):
31 self.keys_down.discard(key)
32
33 def handle_event(self, ev):
34 if ev.type == pygame.locals.KEYDOWN:
35 self.key_down(ev.key)
36 elif ev.type == pygame.locals.KEYUP:
37 self.key_up(ev.key)
38
[100]39 def get_direction(self):
40 dx, dy = 0, 0
41 for (tx, ty), keys in self.direction_keys.iteritems():
42 if self.keys_down & keys:
43 dx += tx
44 dy += ty
45 return (dx, dy)
46
[34]47
[107]48class Drawables(object):
49 def __init__(self):
50 self._drawables = {}
51
52 def add(self, drawable):
53 self._drawables.setdefault(drawable.zorder, []).append(drawable)
54
55 def remove(self, drawable):
56 self._drawables[drawable.zorder].remove(drawable)
57
58 def get_drawables(self):
59 for zorder in sorted(self._drawables):
60 for drawable in self._drawables[zorder]:
61 yield drawable
62
63 __iter__ = get_drawables
64
65
[18]66class AreaScreen(Screen):
[21]67
[37]68 def setup(self):
[190]69 self._disable_render = False # Avoid redrawing on scene changes
[34]70 self.keys = ControlKeys()
[50]71 self._level = Level(self.name)
[145]72 self._level.load(self.space)
[107]73 self._drawables = Drawables()
[37]74 self.add_walls()
[188]75 self._add_collision_handlers()
[180]76 if self.protagonist is not None:
77 # We do things this way to avoid extra pymunk
78 # juggling to move objects between spaces
79 old_protagonist = self.protagonist
80 self.add_protagonist()
81 self.protagonist.copy_state(old_protagonist)
82 else:
83 self.add_protagonist()
[145]84 self.add_game_objects()
[21]85
[188]86 def _collision_pre_solve_handler(self, space, arbiter):
87 gobj = arbiter.shapes[1].physicser.game_object
[256]88 result = gobj.collide_with_protagonist(self.protagonist)
[258]89 if arbiter.shapes[1].collision_type == COLLISION_TYPE_ENEMY:
90 self.protagonist.lose_health(15)
[192]91 # The collision handler must return `True` or `False`. We don't want to
92 # accidentally reject collisions from handlers that return `None`, so
93 # we explicitly check for `False` and treate everything else as `True`.
[188]94 if result is False:
95 return False
96 return True
97
98 def _add_collision_handlers(self):
99 for collision_type in CALLBACK_COLLIDERS:
100 self.space.add_collision_handler(
101 COLLISION_TYPE_PLAYER, collision_type,
102 pre_solve=self._collision_pre_solve_handler)
103
[37]104 def add_walls(self):
[21]105 self.walls = []
106 body = pymunk.Body()
[84]107 body.position = (0, 0)
[53]108 walls = self._level.get_walls()
109 for wall in walls:
110 corners = wall
111 corner = corners[-1]
112 for next_corner in corners:
113 wall = pymunk.Segment(body, corner, next_corner, 5)
114 wall.elasticity = 1.0
115 self.walls.append(wall)
116 corner = next_corner
[21]117 self.space.add(*self.walls)
118
[145]119 def add_game_objects(self):
[191]120 for drawable in self._level.drawables:
[145]121 self._drawables.add(drawable)
122
[37]123 def add_protagonist(self):
[93]124 self.protagonist = Protagonist(self.space, (350, 300))
[107]125 self._drawables.add(self.protagonist)
[21]126
[18]127 def handle_event(self, ev):
[20]128 if ev.type == pygame.locals.KEYDOWN:
129 if ev.key == pygame.locals.K_ESCAPE:
130 ScreenChange.post('menu')
[97]131 if ev.key == pygame.locals.K_c:
[47]132 self.protagonist.toggle_form()
[238]133 self.world.transformations += 1
[261]134 if ev.key == pygame.locals.K_SPACE:
135 self.world.attacks += 1
136 self.protagonist.attack()
[180]137 elif DoorEvent.matches(ev):
138 self.protagonist.set_position(ev.dest_pos)
139 if ev.destination != self.name:
140 # Go to anther screen
[190]141 self._disable_render = True
[238]142 self.world.rooms += 1
[180]143 ScreenChange.post(ev.destination, self.protagonist)
[190]144 return
[180]145 # else we're teleporting within the screen, and just the
146 # position change is enough
[261]147 elif FireEvent.matches(ev):
148 bullet = Bullet(self.space, ev.source, ev.impulse)
149 self._drawables.add(bullet)
[34]150 self.keys.handle_event(ev)
[18]151
[87]152 def _calc_viewport(self, level_surface, display_surface):
153 level_size = level_surface.get_size()
154 display_size = display_surface.get_size()
[93]155 protagnist_pos = self.protagonist.physicser.get_render_position(
[127]156 level_surface)
[87]157 x_wide = display_size[0] // 2
158 y_wide = display_size[1] // 2
[127]159 if display_size[0] > level_size[0]:
160 x = -(display_size[0] - level_size[0]) // 2
161 elif protagnist_pos[0] < x_wide:
[87]162 x = 0
163 elif protagnist_pos[0] > level_size[0] - x_wide:
164 x = level_size[0] - display_size[0]
165 else:
166 x = protagnist_pos[0] - x_wide
[127]167 if display_size[1] > level_size[1]:
168 y = -(display_size[1] - level_size[1]) // 2
169 elif protagnist_pos[1] < y_wide:
[87]170 y = 0
171 elif protagnist_pos[1] > level_size[1] - y_wide:
172 y = level_size[1] - display_size[1]
173 else:
174 y = protagnist_pos[1] - y_wide
175 return pygame.rect.Rect(x, y, display_size[0], display_size[1])
176
[18]177 def render(self, surface):
[190]178 if self._disable_render:
179 return
[50]180 background = self._level.get_background()
[84]181 mysurface = background.copy()
[105]182 for drawable in self._drawables:
183 drawable.render(mysurface)
[87]184 render_rect = self._calc_viewport(mysurface, surface)
185 surface.blit(mysurface, (0, 0), render_rect)
[191]186 for overlay in self._level.overlay_drawables:
187 if overlay.is_visible():
[211]188 overlay.render(surface, render_rect.topleft)
[243]189 self.render_health_bar(surface)
[34]190
191 def tick_protagonist(self):
[100]192 dx, dy = self.keys.get_direction()
[47]193 self.protagonist.set_direction(dx, dy)
[34]194
195 def tick(self, seconds):
196 self.tick_protagonist()
[143]197 for drawable in self._drawables:
198 drawable.animate()
[243]199
[34]200 super(AreaScreen, self).tick(seconds)
[243]201
[258]202 def render_health_bar(self, surface, damage_experienced=None):
[243]203 rect = pygame.Rect(50, 500, 110, 50)
[258]204 if damage_experienced:
205 health_box_colour = pygame.color.THECOLORS['red']
206 else:
207 health_box_colour = pygame.color.THECOLORS['white']
[257]208 pygame.draw.rect(surface, health_box_colour, rect, 0)
[246]209 if self.protagonist.in_human_form():
[243]210 health_colour = pygame.color.THECOLORS['red']
211 else:
212 health_colour = pygame.color.THECOLORS['purple']
[246]213 rect = pygame.Rect(55, 505, self.protagonist.get_health_level(), 40)
[243]214 pygame.draw.rect(surface, health_colour,
[246]215 rect, 0)
Note: See TracBrowser for help on using the repository browser.