CS 1400: Introduction to Programming
Introduction to Programming
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
CS 1400:
Introduction to Programming
More on Pygame
A10
Requirements have been reduced
find_target_in_nested_list
We won’t test with a single value as the 2nd argument
find_file_in_folder
We will only use “” as the 3rd argument
You don’t need to do anything with it
A11
Make an Avoider game
Avoider Game
Your “maze” can
be a simple image
Get creative with
your obstacles
start
end
A11
A11 is due the last day of classes (Tuesday 4/26)
Final exam is Friday 4/29
Make a Simple Game
Move around and collect powerups that randomly appear
We need:
Randomly create powerups occasionally
Keep track of and draw powerups
Collect powerups
Check if we are touching it, then remove it
Manage a Bunch of Powerups
• Hold the powerups in a list
• Just need a list of rectangles
• Same image gets blitted onto the rectangles
• Loop over the list to update positions, etc.
Make Powerups Randomly Appear – First Try
• Test
• Only one ball shows?
• Why?
• There is only one rectangle
• The list is a bunch of
references to that rectangle
• Each time we change the
position, all the references still
point to this newly positioned
rectangle
powerups
ball_rect
Make New Rectangles
golfball.get_rect() – creates a copy of the rectangle
for …:
pow = golfball.get_rect()
pow.center = …
powerups.append(pow)
powerups
pow pow pow
Make a Simple Game
Move around and collect powerups that randomly appear
We need:
Randomly create powerups occasionally
Keep track of and draw powerups
Collect powerups
Check if we are touching it, then remove it
Collisions
We need to know if our player collides with a powerup
Remember, all images are actually rectangles
Collisions
Some simple math can determine if two rectangles overlap
Problem
• Rectangles overlapping doesn’t
mean the pixels overlap
Collisions
Checking for pixel overlap is simple in principle
for pixel1 in image1:
for pixel2 in image2:
if pixel1.location == pixel2.location:
collides
Ignore transparent pixels
Collisions
Checking for pixel overlap is simple in principle
for pixel1 in image1:
for pixel2 in image2:
if pixel1.location == pixel2.location:
collides
Ignore transparent pixels
It’s a little harder in reality if you want it to be fast
Collisions
Luckily pygame supports pixel collisions
Based on the idea of a “mask”
We don’t care what color pixels are, just whether they are transparent
Original image Collision mask
Collisions
Basic idea:
Make a mask for each image
Position the mask with the rectangle for the image
Check for overlap with the masks (use provided code)
Removing from a List
Remove all items less than 5
for x in nums:
if x < 5:
nums.remove(x)
Anything wrong with this?
Removing from a List
We can’t modify a list while looping through it
Adding/removing messes with the loop
Instead, make a new list containing only the items we want to keep
Then set the powerups variable to the new list