Fruit Slasher!
Vineet Srivastava
www.scratchcourse.wordpress.com
In this lesson, we will …
2
www.scratchcourse.wordpress.com
Fruit Slasher!
3
www.scratchcourse.wordpress.com
Building this game
4
The fruit sprite
5
Costumes of the fruit sprite
Notice, we added 5 different fruits costumes.
Also, note that these are costumes 1, 2, 3, 4 and 5 respectively.
(You can also consider importing costumes).
6
Add a bomb costume
7
Bomb Costume
8
Now the split fruits
9
Add fruit costumes once again!
10
Creating the split fruit
11
Step 1: Convert to BITMAP
Step 2: Choose the select tool to select a part of the costume.
Step 3: Split the selection away.
Step 4: Rotate the selection
Note
Step 5: Re-centre
All the cut fruit costumes
12
To summarize the costumes
13
Costumes 1 to 5: Full fruits
Costume 6: Bomb
Costume 7 to 11: Split Fruits
Code for the fruits
14
Create a random number of clones after a random waiting time.
Code for fruit movement
15
Create Local Variables
16
xSpeed: How much will the sprite move along the x-axis
ySpeed: How much will the sprite move along the y-axis
Rotation: How much does the sprite turn as it moves.
Setting the variable values
17
Set the variables to some reasonable (and random) values. The exact values do not matter – you can fine-tune according to your liking.
Send the sprite to the bottom of the stage.
Getting fruits to pop-out
18
This statement ensures that eventually the fruits will fall down!
A small wait makes game more playable.
Recall, Costumes 1 to 5 are the full fruits
(Note: This can also be done BEFORE the clone is created.)
Pick random values for xSpeed, ySpeed and rotation (unique for each sprite).
xSpeed can be both positive or negative – so the fruit may travel left OR right.
ySpeed is strictly positive – so the fruit travels upwards.
rotation can be positive or negative – so fruits turn both anti-clockwise or clockwise .
Food for thought!
19
Bring in the Bomb costume
20
Create a PLAYER sprite
We use a separate costume, with an EMPTY, costume, for the drawing code.
This is IMPORTANT, because mixing the fruits and the drawing sprite functionality into one sprite causes a lot of confusion and makes the code very difficult to follow.
21
Slashing the fruit
22
We can use lists for this purpose
23
Lists for ‘mouse x’ and ‘mouse y’
24
Add ‘mouse x’ and ‘mouse y’ to a list.
The moment this list becomes equal to 6 elements, delete the first element.
The choice of 6 is arbitrary. But notice that this list will contain the ‘last 5’ positions that the mouse has been at.
Mouse_x and Mouse_y lists
25
x0
y0
x0
y0
x1
y1
x0
y0
x1
y1
x2
y2
x0
y0
x1
y1
x2
y2
x3
y3
x0
y0
x1
y1
x2
y2
x3
y3
x4
y4
x0
y0
x1
y1
x2
y2
x3
y3
x4
y4
x5
y5
x1
y1
x2
y2
x3
y3
x4
y4
x5
y5
x1
y1
x2
y2
x3
y3
x4
y4
x5
y5
x6
y6
x2
y2
x3
y3
x4
y4
x5
y5
x6
y6
List Building Up
New position added
Item 1 deleted
New position added
Item 1 deleted
After the list has 5 items
Notice, this list will ALWAYS contain the last 5 items that the mouse went, the latest position is the at item 5.
Food for thought
26
Using list for drawing the slashing line
27
Drawing the slashing line
28
When the MOUSE is down, that is, when we click the mouse button
Go to the first item of Mouse_x and Mouse_y
Erase everything (This is useful to prevent user from holding the MOUSE and getting a LINE - class video part 2 at about 10:30)
Start drawing
Go to every item of the list. Notice, we start from item 2, then go to item 3, item 4 and so on, till the last element,
Notice also that if the mouse stops moving, this will just draw a small dot on the screen.
If the mouse is not clicked,
Erase everything and stop drawing.
Drawing Code expanded
The REPEAT UNTIL on the last slide can be understood by realizing that we are basically going to EVERY element of the list. And sice PEN is down, we end up drawing.
29
Converting this code to a MyBlock
30
We created a new MyBlock called DrawBlade, without any inputs, and moved the entire drawing code into that. This makes the code more readable.
Loop with a variable!
What we have just seen is a very special programming construct – a loop (repeat) where on every iteration of the loop, a counter variable gets changed.
This is known as a FOR LOOP in many languages and is a very useful construct in programming.
We will also use this approach in the next project – Hangman!
31
Checking if the mouse is moving
32
Check Movement
33
Check every element. If any one element of the list is NOT equal to the present mouse position, then we set the variable ‘moving’ to 1.
Slashing the fruits
34
Recall costumes 1 to 5 were uncut fruits. If they are slashed, we get a point.
Also, we change costume to the corresponding cut fruit costume.
If we slash the bomb, (that is, if the costume number of the clone is 6), the game is going to get over.
This code is done on the FRUITS sprite.
Deducting Lives
35
This ‘and’ condition ensures that the fruit is falling DOWN and touches the edge
If the fruit has not been slashed and it is not a bomb, then reduce Lives by 1. (We can determine this from the costume.)
Regardless of the costume, delete this clone.
Getting game to get over
36
Wait Until LIVES < 1 is a better choice.
The two ending Broadcasts
37
Notice, this broadcast will delete all the clones
We stop New clones from getting generated
Play the sound completely
And you are all set!
38
www.scratchcourse.wordpress.com
Extra Innings
39
Rotation Style
40
Sprite looks only two ways (either LEFT (direction less than 0) or RIGHT (direction more than 0).
Sprite does not rotate.
Sprite rotates freely all along.
Experimenting with rotation mode
41
Rotation Style for Bomb
42
Draggable Mode
43
Setting this to ‘not-draggable’ makes the sprite not get dragged (but only on the project page).
Ideas to spice up the game!
44
www.scratchcourse.wordpress.com