Lives and Scoring - Learn Coombe Dean

Lives and
Scoring
Games Programming in
Scratch
Games Programming in Scratch
L2 Lives and Scoring
Learning Objectives: We are learning to…
• Understand and use variables in a program
• Annotate a program with comments to explain
scripts
Games Programming in Scratch
L2 Lives and Scoring
Learning Objectives:
o Understand and use variables in a program
o Annotate a program with comments to explain scripts
Excellence
I can extend my program to use variables of my own idea and fully
explain scripts I have created using comments
Developing
I can use multiple variables in a program and add comments to
explain how they are used
Secure
I can describe the purpose of variables and use them in a program
following instructions given
Foundation
I can with support use variables in a program
Games Programming in Scratch
L2 Lives and Scoring
So far…
• Planned idea for our game
• Added ‘Sprites’ that move in Scratch
 Main Character that moves with arrow keys
 2nd Character that moves on it’s own
(Don’t worry, we will change these to your own
chosen characters later!)
But, at the moment..
What happens when one sprite touches another one?
Games Programming in Scratch
L2 Lives and Scoring
What ‘should’ happen
when one sprite
touches another?
• When?
Forever / When clicked / On a key-press
• Condition?
If touching… / If time exceeds… / If x = y…
• What do you want to happen?
Change colour / end game / go back to start
Games Programming in Scratch
L2 Lives and Scoring
Looping and Selection
The easiest possible answer might be…
If Spaceman touches Asteroid then Game Over
The ‘loop’ is used here to keep checking ‘if’ the Astroid is touching the spaceman,
and ‘if’ it is, ‘then’ it says “Game Over” and stops then game.
Games Programming in Scratch
L2 Lives and Scoring
Looping and Selection
Loops and Selection can be used to do LOTS of
different things in program (and a game).
• Repeat sounds or movement
• Repeat counting something
• Check if touching something
• Check a score or lives
• And loads loads more things!
Games Programming in Scratch
L2 Lives and Scoring
Modifying your game
• Open your game from last lesson
• For ‘Main Character’, change your ‘Movement’ controls
so that they use ‘Forever’ and ‘If…then’ blocks
---------------------------------------------------------------• Delete the original ‘movement events’
(e.g. up, down, left, right)
• Add these ‘blocks’ instead
NOTE!: This will make the
‘up arrow’ work. You need
to extend it to add the other
directions!
Games Programming in Scratch
L2 Lives and Scoring
What is a ‘Variable’?
• A variable represents a location in memory
• This location is used to hold a value which you assign
to it. You can change the value in the program.
• You give a variable a name, which you use to refer to
it, for example to set an initial value, change, use or
output the value.
• For example: A variable called “Lives” might hold
the value “3”.
• Set Lives = 3
Games Programming in Scratch
L2 Lives and Scoring
Variables
Games Programming in Scratch
L2 Lives and Scoring
Writing the algorithm
What are the steps required to set the starting
value of lives and reduce that by one when one
sprite touches another?
1.
2.
3.
4.
5.
6.
Games Programming in Scratch
L2 Lives and Scoring
Programming Lives
1.
2.
3.
4.
5.
When game starts
Start with 3 lives
Check constantly
If..
you are touching
an enemy
6. Subtract 1 life
Games Programming in Scratch
L2 Lives and Scoring
Adding ‘Lives’ to game using Variable
• Create a ‘Variable’ under Data,
and call it ‘Lives’
• Then add the following blocks
to your game
Change this to the
whatever your
‘Enemy’ is called
Think about the Algorithm!
1. When game starts
2. Start with 3 lives
3. Check constantly
4. If..
5. you are touching
an enemy
6. Subtract 1 life
• Test it out… Does it work? What is it doing?
Games Programming in Scratch
L2 Lives and Scoring
NEXT: Scoring
• What makes you earn points in the game?
– Collecting something?
– Beating a timer?
– Solving a problem?
• Use of Show and Hide
blocks for items to collect
What will these blocks do in your game?
Games Programming in Scratch
L2 Lives and Scoring
Adding item to ‘pick-up’ (collect)
• Add a new sprite from the
library for something that
you are going to ‘collect’
(e.g. Apple, Coin)
Games Programming in Scratch
L2 Lives and Scoring
Adding ‘Score’ to game using Variable
• Create a ‘Variable’, and call it ‘Score’
• Then add the following blocks to your game
to the object you want to collect!
e.g. Coin, Apple
Test it out…
• Does it work?
• What is it doing?
Games Programming in Scratch
L2 Lives and Scoring
Variable display settings
•
•
•
•
•
Show or Hide
Integer variable
Slider
Large or Small (With or without label)
Moveable
Games Programming in Scratch
L2 Lives and Scoring
Ending the game
How could you add an ‘ending’ to the game?
• What should make the game stop?
• Could be a running of ‘time’ or ‘lives’ maybe?
We will look at the ‘lives’ option. What would be the
steps (algorithm) for using ‘lives’ to stop the game?
1.
2.
3.
4.
5.
6.
When game starts
Check constantly
If..
Lives = 0
Say message “Game Over!”
Stop all game
Games Programming in Scratch
L2 Lives and Scoring
Add ‘Ending’ to the game
• Add the following blocks to your ‘Main Character’
• Try it out! Does it work?
Games Programming in Scratch
L2 Lives and Scoring
Adding Comments in Scratch
• Comment boxes in Scratch help you, and
others, understand your code at a later date
• Commenting is good practice for professional
programmers