- ep.1 Player Movement
- ep.2 Scrolling
- ep.3 Building
- ep.4 Enemy
- ep.5 Enemy Attack ( Here Now )
- ep.6 Player Attack
Welcome to Fortnite in Scratch, Episode 4! Last time, we completed the enemy spawning logic. This session, we'll implement gunfire from the enemies. Let's dive in!
Today's Goal
Let's check out the short video; it shows what we''ll achieve with our coding today. You can see bullets being fired by enemies, and when these bullets hit the stairs, the stairs collapse. Right now, nothing happens if the bullets hit the player, but first, let's focus on making the structures destructible.
Sprite 'Enemy'
First, when the enemy sprite clones itself, let's store the position and angle information for each clone in variables. This will help us manage their behavior more effectively.
Open the 'Enemy' sprite.
Place a new 'when I start as a clone' block and a forever block.
When creating large-scale projects, having many forever loops can lead to performance issues and make the animation choppy. However, for simple and small-scale projects like this one, you don't need to worry too much about it. The performance should be manageable.
Variables: 'ENEMY ATTACK X', 'ENEMY ATTACK Y' AND 'ENEMY ATTACK DIR'
Create new variables for all sprites as shown below.
Initialize these variables like this:
- ENEMY ATTACK X = x position
- ENEMY ATTACK Y = y position
- ENEMY ATTACK DIR = direction
Simple! This sets up the basic tracking for each enemy clone's position and direction, which will be crucial for managing their attacks.
Message 'Enemy Attack'
Create a new message named 'Enemy Attack'.
Place the broadcasting block for this message right below in the script.
And wait for 2 seconds.
Sprite 'Enemy Bullet'
Alright, let’s create the process for firing enemy bullets in response to this message. Let's get coding!
Open the 'Enemy Bullet' sprite.
Hide the sprite when it starts.
When I receive 'Enemy Attack', create clone of myself.
Place another 'when I start as a clone' block, and set the direction to 'ENEMY ATTACK DIR', x to 'ENEMY ATTACK X', and y to 'ENEMY ATTACK Y'.
Set size to 30% and show.
Place a forever block and keep moving 10 steps.
Place an 'if' block and include an 'OR' conditional block inside it.
Insert the expression 'x position < -230' on the left side and 'y position < -150' on the right side of the 'OR' conditional block.
This means checking whether the enemy bullet has gone off-screen.
Then delete this clone.
Preview
Nice! The bullets are being fired from the specified location at the specified angle, and they disappear once they go off-screen. Great job!
Sprite 'Wall'
Let’s create the process for breaking walls when bullets hit them. So far, the only animation in the game has been the player's walking, but adding animations for walls and stairs collapsing will enrich the gameplay and make the game feel more dynamic!
Open the 'Wall' sprite.
Block Definition 'Broken'
Create a new block definition named 'Broken'.
Place this block in the forever loop under the 'when I start as clone' block.
In this definition, check if the sprite touces the 'Enemy Bullet'.
Message 'Wall is broken'
Create a new message.
Broadcast this message inside the 'if' block.
Switch costume to 'wall 2'.
Wait for 0.1 seconds.
Finally delete this clone.
Drag and drop this 'Broken' definition to the 'Stairs' sprite.
Sprite 'Stairs'
Open the 'Stairs' sprite.
Place the 'Broken' block in the forever loop block under the 'when I start as a clone' block.
Message 'Stairs are broken'
Create a new message.
Then adjust the 'Broken' block specifically for the 'Stairs' sprite to ensure it behaves correctly when triggered.
Preview
Marvelous! You can see the stairs and walls being destroyed by the bullets. It really brings the game to life with a sense of dynamic action!
BROKEN POSITION Y
It's looking great so far, but let's take it even further! If there are stacked stairs, we'll make it so that when the bottom stair is destroyed, the one above it also collapses. This will add more realism and make the game even more engaging!
Insert a block here to set 'BROKEN POSITION Y' to the current 'y position'.
Stairs are broken
Add the process for when the "Stairs are broken" message is received.
Check if the 'y position' is greater than 'BROKEN POSITION Y'.
Then, after 0.5 seconds, switch costume to 'stairs 2'.
And again, wait for 0.1 seconds and then delete this clone.
Preview
Yay! Now the stairs break and fall down. It’s frustrating to be knocked down after climbing so high, isn’t it? This mechanic could be useful not just in Fortnite-style games but in many other types of games as well!
Bug fixes
The enemy bullets passing through both walls and stairs and continuing to the edge of the screen is a bit overpowered, isn’t it? Let’s fix this to balance the game better.
Enemy Bullets
Open the 'Enemy Bullet' sprite.
Place these blocks to delete bullets.
Stairs
Oh, that’s right—I forgot to add the breaking process to the walls.
Open the 'Stairs' sprite.
Let’s drag and drop the breaking logic from the stairs sprite to the walls sprite to duplicate it.
Adjust some parameters for the 'Wall' sprite.
Alright, this part is complete! Now let’s move on to creating the player’s attack and the game-over process. We’re in the final stretch—let’s finish strong!
- ep.1 Player Movement
- ep.2 Scrolling
- ep.3 Building
- ep.4 Enemy
- ep.5 Enemy Attack ( Here Now )
- ep.6 Player Attack