Personal tools
You are here: Home → Comp 171 Scripting Practicum → Course Materials → Python Materials → Python Programs → Starting into Pygame
Document Actions

Starting into Pygame

by Dr. William L. Honig — last modified 2012-04-15 12:40 PM

Pygame is a great game engine. Here's a first taste...

Click here to get the file

Size 2.6 kB - File type text/python-source

File contents

# 
#Comp 171 Standard Python Script Header 
#Assignment name: Introduction to Pygame 
#LUC id and Name whonig@luc.edu - Dr. William L. Honig 
#Date 15 April 2012 
# 
#Not really a script, but a good way to learn some more Python
#Pygame is a complete game engine
#Main Pygame site
#       http://www.pygame.org/news.html
#       download info: http://www.pygame.org/download.shtml
#       problems? this may help: http://www.python-forum.org/pythonforum/viewtopic.php?f=17&t=24994
#       tutorials: http://www.pygame.org/docs/
#RBG Colors - pick your own, experiment or check on:
#       http://www.tayloredmktg.com/rgb/
#Questions: Can you change the game a bit?
#               How would you add a sprite? Maybe a ball?
#               Make the rectangle change colors when it bounces
# 
#Acknowledements: Program adapted from
# Sample Python/Pygame Programs - Dr. Paul Vincent Craven 
# Simpson College Computer Science 
# http://cs.simpson.edu
# See the excellent introduction to Pygame programming at
#   http://cs.simpson.edu/cmsc150/index.php?chapter=example_code

import pygame

# Define some colors 
black = ( 0, 0, 0) 
white = ( 255, 255, 255) 
green = ( 0, 255, 0) 
red = ( 255, 0, 0) 
pygame.init()

# Set the height and width of the screen 
size=[700,500] 
screen=pygame.display.set_mode(size) 
pygame.display.set_caption("Bouncing Rectangle")

#Loop until the user clicks the close button. 
done=False
# Used to manage how fast the screen updates 
clock=pygame.time.Clock() 
# Starting position of the rectangle 
rect_x = 50
rect_y = 50
# Speed and direction of rectangle 
rect_change_x = 5
rect_change_y = 5

# -------- Main Program Loop ----------- 
while done==False: 
    for event in pygame.event.get(): # User did something 
        if event.type == pygame.QUIT: # If user clicked close 
            done=True # Flag that we are done so we exit this loop 
    # Set the screen background 
    screen.fill(black) 
    # Draw the rectangle 
    pygame.draw.rect(screen,white,[rect_x,rect_y,50,50]) 
    # Move the rectangle starting point 
    rect_x += rect_change_x 
    rect_y += rect_change_y 
    # Bounce the ball if needed 
    if rect_y > 450 or rect_y < 0: 
        rect_change_y = rect_change_y * -1
    if rect_x > 650 or rect_x < 0: 
        rect_change_x = rect_change_x * -1
    # Limit to 20 frames per second 
    clock.tick(20) 
    # Go ahead and update the screen with what we've drawn. 
    pygame.display.flip() 
# Be IDLE friendly. If you forget this line, the program will 'hang' 
# on exit. 
pygame.quit ()
« April 2027 »
Su Mo Tu We Th Fr Sa
123
45678910
11121314151617
18192021222324
252627282930
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: