Skip to content

Instantly share code, notes, and snippets.

@ibebrett
Created January 21, 2019 00:20
Show Gist options
  • Save ibebrett/8f122b9dd21ab0c6861c8447098cf0dd to your computer and use it in GitHub Desktop.
Save ibebrett/8f122b9dd21ab0c6861c8447098cf0dd to your computer and use it in GitHub Desktop.
# import the pygame module
import pygame
# import pygame.locals for easier access to key coordinates
from pygame.locals import *
# Define our player object and call super to give it all the properties and methods of pygame.sprite.Sprite
# The surface we draw on the screen is now a property of 'player'
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.Surface((75, 25))
self.surf.fill((255, 255, 255))
self.rect = self.surf.get_rect()
# initialize pygame
pygame.init()
# create the screen object
# here we pass it a size of 800x600
screen = pygame.display.set_mode((800, 600))
# instantiate our player; right now he's just a rectangle
player = Player()
# Variable to keep our main loop running
running = True
# Our main loop!
while running:
# for loop through the event queue
for event in pygame.event.get():
# Check for KEYDOWN event; KEYDOWN is a constant defined in pygame.locals, which we imported earlier
if event.type == KEYDOWN:
# If the Esc key has been pressed set running to false to exit the main loop
if event.key == K_ESCAPE:
running = False
# Check for QUIT event; if QUIT, set running to false
elif event.type == QUIT:
running = False
# Draw the player to the screen
screen.blit(player.surf, (400, 300))
# Update the display
pygame.display.flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment