Você está na página 1de 3

Image and game making challenges

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

screen.blit(background_image, [0, 0])


Run your code it should now draw your imported picture on the screen! If not: time to debug!
Try changing the [0, 0] in the screen.blit command to see what effect this has.
Can you draw your imported picture a second time by copying and pasting the

Completed
Completed

screen.blit(background_image, [0, 0])


and changing the [0,0]
Mega challenge: in the last lesson, we animated an image youd drawn by declaring a variable before
the while loop, (rect_x we called it, but it could be anything you like) and then setting the position of
the shape to equal rect_x. We then incremented rect_x by 1 in the loop, using rect_x += 1. Your
challenge is to see if you can animate the imported image from todays lesson in a similar way!
Making an image follow the mouse
Add the following code below the # PUT YOUR CODE FOR ANIMATING BENEATH THIS COMMENT, and
below the code for adding your background image

Completed

Completed

# Get the current mouse position. This returns the


position as a list of two numbers.
player_position = pygame.mouse.get_pos()
x = player_position[0]
y = player_position[1]
# Copy image to screen:
screen.blit(background_image, [x, y])
Mega challenge: we have used the same image for everything so far. Can you import a second image
in the same way that we have imported the first image? The second image could be, for example, a
spaceship for your game. Copy and adapt the code that you have already used!
Mega challenge - Play a sound
Just before your main loop (# -------- Main Program Loop -----------), use the line of code below to load a
sound into Python, replacing laser5.ogg with the name of your sound (laser sound can be downloaded

Completed

Completed

from: http://programarcadegames.com/python_examples/en/laser5.ogg ). Python will play most WAV


and MP3 sound files too!

click_sound = pygame.mixer.Sound("laser5.ogg")
We can play the sound in the game by adding the following in the while loop:

elif event.type == pygame.MOUSEBUTTONDOWN:


click_sound.play()
Have a look at the screenshot below for where to put your code and how to indent it:

"""
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()

Você também pode gostar