source: nagslang/screens/area.py @ 27:3e4d8091268c

Last change on this file since 27:3e4d8091268c was 27:3e4d8091268c, checked in by Simon Cross <hodgestar@…>, 7 years ago

A werewolf always knows where she's going.

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