Changeset 34:2995723e8ccf


Ignore:
Timestamp:
Sep 1, 2013, 2:50:06 PM (7 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
rebase_source:
8868dabb7bb3d38916d4b5a540fce7aab2d8a376
Message:

Move and hold.

Location:
nagslang/screens
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nagslang/screens/area.py

    r27 r34  
    1111
    1212
     13class ControlKeys(object):
     14    def __init__(self):
     15        self.keys_down = set()
     16
     17    def key_down(self, key):
     18        self.keys_down.add(key)
     19
     20    def key_up(self, key):
     21        self.keys_down.discard(key)
     22
     23    def handle_event(self, ev):
     24        if ev.type == pygame.locals.KEYDOWN:
     25            self.key_down(ev.key)
     26        elif ev.type == pygame.locals.KEYUP:
     27            self.key_up(ev.key)
     28
     29
    1330class AreaScreen(Screen):
    1431
    1532    def setup(self, world):
     33        self.keys = ControlKeys()
    1634        self.add_walls(world)
    1735        self.add_protagonist(world)
     
    3149        self.protagonist_body = pymunk.Body(100, 1000)
    3250        self.protagonist_body.position = (400, 300)
     51        self.protagonist_body.velocity_limit = 5000
    3352        self.protagonist = pymunk.Circle(self.protagonist_body, 30)
    3453        self.space.add(self.protagonist_body, self.protagonist)
    3554
    36     def protagonist_move(self, dx, dy, angle):
    37         self.protagonist_body.angle = angle
    38         self.protagonist_body.apply_impulse((dx * 2000, dy * 2000))
     55    def protagonist_move(self, dx, dy):
     56        vec = pymunk.Vec2d((dx, dy))
     57        self.protagonist_body.angle = vec.angle
     58        self.protagonist_body.apply_impulse((dx * 1000, dy * 1000))
    3959
    4060    def handle_event(self, ev):
     
    4262            if ev.key == pygame.locals.K_ESCAPE:
    4363                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)
     64        self.keys.handle_event(ev)
    5265
    5366    def render(self, surface):
     
    5568        pymunk.pygame_util.draw(surface, *self.walls)
    5669        pymunk.pygame_util.draw(surface, self.protagonist)
     70
     71    def tick_protagonist(self):
     72        dx, dy = 0, 0
     73        for key, tx, ty in [
     74            (pygame.locals.K_UP, 0, 1), (pygame.locals.K_DOWN, 0, -1),
     75            (pygame.locals.K_LEFT, -1, 0), (pygame.locals.K_RIGHT, 1, 0)
     76        ]:
     77            if key in self.keys.keys_down:
     78                dx += tx
     79                dy += ty
     80        self.protagonist_move(dx, dy)
     81
     82    def tick(self, seconds):
     83        self.tick_protagonist()
     84        super(AreaScreen, self).tick(seconds)
  • nagslang/screens/base.py

    r20 r34  
    2626        pass
    2727
    28     def tick(self, step):
     28    def tick(self, seconds):
    2929        """Step the given amount of time."""
    30         self.space.step(step)
     30        self.space.step(seconds)
Note: See TracChangeset for help on using the changeset viewer.