- Learnt28 min · ammo, reloading, four weapons, one bullet
- Basic24 min · build the weapons
- Challenge 1–327 min · ammo, switching, damage
- Extrabonus · a better display
Review — where we got to 5 min
- Friction and push are settings with names. You change the feel with one number.
(hp)is “for this sprite only”. So every clone has its own health.- Three hearts. A referee sprite ends the game at zero.
Quick-fire
What really makes a machine gun different from a pistol?
Reveal the answer
Numbers. How much damage it does. How often it fires. How many bullets it holds. How long it takes to reload. The picture matters least. Today you will use one bullet sprite for all four.
Today’s Topic 3 min
- Ammo, and running out of it.
- Reloading, and a variable for “busy”.
- Four weapons from one My Block.
- Each weapon keeps its own ammo.
Learning outcome
By the end of this lesson you will be able to:
- Stop the player firing when a check says no.
- Set lots of settings with one My Block.
- Save a value before you write over it. Put it back later.
Learnt — four weapons 28 min
A weapon is a row of numbers
Here are our four. Read across each row, not down:
The machine gun fires nearly three times a second. But it takes three whole seconds to reload. The RPG takes six health off in one hit. But it only holds five shots. That makes choosing a weapon fun.
Ammo
wait until <mouse down?>
if <<(ammo) > (0)> and <(reloading?) = (0)>> then
create clone of [Bullet v]
change [ammo v] by (-1)
wait (fire rate) seconds
else
wait (0.1) seconds
end
wait (fire rate) seconds makes each weapon feel different.A variable that means “busy”
(reloading?) is not a score and not a setting. It holds 1 for busy and 0 for ready. Then two scripts can both know what the player is doing.
when [r v] key pressed
if <(reloading?) = (0)> then
set [reloading? v] to (1)
wait (reload speed) seconds
set [ammo v] to (max ammo)
set [reloading? v] to (0)
end
Four weapons, one block
When you pick up a weapon, you set seven things at once. You need to do that in four places. That is just what a My Block is for:
define Equip (slot)
if <(slot) = (1)> then
set [weapon v] to [Pistol]
set [bullet costume v] to [bullet1]
set [damage v] to (1)
set [fire rate v] to (0.35)
set [bullet speed v] to (12)
set [reload speed v] to (1.5)
set [max ammo v] to (30)
set [ammo v] to (ammo - Pistol)
end
Then each key needs three blocks:
when [3 v] key pressed
Save ammo :: custom
Equip (3) :: custom
Save before you write over it
Equip () :: custom writes over (ammo). So first, save what is left in the gun you are putting down:
define Save ammo
if <(weapon) = [Pistol]> then
set [ammo - Pistol v] to (ammo)
end
if <(weapon) = [Machine Gun]> then
set [ammo - Machine Gun v] to (ammo)
end
One bullet, four jobs
The bullet sprite does not know about weapons. It just reads three variables when it starts:
when I start as a clone
go to [Human v]
point towards [mouse-pointer v]
switch costume to (bullet costume)
show
repeat until <touching [edge v]?>
move (bullet speed) steps
end
change [hp v] by ((0) - (damage)).Basic — build the weapons 24 min
Step 1 — the settings
Make seven variables. They are weapon, damage, fire rate, bullet speed, bullet costume, reload speed and max ammo. Then add (ammo), (reloading?) and (slot).
Step 2 — the four ammo stores
(ammo - Pistol), (ammo - Shotgun), and two more. Set each one to full at the start.
Step 3 — Equip
Make the block with one input and four ifs. Test slot 1 on its own before you write the rest.
Step 4 — Save ammo
Make the second block: four ifs and no inputs.
Step 5 — the keys
Add keys 1, 2, 3, 4, then Q, then R.
Step 6 — firing
Change the fire loop. Give it the two checks and the fire-rate wait.
Step 7 — the bullet
Draw three costumes. Add switch costume to (bullet costume) when the clone starts.
Every weapon has the same ammo.
You are missing Save ammo :: custom. Or Equip is setting (ammo) to (max ammo). It should use the weapon’s own store.
Pressing R twice makes the reload take for ever.
Two reload scripts are running at once. The if <(reloading?) = (0)> then goes around the whole thing. It stops the second one starting.
The machine gun fires one bullet a second.
(fire rate) is not being read. Check that Equip really sets it. Check that the wait block uses the variable. It must not use a typed number.
All the bullets look the same.
The switch costume to (bullet costume) is on the wrong script. Or the costume names in Equip are wrong. Check them on the Bullet sprite. They must match, capital letters and all.
Ammo and reloading
- Every shot costs one bullet.
- At zero, clicking does nothing.
- R reloads, and takes time.
- You cannot fire during a reload. Pressing R during a reload does nothing.
Four weapons
- 1, 2, 3, 4 pick a weapon. Q moves to the next.
- Each weapon has its own fire rate and reload time.
- Each weapon remembers its own ammo.
- The bullet looks different for each one.
Teacher note
Build Equip for slot 1 only and test it before writing the other three. A four-branch block written all at once and then debugged is miserable; the same block grown one branch at a time is straightforward.
Each one hits its own way
This is the mission from the slides. Make the choice of weapon really matter.
- Damage comes from the weapon, not from the bullet.
- Bullet speed comes from the weapon too.
- A big zombie takes one RPG or six pistol shots.
- Every weapon is the best one at some time.
Teacher reveal — balancing
Ask which weapon they use most. If the whole class says the same one, it is too good — and the fix is a number, not a new feature. Add half a second to its reload and ask again.
This is what game studios call balancing, and it is the same loop as debugging: change one number, play, ask whether it is better.
A better display
Four ammo variables on the Stage show four grey boxes. Three of them are wrong for the gun in your hands. Fix it.
- One display that shows the weapon you are holding.
- Its ammo, and how many it holds — “11 / 15”.
- Something easy to see while you reload.
- No grey boxes anywhere.
Summary 5 min
- A weapon is a set of numbers. The drawing matters least.
- A variable can be a state:
(reloading?)holds 1 for busy. Then two scripts can agree. - A My Block with one input can set seven things at once. So four weapons are easy.
- Save the old value before you write over it. If you do not, the switch loses it.
- One sprite can do four jobs. Keep the differences in variables.
- Ammo
- How many shots are left before you reload.
- Fire rate
- How long the game waits between shots.
- State
- A variable that says what is going on, like busy or ready.
- Balancing
- You change numbers until every choice is good.
Hand in
Share your game and send the link to your teacher. Next lesson is the last one in this game. It has coins, waves, and a shop.