Escolar Documentos
Profissional Documentos
Cultura Documentos
Learning Intention: sequence instructions to import, animate and control bitmap graphics
Use the snipping tool or similar to take images for your blog!
Challenge 1 importing bitmap images and controlling them using the mouse
Setting up your Window
Copy and paste on page 3 into a new Python file and check it runs it should create a blank window
(F5). the code
Creating an image
Using either Photoshop, Paint or the snipping tool (remembering to adhere to copyright) create a jpg,
gif or png image to use in your work. Remember that your window size is 700x500 when you are
creating the image. Save this in the same folder that your Python file (from the first challenge) is
saved, taking careful note of the name and file type (jpg/png/gif)
Loading an image
Just before your main loop (# -------- Main Program Loop -----------), use the line of code below to load
your image into Python, replacing my_image.jpg with the name of your image.
Completed? (add a
note if you wish)
Completed
Completed
Completed
background_image = pygame.image.load("my_image.jpg").convert()
Run your code with F5; nothing will appear to happen, but if you get an error message check that you
have saved your image file in the same folder as your python file, and that you have typed the name
correctly.
Using the image in the main loop
Weve loaded the image ready to use; next, we are going to draw the picture in the main loop using
the command below, inserted just below # PUT YOUR CODE FOR ANIMATING BENEATH THIS
COMMENT
Completed
Completed
Completed
Completed
Completed
Completed
Completed
click_sound = pygame.mixer.Sound("laser5.ogg")
We can play the sound in the game by adding the following in the while loop:
"""
Show how to use a sprite backed by a graphic.
Sample Python/Pygame Programs
Simpson College Computer Science
http://programarcadegames.com/
http://simpson.edu/computer-science/
Explanation video: http://youtu.be/vRB_983kUMc
"""
import pygame
# Define some colours using RGB values
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
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()
# -------- Main Program Loop ----------while not done:
# --- Main event loop
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
# --- Game logic should go here
# --- Drawing code should go here
# First, clear the screen to black. Don't put other drawing commands
# above this, or they will be erased with this command.
screen.fill(BLACK)
# PUT YOUR CODE FOR ANIMATING BENEATH THIS COMMENT
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()