Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python2
- import pygame
- import sys
- from player import Player
- class Game(object):
- '''
- A basic game
- '''
- FPS = 30
- '''
- The frame per second cap for the game
- '''
- def __init__(self, screen):
- '''
- Start the game
- '''
- # Initialize objects
- clock = pygame.time.Clock()
- player = Player()
- # Main game loop
- while True:
- # Cap at FPS
- dt = clock.tick(Game.FPS)
- # Check for events
- for event in pygame.event.get():
- # If the window is closed, end the game
- if event.type == pygame.QUIT:
- sys.exit(0)
- # Check for keyboard input
- if event.type == pygame.KEYDOWN:
- # Pass the key down event to the player
- if player.keydown(event):
- break
- # Key not handled; Ignore it
- elif event.type == pygame.KEYUP:
- # Pass the key up event to the player
- if player.keyup(event):
- break
- # Key not handled; Ignore it
- # Clear the buffer
- screen.fill((200, 200, 200))
- # Update the player state
- player.update(dt / 1000.0)
- # Paint the player
- player.draw(screen)
- # Flip the buffer and screen
- pygame.display.flip()
- if __name__ == "__main__":
- pygame.init()
- pygame.display.set_caption("Example")
- screen = pygame.display.set_mode((640, 480))
- Game(screen)
- #!/usr/bin/python2
- import pygame
- class Player(pygame.sprite.Sprite):
- '''
- A player instance
- '''
- def __init__(self):
- '''
- Initialize the player
- '''
- # Initialize Parent
- pygame.sprite.Sprite.__init__(self)
- # Create a singleton group for the player and add the player to it
- self.__group = pygame.sprite.GroupSingle()
- self.__group.add(self)
- # Set the image and bounds
- self.image = pygame.image.load("resources/rsffavicon.png")
- self.rect = pygame.rect.Rect((320, 240), self.image.get_size())
- # Initialize key states
- self.left = False
- self.right = False
- self.up = False
- self.down = False
- def draw(self, surface):
- '''
- Draw the player on the specified surface
- '''
- self.__group.draw(surface)
- def keydown(self, event):
- '''
- Handles a key down event.
- Returns: Whether the key has been handled
- '''
- # Initialize a default return value
- result = False
- # Check to see if we can handle the key
- if event.key in (pygame.K_a, pygame.K_LEFT):
- self.left = True
- result = True
- elif event.key in (pygame.K_d, pygame.K_RIGHT):
- self.right = True
- result = True
- elif event.key in (pygame.K_w, pygame.K_UP):
- self.up = True
- result = True
- elif event.key in (pygame.K_s, pygame.K_DOWN):
- self.down = True
- result = True
- # Return whether the key has been processed
- return result
- def keyup(self, event):
- '''
- Handles a key up event.
- Returns: Whether the key has been handled
- '''
- # Initialize a default return value
- result = False
- # Check to see if we can handle the key
- if event.key in (pygame.K_a, pygame.K_LEFT):
- self.left = False
- result = True
- elif event.key in (pygame.K_d, pygame.K_RIGHT):
- self.right = False
- result = True
- elif event.key in (pygame.K_w, pygame.K_UP):
- self.up = False
- result = True
- elif event.key in (pygame.K_s, pygame.K_DOWN):
- self.down = False
- result = True
- # Return whether the key has been processed
- return result
- def update(self, dt):
- '''
- Update the player's state
- '''
- if self.left:
- self.rect.x -= 300 * dt
- if self.right:
- self.rect.x += 300 * dt
- if self.up:
- self.rect.y -= 300 * dt
- if self.down:
- self.rect.y += 300 * dt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement