Space Shooter 2 - Enemy Collisions
Key Takeaways
Builds a space shooter game with enemy collisions and clone to clone interactions using Scratch
Full Transcript
[Music] hello fellow scratchers welcome to the much anticipated episode two of our space shooter tutorial lemonoid invasion and this is going to be exciting as those rampant lemons are finally about to make their appearance we are also going to cover a common cause of many a scratch's headache two-way collisions between clones delays hit lemons or lemons hit lasers so yeah we had lots of fun with episode one and i got such an amazing response just look at all these scratch projects wow if like me you want to see more then stay tuned because i'm going to dig into these later on in the video so cool i just love what you guys are creating but now on with part two okay i've updated the lemonoid asset project on my griffpatch tutor account a link to it is available under the video you don't need to use these but i've added some new sounds to the project that i will be making use of today convenience they're all in this sprite named all audio i'll just backpack the audio sprite and we're ready to go open the tutorial project and drag the audio sprite from your backpack into the project like so let's see what we have there's the main music theme at the top here a laser sound and three explosions that's cool hey why don't we make use of the game music right away drag the top sound into the stage sprite have you made an audio loop before it's dead easy now in a stage sprite add a when green flag clicked and start of rev-loop then simply drop in the topmost play sound block that's the play sound until done one give it a test oh yeah i can almost feel those lemons charging up their juice powered space drives but before we can shoot them in style we really need an audible laser sound back in the all audio sprite let's drag the laser shoot 16 sound to the laser sprite and then in the laser sprite simply place a start sound laser right at the top of the when i start as clone block this time don't use the start sound and wait otherwise your new laser sprite won't appear until the sound has finished playing that would not be good at all testing that now and yeah that will do fine you'll note that i edited this sound to make it quite a bit quieter than its original level because otherwise it would get super annoying okay it's been a long time coming but it's lemon time make a new sprite and name it lemon now i don't know which of you have studied the finer qualities of the majestic lemon subtle form but let me quickly guide you on how to draw this surprisingly complex fruit start with a yellow oval around 11 grid squares wide and seven squares tall now this is important using the selection tool click and drag the entire lemon until it snaps to the center of the canvas see this little center indicator snap into place this ensures the lemon rotates nicely around in the middle when we add it into the game your spacecraft should have been done the same way next i'm using the shaping tool to pull in the line above and below each end of the lemon that's pretty cool i think i could tweak this lemon shape forever okay we should name the costume lemon and then we'll make a second costume which we'll name big if you've followed any of my other tutorials you'll know all about this we can use it when we want a sprite move off screen this one is going to be huge though zoom right out of the drawing canvas like so and then use the rectangle tool to draw the biggest rectangle you possibly can even bigger than the main canvas that should do us just fine and will allow us to move things really far off the screen right to code then let's set the sprite up how we want it when green flag clicked switch to the lemon costume and then right away send the lemon sprite to the back layer this will be important to ensure the lasers and the player appear in front of the lemons but it has another effect and that is to change the order of which sprite scripts run first but that is a story for another tutorial set size to 300 and run the project to check the sprite's size yeah that's about right if your sprite isn't showing then just tap the show button here okay so the plan is to use clones to bring the new lemons into the game so we'll hide this initial lemon sprite just like we did for the main laser sprite only the clones of the lemon will be made visible so the game has now begun but we should delay our first lemon's appearance using a weight block however i'm going to set it to zero until later on as having to wait would make testing annoying we'll increase it later next we'll add a forever loop this is where we'll begin cloning lemons make a new block named new enemy and use it in the forever loop this will create the clone and so we then just need to wait for two seconds to begin with before repeating and creating the next enemy lemon now this will be far too quick for our real game but again it will make testing things easier let's think now about how we want a new enemy to appear my idea is to create each lemon off-screen and then to get the lemon to slowly drift in towards the center of the stage pass by and slip quietly off the other side there's a number of ways i can think of setting this up and the one i settled on is this position a lemon in the middle of the screen turn it to face a random direction and then walk it backwards off screen so now if we slowly move the lemon forward it is guaranteed to come back to the middle before continuing onwards excellent let's get scratching we'll start by switching the lemon sprite to use the big costume after all we want to push the lemon far off screen so this is a must now position in the center of the stage with a go to x0 and y0 i'm going to make a new variable though to keep track of the lemon's direction of movement naming it d d i r ensuring it's for this sprite only now set it to pick random of -180 to 180 to give us a full circle of possible directions now we can use a point in direction of d good so all that's left is to walk it backwards off the screen so move by negative 350. negative movement always moves us right backwards okay the lemon should be well off screen so it's safe to switch back to the lemon costume once more lastly before we actually clone our lemon we should decide how fast it will travel so make a new variable named speed again ensuring it's for this sprite only as we want different lemons to have their own speeds i'm going to set speed to 1. that's nice and slow for the initially large lemons and now yay we create clone of myself all the work we just did will be copied into the new clone ready to go wow we're doing a lot of coding without a test and that can make finding problems harder but we're not quite ready yet the lemon should be cloning off screen but we haven't yet told it what to do when it gains consciousness so when i start as a clone show the lemon and straight into a forever loop then simply move it forwards by speed and we can test yay and here comes a lemon gosh they really are slow they are coming in from random directions forgive me if i speed this up and they are traveling through the center of the screen before moving off the other side ah except not quite look at this i've coloured the border so you can see it the lemons are getting trapped on the edge of the screen we should have used the big costume trick switch the costume to big before we move the sprite forward and then afterwards switch it back to lemon easy as lemon pie now you can see each citrus oval glides smoothly out of view much better but where do they go the answer is nowhere useful yet in games like this one it is common to allow the enemies to wrap around from one side of the level to the other this way we are forced to keep shooting them to prevent us being overrun as more and more enemies are introduced into the game make a new custom block and we'll name it wrap around and move it into some free space we can make use of the new block here just after moving forwards but before we switch back to the lemon costume wrapping around then we could code this the shorter but trickier to understand way using mods or do it the long but simple way this time we are going to do it the long simple bring in an if else block check if x position is greater than 350 that's way off the right hand side of the screen we want it to wrap back around to the left so set x to negative 350 but what if the lemon has moved off the left side of the screen if x position is less than minus 350 then we set x to 350. that wraps it back over to the right again sorted let's do the same for the y positions i'm going to duplicate the whole thing but to be honest we have to change so much that we might be better off scripting this all over again if y position is bigger than 280 set y position to negative 280. else if y position is less than -280 then set y to 280. cool that looks about right let's test it whoa and now it occurs to me that with this many lemons spawning i can't easily tell if this wrapping around is working or not i'm going to change the speed of the lemons to be 8 and increase the delay between new lemons to be every 10 seconds and then let's test again yeah now i can see that the single lemon is traveling off the bottom and reappearing at the top very nicely clicking the green flag again causes the lemon to start in a different direction and we can confirm that it wraps around again top to bottom right to left left to right etc okay let's put the delay back to one second and the speed also to one now look at this all the lemons are coming headlong towards me but something is amiss you see these lemons are not projectiles or spacecraft i want them to be more akin to space rocks and everyone knows that space rocks and lemons for that matter spin around as they travel through space our lemons have no spin and here we run into a little problem if we start turning our lemons to make them spin around then the direction the lemon travels in will change too because they're always traveling forward but we wanted the lemons to travel in straight lines so we have a challenge to solve because i really want those spinning yellow fruits okay let's set that weight block back to two it was just too quick i find that when i start as a clone script the problem we have is this move block it would be useful to be able to move the lemon in a direction other than that which it is facing okay i think you need to know this next trick i use this all the time in a lot of projects so i think you may be ready to use it too here we go change x by speed multiplied by and here's the bit you won't like sign of direction then we also change y by speed multiplied by cosine of direction so this is written as sin and cos so what does this do well believe it or not this script does exactly the same thing as the move speed block don't believe me let's swap the move block for those two lines and then watch this running the project now and there behold the lemons are traveling just the same as if we were using the old move block well welcome to the mathematics of the move block this is actually how the move block works it will be using a sign and cause itself to work out how to move forward so why bother telling you this little nugget of information well we wanted to be able to move the lemon in one direction while it is facing a different direction and now with this change we can all we need to do is drop in the direction here so replace the direction reporter with our variable de dir yes now it becomes obvious why i made this variable way back at the beginning we can just check everything is still working yeah and so the fun bit place a turn right by 5 degrees just after the change y block are you ready for this look at that we have our beautifully sour juicy nuggets spinning menacingly towards the center of the stage that's a victory for us but our reveling will be short-lived if we don't sort out our lasers i have the need to start fighting back we'll start in the lemon sprite add an if check after switching back to the lemon costume here and simply check whether the lemon is touching a laser if it is then for now just delete this lemon clone run the project and finally we can start to tell those lemons who is boss yes it's me well that's super easy i can wipe them out before they've even got fully onto the screen what you need to notice though is that my lasers are able to wipe out a lemon but they don't stop there they continue on destroying all the lemons in their path now this may be your desired behavior for your game but for me i want the lasers to only be able to hit one lemon and this is where we hit a difficulty and something that many a scratcher shakes their fist at because it feels like it should be really easy what is missing is that we know the lemon was touched by a laser clone but we don't know which clone how can we find out which one it was well sadly this question has no easy answer in reality there is no 100 accurate way of doing this without some enormous amount of work anyhow but luckily there are simple ways of getting quite close start by clicking into the laser sprite we are going to look out for when the laser is touching a lemon add another when i start as clone hat block and wait until the laser is touching a lemon as soon as this occurs we delete the laser clone right so is there anything wrong with this we are now deleting the lemon and the lasers when they are touching each other right sadly no run the project we have a new problem these lasers are really not doing a good job of destroying the lemons all the lasers are being deleted as they touch the lemons but not so the lemons they have somehow become much more resilient to our lasers so we can assume that they are not detecting the laser touches most of the time so why is that well there are three reasons that i know of ha shall we look at the first we have two sprites a laser and a lemon each one starts by moving the sprite and then checks whether it is colliding with the other sprite but although it looks on screen like these two sprites are moving at the same time they are not no everything in scratch runs in an order one after the other you don't see this on screen because it all happens too fast within a single screen refresh so in our example here the laser sprite scripts run first it moves the laser forward and then checks for collisions nope no collision there next the lemon gets to run its scripts it moves forward no no collision at this point the screen will refresh and the player sees both sprites have moved towards each other now it's a laser's turn again it moves forward and aha it has detected a collision what does it do next well it deletes itself hmm can you see the problem yet now the lemon script runs and the lemon moves and checks for a collision yep there's no laser so this lemon is safe and doesn't get deleted bother in computer terms what we have here is called a race condition as only one of these sprites will touch first and get deleted but we want both to get the chance to collide so what do we do there is a simple remedy for this all we need is for the laser to not get deleted until the lemon has run its next script then the lemon will also get to detect the collision and we can delete them both raw this is quite easy to do before we delete the laser clone drop in a weight block and we'll wait for zero seconds now hold on was that a mistake no no no this is really cool when you use a weight zero scratch will always wait for the next screen refresh even if you don't put in a length of time so that will ensure the lemon gets its moment to run and then we are free to delete this clone as before let's try that again now and look at that so much more promising most of our lasers are gliding correctly i can't help but note that a few lasers are somehow getting through not to worry i did say i could think of three reasons why collisions were not working and we've only addressed one of them we'll come back to this again later right now though i'm really keen to focus on what happens when a lemon is hit click into the lemon sprite if you paid attention to my original gameplay video you probably saw that these lemons are destined to split into three smaller lemons when destroyed this is a classic asteroids mechanic and i love it we can make a start on this by replacing the delete clone with a new custom block named enemy hit move the delete clone block over here okay when a lemon is hit we want it to split into smaller lemons let's begin by making the lemon half its original size by setting size 2 size divided by 2. we can test that [Music] yeah i'd say this is mostly working as expected but the smaller lemons are getting a bit too small at some point they should stop shrinking and just be deleted instead check if size is less than 35 and use the delete this clone block if it is giving that a quick test should now let me put pay to those tiny lemons and yep i can confirm they are no more super now before we split we also want our lemons to travel faster so set speed to speed multiplied by 1.5 and to mix things up let's change the direction of the first lemon by a random number between 0 and 180 and here we go we split the lemon create a clone of self now we have two lemons but they are facing the same direction so change the variable dir da by a hundred and twenty that's a third of a full circle turn now clone again to make the third lemon and finally change the by another 120. oh yeah now this is exciting just before we test increase the time between new lemons appearing from two to every five seconds okay i'm going to test this this is sweet i love how those look as i blast them apart and wow how fast those little ones are traveling that makes them much more dangerous and tricky to shoot the more i blast the more lemons start to fill up the screen that's wonderful but now i'm seeing a lot more missed collisions between lasers and the smaller more speedy lemons i think we should take a look at the collision issue number two the second problem lies again in the order of our scripts here a laser is chasing a lemon the laser as before collides first so now we are being clever and not deleting the laser yet we allow the lemon a chance to run its scripts so here goes it first does its movement scripts uh oh do you see the problem the lemon only checks for collisions after moving and oh my gosh no collision is detected it's moved too far away okay so how do we solve that simple we change the order of the lemon scripts to perform the collision check first before moving like this now we ensure that the lemon is in the same position as it was when the bullet did its collision check first detecting the same collision but in reverse so yeah in the lemon sprite move the if touching laser to be the first thing we do in the forever loop even before changing the costume that's important let's give it a try i'm just picking off one lemon at a time and all i can say is yes i'm not seeing any failed collisions at all now that seems to have fixed the problem sweet so i'm really pleased how these lemons are looking and especially how well our collisions are being detected there's still quite a bit of polish before it looks at its best that though we'll have to wait until episode 3 where we'll be adding lemon health special effects like light flashes and god rays and of course camera shake it's going to look amazing right before we go as i promised let's take a look at some of the projects created by fellow scratchers starting with two of my awesome channel members thanks guys raz kanaz kindly shared with me their take on episode one they followed the tutorial and then added mobile controls how cool is that this will be awesome once we introduce the lemons wow i just wish that scratch supported dual touch because we can't use both sticks at once razka naz thought of this too and the project comes in two flavors this second version simply follows the mouse firing as it goes awesome work and here's another project this time by channel member jude pom oh wow look at this it's a full game well done i don't think i can actually control the cat no they are just floating around but i have to shoot these lemons before they reach lemongirl in the middle poor lemongirl i'd love to know her backstory thanks so much for creating this game for us jude bomb and now just look at all these other projects in the lemonoid studio if you want some inspiration you just need to have a click through these i'm dead impressed with you guys well done [Music] and that is all we have time for this episode if you like this video then please smash the like button subscribe to catch the latest videos and now don't forget you have the option to become a channel member if you really want to support the creation of these tutorials the more support i get the faster these videos will come plus you get extra perks right away so please check that out so until next time have a great week ahead and scratch on [Music] guys [Music]
Original Description
How to build a fast paced space shooter based on the classic arcade game Asteroids, but with Lemons! In this second part of this Scratch tutorial we cover enemy Lemon spawning and movement, and delve into the slightly complex world of clone to clone collisions. We also look at making the lemons split into pieces when destroyed. That leaves us with the exciting explosion flashes, screen shake and more for part 3!
Wondering why Lemons? Well, Lemons were the first fruit to have achieved interstellar capability and are thus attempting to take over the universe. To tackle such a scenario we obviously need to fight back with rapid fire lemon busting lasers!
🚀 *Boost Your Creativity with Griffpatch*
The Griffpatch Academy will take you from "Gamer to Game Creator"
Learn more at 👉 https://griffpatch.academy 👈
😺 Scratch was developed by the Lifelong Kindergarten Group at the MIT Media Lab. See http://scratch.mit.edu
🐱 Scratch Starter Project Game Assets
https://scratch.mit.edu/projects/548866331/
👀 Simple Space Shooter
Part 1 - https://youtu.be/sqsNb0s7Oq4
Part 2 - https://youtu.be/okhIwJUOalM
Part 3 - https://youtu.be/RKdXM3x-jEo
Bonus - Scores - https://youtu.be/atMwy3UKc14
Bonus - Health Bars - https://youtu.be/rTKDzz_d6X4
Part 4 - https://youtu.be/eJf42cpqY-w
❤️ Enhanced Scratch Developer Tools
https://www.griffpatch.co.uk/
📹 The Awesome Video Editing software I use (Camtasia)
https://techsmith.z6rjha.net/5bajbo
--------------Video Chapters--------------
0:00 Intro
00:43 Game Music & Sounds
02:34 Drawing a Lemon
03:57 Coding a Lemon
08:47 Wrap around lemons
11:20 Spinning Lemons
14:29 Lemon Laser Laser Lemon
16:55 Touching Race Conditions
19:29 Splitting the Lemon
22:18 Fix Collisions II
23:49 Episode 3
24:14 Members Projects
25:23 Outro
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from griffpatch · griffpatch · 55 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
▶
56
57
58
59
60
Burrow Blitz - A game by Griffpatch
griffpatch
Burrow Blitz Level 1 Speed Run!
griffpatch
Scratch Messaging Extension (for chrome & firefox)
griffpatch
Scrolling Platformer Tutorial | Part 1 | Get Scrolling
griffpatch
Scrolling Platformer Tutorial | Part 2 | Gravity
griffpatch
Scrolling Platformer Tutorial | Part 3 | Platform Detection
griffpatch
Scrolling Platformer Tutorial | Part 4 | Off Screen Sprites and Player Death
griffpatch
Scrolling Platformer Tutorial | Part 5 | Collectables
griffpatch
Scrolling Platformer Tutorial | Part 6 | Danger Sprites (Bug fix)
griffpatch
Scrolling Platformer Tutorial | Part 7 | Exit Portal
griffpatch
Scratch 3 | Box2d | Physics Extension
griffpatch
Scrolling Platformer Tutorial | Part 8 | Vertical Scrolling
griffpatch
Zombie Cube | Speedrun challenge
griffpatch
Scratch 3 | Physics Extension | Version 2
griffpatch
Top Down Scroller | E1 - Scrolling | Scratch Tutorial
griffpatch
Top Down Scroller | Part 2 | Enemy AI
griffpatch
Top Down Scroller | Part 3 | Enemy Clones & List
griffpatch
Top Down Scroller | Part 4 | Live Die Repeat (Game Over)
griffpatch
Top Down Scroller | Part 5 | You've Won!
griffpatch
Cloud Game Tutorial | Part 1 | The Basics
griffpatch
Cloud Game Tutorial | Part 2 | Encoding and Decoding
griffpatch
Scratch 3 Dev Tools Browser Extension by griffpatch
griffpatch
Cloud Game Tutorial | Part 3 | Multiplayer
griffpatch
Scrolling Platformer Tutorial | Part 9 | Momentum
griffpatch
Scrolling Platformer Tutorial | Part 10 | Wall Jumping
griffpatch
Cloud Game Tutorial | Part 4 | Auto Game Joining
griffpatch
Game Preview - "Getting Over It" with Griffpatch - A fan recreation in Scratch
griffpatch
Speed Run with Outtakes | Getting Over It | 4 minutes 30 seconds
griffpatch
Griffpatch's Live Stream
griffpatch
MMO Platformer Speedrun Challenge
griffpatch
Appel Platformer | Scratch Game Blog
griffpatch
Cloud Game Tutorial | Part 5 | Smooth Movement
griffpatch
How to Design a Level for Appel
griffpatch
Fastest Appel Speedruns | Jan 2021
griffpatch
Tile Scrolling Platformer (Mario) | 1. Setup
griffpatch
Tile Scrolling Platformer | 2. Grid List
griffpatch
Tile Scrolling Platformer | 3. Tile Collisions
griffpatch
Tile Scrolling Platformer | 4. Platforming Scripts
griffpatch
Tile Scrolling Platformer | 5. The Level Editor
griffpatch
Tile Scrolling Platformer | 6. Level Codes
griffpatch
Tile Scrolling Platformer | 7. Drop Through Platforms
griffpatch
How to make Physics in Scratch | Full Tutorial
griffpatch
Tile Scrolling Platformer | 8. Enemy Clones
griffpatch
Simple Pathfinding Tutorial
griffpatch
Tile Scrolling Platformer | 9. Enemies in Level Editor
griffpatch
Simple Maze Generation | Scratch Tutorial
griffpatch
Tile Scrolling Platformer | 10. Mystery Blocks
griffpatch
Simple Car Steering Simulation | Scratch Tutorial
griffpatch
Simple Background Scrolling Tutorial
griffpatch
Tile Scrolling Platformer | 11. Death, Background & Sound
griffpatch
Mid Week Update & Channel Membership
griffpatch
Simple Grid List Tutorial with Image Scanning
griffpatch
Tile Scrolling Platformer | 12. Auto Arranging Tiles
griffpatch
Code a Fun Space Shooter Game 🚀 | 1. Move & Shoot | Scratch Tutorial
griffpatch
Space Shooter 2 - Enemy Collisions
griffpatch
Tile Scrolling Platformer | 13. Level Progression & Editor Improvements
griffpatch
Space Shooter 3 - Camera Shake & FX
griffpatch
Tile Scrolling Platformer | 14. Sloping Tiles
griffpatch
Pathfinding a Maze using Lists
griffpatch
Tile Scrolling Platformer | 15. Crouch & Slide
griffpatch
More on: Visual / Block Coding
View skill →Related Reads
📰
📰
📰
📰
Math Homework: Step-by-Step Solver & Scanner
Medium · AI
Passports, Playbooks, and Platforms: How Emerging Tech Is Changing Travel and Sports
Medium · AI
Wi-Fi Sensing: Your Router Is Becoming a Motion Detector
Dev.to · Haven Messenger
Sifting Through Existence, OR, Why You Can’t Trust AI with Your Bibliography
Medium · AI
Chapters (13)
Intro
0:43
Game Music & Sounds
2:34
Drawing a Lemon
3:57
Coding a Lemon
8:47
Wrap around lemons
11:20
Spinning Lemons
14:29
Lemon Laser Laser Lemon
16:55
Touching Race Conditions
19:29
Splitting the Lemon
22:18
Fix Collisions II
23:49
Episode 3
24:14
Members Projects
25:23
Outro
🎓
Tutor Explanation
DeepCamp AI