source: nagslang/screens/area.py@ 25:e93eac7cf8c2

Last change on this file since 25:e93eac7cf8c2 was 25:e93eac7cf8c2, checked in by Simon Cross <hodgestar@…>, 8 years ago

Consider a spherical werewolf.

File size: 1.8 KB
Line 
1"""Display a game area."""
2
3import pygame
4import pymunk
5import pymunk.pygame_util
6
7from nagslang.screens.base import Screen
8from nagslang.events import ScreenChange
9
10
11class AreaScreen(Screen):
12
13 def setup(self, world):
14 self.add_walls(world)
15 self.add_protagonist(world)
16
17 def add_walls(self, world):
18 self.walls = []
19 body = pymunk.Body()
20 body.position = (400, 300)
21 corners = [(-200, -150), (-200, 150), (200, 150), (200, -150)]
22 corner = corners[-1]
23 for next_corner in corners:
24 self.walls.append(pymunk.Segment(body, corner, next_corner, 5))
25 corner = next_corner
26 self.space.add(*self.walls)
27
28 def add_protagonist(self, world):
29 self.protagonist_body = pymunk.Body(100, 1000)
30 self.protagonist_body.position = (400, 300)
31 self.protagonist = pymunk.Circle(self.protagonist_body, 30)
32 self.space.add(self.protagonist_body, self.protagonist)
33
34 def protagonist_move(self, dx, dy):
35 self.protagonist_body.apply_impulse((dx * 2000, dy * 2000))
36
37 def handle_event(self, ev):
38 if ev.type == pygame.locals.KEYDOWN:
39 if ev.key == pygame.locals.K_ESCAPE:
40 ScreenChange.post('menu')
41 elif ev.key == pygame.locals.K_UP:
42 self.protagonist_move(0, 1)
43 elif ev.key == pygame.locals.K_DOWN:
44 self.protagonist_move(0, -1)
45 elif ev.key == pygame.locals.K_LEFT:
46 self.protagonist_move(-1, 0)
47 elif ev.key == pygame.locals.K_RIGHT:
48 self.protagonist_move(1, 0)
49
50 def render(self, surface):
51 surface.fill(pygame.color.Color(0, 0, 0))
52 pymunk.pygame_util.draw(surface, *self.walls)
53 pymunk.pygame_util.draw(surface, self.protagonist)
Note: See TracBrowser for help on using the repository browser.