Summary
Things You Have Learn Today
- How to create a single costume sprite
- Three types of events
when_game_startwhen_this_sprite_clickedwhen_backdrop_switched
- Flow Control
- Using loops with
yield - Using
if/elsewith Boolean variables (hexagon blocks)
- Using loops with
- Shared Variables (and the display)
- Backdrop & Sound
- Most of the functions that correspond to a Scratch block except
- Those that reference to other sprites (day 2)
- The other events that hasn’t been mentioned yet (day 3)
Yet to Learn
- Sprites with multiple costume
- Blocks that references to another sprite (such as
is_touching) - Other events
Exercises: Improve the Game
1. Add a targeting crosshair to the cursor
Easy - LV1
- Put the crosshair image to the asset folder
- Create the sprite with the image and import it to
main.py - Add a
when_game_startevent, with a forever loop:- Move the sprite to the cursor
- Yield for one frame (or 1/60 second)
- Optional: Bring the sprite to the front layer in the
when_game_startevent before the loop
Optional: Add these two lines on the top of `main.py` to hide the mouse
import pygame
pygame.mouse.set_visible(False)
Outcome
2. Add visual and audio effects for clicking the friend
Easy - LV1
- Put a audio file to the asset folder
- Add a
when_this_sprite_clickedevent for the friend- Play the sound
- In a repeat (
for) loop, flash the friend by showing and hiding it.
Outcome
3. Add words at the end of the game
Intermediate - LV2
- Create an image with the word “You Lose” or “Game Over” using Piskel
- Create a sprite with the image and hide it on game start
- Show the sprite when the game switch to the backdrop
"lose"
Outcome
4. Spin the enemy when clicked
Hard - LV3
To spin the enemy, you need to increment the enemy’s direction inside the repeat loop of the when_this_sprite_clicked event.
However, we already have a forever loop that continuously changes the enemy’s direction to control its movements. This will intervene the spinning of the enemy.
Therefore, you need to create a shared variable to tell your forever loop not to change the enemy’s direction when you are spinning the enemy.
Detailed Instruction
- Create a shared variable
"spinning", default toFalse - In the
when_this_sprite_clickedevent,- Set
"spinning"toTrue - Turn the enemy by incrementing the
directioninside the repeat loop (forloop) - Set
"spinning"back toFalse
- Set
- In the event where you move the enemy
- If
"spinning"isTrue, do not runpoint_towards_mouseand the related logics.
- If
Outcome
5. Add a perk that you can click to slow down the enemy
Hard - LV3
- Create a sprite when the any desired image
- Create a shared variable called
"speed_multiplier", default to 1.0 - In a
when_this_sprite_clickedevent- Hide the sprite
- Move the sprite to a random location
- Multiply
"speed_multiplier"by 0.9
- In a
when_game_startevent, with a forever loop- Yield for a random amount of time
- Show the sprite
- In the event where you move the enemy
- Multiple the enemy’s speed by
"speed_multiplier"
- Multiple the enemy’s speed by