- Learnt28 min · ammo, reloading, four weapons, one bullet
- Basic24 min · build the arsenal
- Challenge 1–327 min · ammo, switching, damage
- Extrabonus · a proper readout
Review — where we got to 5 min
- Friction and push are named settings, so the feel is one number away.
(hp)is “for this sprite only”, so every clone has its own health.- Three hearts, and a referee that ends the game at zero.
Quick-fire
What actually makes a machine gun different from a pistol?
Reveal the answer
Numbers. How much damage, how often it fires, how many bullets it holds, how long it takes to reload. The picture is the least important part — and 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 that means “busy”
- Four weapons from one My Block
- Each weapon remembering its own ammo
Learning outcome
By the end of this lesson you will be able to:
- Stop the player firing when a condition says they may not.
- Load a whole set of settings with one custom block.
- Save a value before overwriting it, and put it back later.
Learnt — an arsenal 28 min
A weapon is a row of numbers
Here are our four. Look across the rows, not down them:
The machine gun fires nearly three times a second — and takes three whole seconds to reload. The RPG takes six health off in one hit — and holds five shots. That is what makes choosing a weapon interesting.
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 that 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, and it is there so that two different scripts can agree about 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
Picking up a weapon means setting seven things at once. Doing that four times, in four places, is exactly 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 the keys are three blocks each:
when [3 v] key pressed
Save ammo :: custom
Equip (3) :: custom
Save before you overwrite
Equip () :: custom writes over (ammo). So whatever was left in the gun you are putting down has to be stored first:
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 never learns anything about weapons. It just reads three variables when it is born:
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 arsenal 24 min
Step 1 — the settings
Seven variables: weapon, damage, fire rate, bullet speed, bullet costume, reload speed, max ammo. Plus (ammo), (reloading?) and (slot).
Step 2 — the four ammo stores
(ammo - Pistol), (ammo - Shotgun), and two more. Set each to its full amount at the start.
Step 3 — Equip
Make the block, one input, four ifs. Test with just slot 1 before writing the rest.
Step 4 — Save ammo
The second block, four ifs, no inputs.
Step 5 — the keys
1, 2, 3, 4, then Q, then R.
Step 6 — firing
Rewrite the fire loop with the two conditions and the fire-rate wait.
Step 7 — the bullet
Three costumes, and 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) instead of to 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 around the whole thing is what 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, and that the wait block uses the variable rather than 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 do not match the costume names on the Bullet sprite. They must match exactly, 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 while reloading, and 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.
They hit differently
The deck’s mission. Make the choice of weapon actually 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 worth using at some moment.
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 readout worth looking at
Four ammo variables on the Stage is four grey boxes, three of which are wrong for the gun in your hands. Fix it.
- One readout, showing the weapon you are actually holding.
- Its ammo, and how many it holds — “11 / 15”.
- Something obvious when you are reloading.
- No grey monitors anywhere.
Summary 5 min
- A weapon is a set of numbers. The drawing is the least of it.
- A variable can be a state:
(reloading?)holds 1 for busy, and two scripts agree because of it. - A My Block with one input can set seven things at once — that is what makes four weapons manageable.
- Save the old value before you overwrite it, or the switch quietly loses it.
- One sprite can do four jobs if the differences live in variables.
- Ammo
- How many shots are left before reloading.
- Fire rate
- How long the game waits between shots.
- State
- A variable that says what is happening right now, like busy or ready.
- Balancing
- Changing numbers until every choice is worth making.
Hand in
Share your game and send the link to your teacher. Next lesson is the last one — coins, waves, and a shop.