Scratch Programming Lesson 12 Mini Mario Game Part 3 - Adding Game Rules
Scratch Programming Lesson 12 Mini Mario Game Part 3 - Adding Game Rules
In this lesson, I will show you how to create additional sprites and scripts to apply the game rules. We will add scripts based on Sprite Interaction Table. Moreover, we will create random motion scripts for Marios enemies.
If Mario hand hits Brick, then Brick would crack. When the Brick cracks, Coin would start spinning above it. When Coin disappears, Mario gets one point.
The Sprite Interaction Table below lists all relevant interactions for this rule. Sprite1 Sprite2 Interaction Rule If Mario hand hits Brick, then Brick would crack. Mario Brick Each time a coin appears, Mario scores 1 point. Coin Mario When Brick cracks, Coin would show spinning above Brick Brick Coin
You may have noticed that the JUMPING UP costume has a bit of RED on the fist. Its not for decoration. The RED bit is added so that the Brick sprite can know when Marios fist has hit it. I call this color (RED in this case) a sensitive color for the Brick, and the Brick is a color-sensitive sprite of the RED color. A sensitive color of a
ShallWeLearn | http://shallwelearn.com
color-sensitive sprite is a color which such a sprite is sensitive to and responds to when touched. A good sensitive color for a color-sensitive sprite is a color that is not yet used by any sprite in the project. In the case of Brick, which contains burgundy and black, it can have all other colors as sensitive colors. I just picked RED. You can certainly use BLUE.
NOTE: To copy the color of the RED from Marios fist, first select Marios JUMPING UP costume so Mario shows up on Stage in JUMPING UP costume.
Select Bricks script panel and drag out a touching color block. Click the color box (arrow cursor would turn into an eyedropper) and move the eyedropper
ShallWeLearn | http://shallwelearn.com
Move the eyedropper to the Stage to copy the RED on Marios fist. The touching color block would change to reflect the color just copied.
1.2 Rule: If Mario is attached or touched by a Bat or a Crab, he would die. The Sprite Interaction Table below lists all relevant interactions for this rule. Sprite1 Sprite2 Interaction Rule If Mario touches Crab, he would die. Mario Crab If Mario touches Bat, he would die. Mario Bat
Add the script shown at right to Mario so that he would die when touched by a Crab or a Bat. To die, Mario switches costume to DEAD, sets game_on to 0, and then stop all scripts.
ShallWeLearn | http://shallwelearn.com
Modify the script to move right so that Mario responds to right arrow key click only when game_on variable is 1.
Modify the script to move left so that Mario responds to left arrow key click only when game_on variable is 1.
Modify the script to jump so that Mario responds to up arrow key click only when game_on variable is 1.
Modify the script to squat so that Mario responds to down arrow key click only when game_on variable is 1.
ShallWeLearn | http://shallwelearn.com
2.1 Rule: Crab Randomly Crawls to Left and Right Since a real Crab can only move side to side, but not up and down, we will make Crab move toward Mario, but with some randomness in its direction when deciding to turn left or right. To do so, we need a way to pick left or right randomly. Create a variable named dir and set it to the value of . The
block returns a random value between 0 and 1. And the block returns the round number of this random value. If the random number is smaller than 0.5, then the result would be 0, but if the random number returned is greater or equal to 0.5, then the result would be 1. This way, there is 50% chance the result would be 1, and 50% chance the result would be 0. To use the result to determine the direction, use the if else block.
ShallWeLearn | http://shallwelearn.com
Lets put Crabs script together so that it moves randomly to left and to right, and that it bounces if it hit the edge.
2.2 Rule: Bat Flies Toward Mario, with Some Randomness Unlike Crab, Bat flies so it should move in all four directions. But theres much space on Stage so Bat may never touch Mario if it just flies randomly. What we want is to have Bat fly toward Mario but randomly adjusts its direction. To have it move toward Mario, use point towards X block and move X steps block.
To keep flying toward Mario when game starts, expand the script to look like this:
If you try it now, Bat would shoot straight toward Mario and the game will end before you have time to move Mario! What we need to add is a bit wondering by adding directional randomness. Just like Crab, Bat should randomly pick a direction to turn, but unlike Crab, Bat should pick a direction from -90 to 180, the whole range of
ShallWeLearn | http://shallwelearn.com
rotation in Scratch.
Dont forget to add the if edge, then bounce block. So the final script for Fly motion should be:
!!TEST TIME!! Now we are ready to test! Click Green Flag to start. Crab should crawl side to side, and Bat should fly in zig-zag motion. Try moving Mario under Brick and click up arrow key to jump to hit it. Coin should appear above Brick and disappear shortly after. The score would go up by 1. Also try move Mario toward Bat or Crab to commit suicide; when Mario touches either Bat or Crab, all should stop.
ShallWeLearn | http://shallwelearn.com