<-- Back to list of examples
"""
Sample Python/Pygame Programs
Simpson College Computer Science
http://programarcadegames.com/
http://simpson.edu/computer-science/
"""
import pygame
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
def draw_stick_figure(screen, x, y):
""" Draw a stickfigure at x, y """
# Head
pygame.draw.ellipse(screen, BLACK, [1 + x, y, 10, 10], 0)
# Legs
pygame.draw.line(screen, BLACK, [5 + x, 17 + y], [10 + x, 27 + y], 2)
pygame.draw.line(screen, BLACK, [5 + x, 17 + y], [x, 27 + y], 2)
# Body
pygame.draw.line(screen, RED, [5 + x, 17 + y], [5 + x, 7 + y], 2)
# Arms
pygame.draw.line(screen, RED, [5 + x, 7 + y], [9 + x, 17 + y], 2)
pygame.draw.line(screen, RED, [5 + x, 7 + y], [1 + x, 17 + y], 2)
pygame.init()
# Set the width and height of the screen [width, height]
size = [700, 500]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Current position
x_coord = 10
y_coord = 10
# Count the joysticks the computer has
joystick_count = pygame.joystick.get_count()
if joystick_count == 0:
# No joysticks!
print("Error, I didn't find any joysticks.")
else:
# Use joystick #0 and initialize it
my_joystick = pygame.joystick.Joystick(0)
my_joystick.init()
while not done:
# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT
# ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT
# As long as there is a joystick
if joystick_count != 0:
# This gets the position of the axis on the game controller
# It returns a number between -1.0 and +1.0
horiz_axis_pos = my_joystick.get_axis(0)
vert_axis_pos = my_joystick.get_axis(1)
# Move x according to the axis. We multiply by 10
# to speed up the movement.
x_coord = x_coord + int(horiz_axis_pos * 10)
y_coord = y_coord + int(vert_axis_pos * 10)
# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
# First, clear the screen to WHITE. Don't put other drawing commands
# above this, or they will be erased with this command.
screen.fill(WHITE)
# Draw the item at the proper coordinates
draw_stick_figure(screen, x_coord, y_coord)
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
pygame.display.flip()
clock.tick(60)
pygame.quit()