Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Mekire
file 94 lines (77 sloc) 2.996 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
"""
This script implements a basic sprite that can move in all 8 directions.

-Written by Sean J. McKiernan 'Mekire'
"""

import os
import sys
import pygame as pg


SCREEN_SIZE = (500, 500)

WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLACK = (0, 0, 0)
TRANSPARENT = (0, 0, 0, 0)

#This global constant serves as a very useful convenience for me.
DIRECT_DICT = {pg.K_LEFT : (-1, 0),
               pg.K_RIGHT : ( 1, 0),
               pg.K_UP : ( 0,-1),
               pg.K_DOWN : ( 0, 1)}


class Player(object):
    """This class will represent our user controlled character."""
    def __init__(self, rect, speed):
        """The rect arg is (x, y, width, height); speed is in pixels/frame)"""
        self.rect = pg.Rect(rect)
        self.speed = speed
        self.image = self.make_image()

    def make_image(self):
        """Creates our hero (a red circle/ellipse with a black outline)."""
        image = pg.Surface(self.rect.size).convert_alpha()
        image.fill(TRANSPARENT)
        image_rect = image.get_rect()
        pg.draw.ellipse(image, BLACK, image_rect)
        pg.draw.ellipse(image, RED, image_rect.inflate(-12, -12))
        return image

    def update(self, screen_rect, keys):
        """Updates our player appropriately every frame."""
        for key in DIRECT_DICT:
            if keys[key]:
                self.rect.x += DIRECT_DICT[key][0]*self.speed
                self.rect.y += DIRECT_DICT[key][1]*self.speed
        self.rect.clamp_ip(screen_rect) #Keep player on screen.

    def draw(self, surface):
        """Blit image to the target surface."""
        surface.blit(self.image, self.rect)


class Control(object):
    """Keep things under control."""
    def __init__(self):
        """Initializing in the init; how novel."""
        os.environ['SDL_VIDEO_CENTERED'] = '1'
        pg.init()
        pg.display.set_caption("Move me with the Arrow Keys.")
        self.screen = pg.display.set_mode(SCREEN_SIZE)
        self.screen_rect = self.screen.get_rect()
        self.clock = pg.time.Clock()
        self.fps = 60.0
        self.done = False
        self.keys = pg.key.get_pressed()
        self.player = Player((0, 0, 100, 100), 5) #Create player instance.
        self.player.rect.center = self.screen_rect.center

    def event_loop(self):
        """One event loop. Never cut your game off from the event loop."""
        for event in pg.event.get():
            self.keys = pg.key.get_pressed()
            if event.type == pg.QUIT or self.keys[pg.K_ESCAPE]:
                self.done = True

    def main_loop(self):
        """One game loop. Simple and clean."""
        while not self.done:
            self.event_loop()
            self.player.update(self.screen_rect, self.keys)
            self.screen.fill(WHITE)
            self.player.draw(self.screen)
            pg.display.update()
            self.clock.tick(self.fps)


if __name__ == "__main__":
    run_it = Control()
    run_it.main_loop()
    pg.quit()
    sys.exit()
Something went wrong with that request. Please try again.