Page 109 - Scratch Programming Playground
P. 109
Script uses code similar to the Cat sprite code. when the
player presses the spacebar, the basketball appears in front of
the cat and starts moving forward. The code sets the Basketball
sprite’s y velocity variable to a positive number, just like how the
Cat sprite’s y velocity variable is set to a positive number when
the cat jumps; this is how the cat throws the ball.
The repeat until y position < -130 block will keep the
Basketball sprite falling until it reaches the ground. when it
reaches the ground, the basketball will be hidden until the
next time the player presses the spacebar.
Save point
Click the green flag to test the code so far. press the spacebar
to make the cat throw the basketball. Make sure the basket-
ball disappears when it touches the ground. Then click the red
stop sign and save your program.
9. Detect Whether a Basket Is Made
next, you’ll add the code that checks whether the Basketball
sprite is touching the Hitbox sprite. This is how you know
whether a basket has been made, and if it has, the Player 1
Score variable should increase. however, there is one snag:
the basket shouldn’t count if the basketball goes up through
the hoop.
Keep in mind that if the y velocity variable is positive, the
change y by y velocity block will move the Basketball sprite
up. If y velocity is 0, then the Basketball sprite is not moving
up or down. But if y velocity is a negative number, then the
Basketball sprite will be falling down.
So you’ll add another if then condition to the Basketball
sprite’s code. you’ll increase the score only if the basketball is
touching the hitbox (using the touching Hitbox? block) and
falling downward (y velocity < 0).
ShooTIng hoopS wITh gravITy 83

