Make Epic Fireworks & Particle Explosions with Scratch Pen Extension

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign scratchers particle simulations are crazy cool and they are like the icing on the cake for many projects elevating a game from good to awesome over the course of the next two videos we are going to be creating some absolutely beautiful particle simulations from particle streams fizzing trails to explosions with collisions and awesome sound effects wow I cannot wait to get started the colors and the sounds really make it so satisfying to watch and it gets me so excited as I can imagine it's looking awesome in any scratch game right just too cool so guys are you ready let's get scratching we'll Begin by creating a shiny new scratch project particles our first step is to create the initial particle stream firing our new particle clones that follow a curve back down to the ground we'll set the scene in the background Sprite filling the entire Canvas black against this our particles will show up really bright and colorful then back in Sprite 1 sorry scratchy I'm replacing you with a small red circle Zoom right in and draw it around the size of one of these grid squares that's four by four pixels then make absolutely sure to drag our Circle to snap it in the center of the costume canvas like so don't miss that step otherwise your particles won't be positioned quite right and now it's coding time let's fire off some new particles shall we when flag clicked and we hide Yep this original particle Sprite is a clone Factory you've probably heard me set before it will create many visible particle clones but doesn't itself want to be visible then we Loop forever firstly waiting for the mouse button to be down and then we want to create particles now but thinking ahead if we want to create more than one particle at a time then we'll need this next bit to be in a custom block so create a new block naming it create particles at with an input of x and another why and of course we tick the run without screen refresh that will do fine drop the Block in after the mouse is down here for this demo project we'll spawn the new particles at the mouse cursor mousex and mouse y but you can enter in any position you like into these inputs so next up in our custom block we go to X Y of the past in X and Y inputs what we need next is to Define how fast our particles are moving make two new variables speed X for this Sprite only and speed y they need to be for this byte only because each particle wants to be able to move in a different direction with different speeds otherwise with for all Sprites all the particles would end up moving the same direction in a kind of grand dance not really what we're after gosh no okay so just for now let's set the initial speed X to six and speed y to 12. and finally we get to create the clone of myself the cloned particle Sprite will take on the same position and speeds that we just set almost there when I start as a clone first we'll show the Sprite remember that it was originally hidden then Loop forever so that we can animate this sucker and I'm going to use a go to XY this might seem like an unusual choice but it's because I need to move both the X and Y positions at the same time in which case we need to reposition the Sprite at the original X position and Y position plus our speed X and our speed y adjustments yay since this Loops forever it should keep moving the particle each screen refresh and will produce the movement animation for the particle let's smash that green flag and run the project pew pew pew yeah now this is fun my little red dots are flying across the screen and makes them rather funky patterns in the process you might have noticed that they appear to be congregating at the top right of the stage however and indeed very soon I've used up all 300 of my available Sprite clones and scratches not willing to yield me anymore shame on you scratch anyhow we can combat this by removing the ones that should have gone off screen if touching Edge then we delete this clone easy as pie and now you can stay here firing off little red particle friends as long as you want and scratch willing you'll be here until someone calls you for tea if you're that lucky but in the meantime particles just don't look good without gravity to pull them down so make a new variable named gravity for all Sprites since gravity is the same for everyone and just after the green flag is clicked set gravity to negative one and to have it take effect drop in a change speed Y block by gravity just before we actually move the Sprite come on then green flag time and yes it's like playing with a hose pipe in your garden really very entertaining and a little mesmerizing to boot I just love the natural forming patterns so cool but now I want to look at how we can use these particles to simulate a sparkler Trail for this we want to fire out a batch of particles in a circle all at once and for this each particle will need a power and a Direction find the create particle script and drop a repeat 10 loop around the set speed and create Cloud blocks this will create 10 clones all at once because we checked the run without screen refresh box for this custom block then create two new variables power for this Sprite only and for the direction also for this Sprite only set these two variables before we begin looping around set der to zero that's straight up and a power of 12. now to calculate the speed X and Y for each particle set speed X to and we need a multiply block the power variable and we multiply by the sine of our Direction variable Isn't that cool so easy do the same for Speed y only remembering to switch the sign for a COS operator lastly change duh by 360. that's the degrees of a circle divided by the number of particles we are firing that's 10. see a full circle in 10 equal steps cool give that a test woohoo it's like having 10 hose pipes all at once now wacky oh look at that ha now it's not perhaps the circle of particles we were imagining so let's up that repeat to 30 particles and consequently divide by 30 as well and then oh man yes now we can see the circle so good but just watch out once we try to create more than 3 000 clones we do start to get weird things happening to our beautiful circles so I'll drop the repeat back down to 10 and now rather than creating perfect circles of particles let's try randomizing it a bit and make a sparkler Trail instead for this we remove the change dur block entirely and move the set initial sector and power variables inside the repeat Loop like so now each particle can have its own Direction and power the direction will be a random number between negative 180.0 and 180.0 the power is also a random number between 0.0 and 10.0 now including the point zero on the end in a random number block allows it to generate fractional numbers and not just the whole numbers you'll get otherwise so this will make our sparklers look much better let's see this in action oh wow look at this how good is that it is just like holding a sparkler again we can have a lot of fun playing with this I can't wait to play with the colors but before we do I want to just talk about air resistance and terminal velocity at present these Sparks float through space and keep accelerating downwards until they are off screen but in reality they should be slowed down by the air that's why fireworks explode out really fast and then slow down as they begin to fall it's the air resistance we can simulate this quite easily too so make a new variable named resist for all Sprites and set it under the gravity to 0.9 great so back to the start as clone script where we change speed y now we bring in a set speed X block setting speed X to itself speed X multiplied by the resist variable being a number just less than one this will cause speed X to reduce in speed each time we set it we will do the same for Speed y setting it to speed y multiplied by resist uh but look then we also add gravity to speed y so why don't we drop in an addition block into our set speed y and combine the multiply on the left with the gravity on the right then we can remove the change speed y altogether one less block we'll run that little bit faster right and run the project what we find is the particles no longer spread out anywhere near as widely and they all Begin to Fall together at the same speed this is the effect of air resistance and the particles have reached their terminal velocity the fastest that they can fall before gravity and air resistance equal out it's so interesting to see how this applies to fireworks let's change resist to 0.8 and then in the create particle script change the power all the way up to 30.0 there do you see how the Sparks now shoot out and then slowly fall together just like fireworks anyhow this is a little over the top for our use so put the power back down to 10.0 and then scroll right up and set the resist to a more sensible 0.97 much closer to one just remember you won't see the resistance change until you click the green flag right so did I mention playing with colors might be fun how about we just cycle through different colors as we are creating the clothes in the create particle script drop in a change color by and this needs to be really small as little as 0.2 perhaps don't forget we are creating hundreds of these clones so this will actually change really fast how beautiful is this it's a very magical effect and really brings this project to life I wonder how this will look if we up the number of generated particles to 100. wow just tapping the mouse now generates these awesome firework like blasts of color now that is cool one thing that is perhaps missing here are particle Trails you know how lights leave a motion trail behind it as it moves these particles if paused are perfect dots what I would prefer is for them to look like this so how can we do that the answer is with the pen extension I'll just put my repeat back to 10 items and then click into the extension add-ons enable the pen extension whoo nice new blocks to play with find with me the when I start as clone script so as a new particle is created we can start by setting the pen size to 2 pixels that makes sure our lines are a little more chunky then before we move this Sprite stuff in a pen down to begin drawing a line and then right after we've moved we use a pen up to finish drawing again this way each particle will Trace its last movement as a single Short Line on the stage canvas well that sounds cool but what does it look like wow Trails indeed only these trails are all blue and not matching our particle colors easy fix find the change color block before we clone to these particles and pop in the equivalent change pen color changing it again by 0.2 right let's give that a spin color change confirmed nice now if you're wondering why the trail color is not the same as the Sprite color is because the Sprite color effect ranges from 0 to 200 but the pen color effect ranges from zero to one hundred and one go figure anyhow these trails are just way too long they persist forever let's stop that we'll make a new Sprite but before that change the name of sprite 1 to the more sensible particles and now make that new Sprite naming it curtain we call it this because it is used to cover over things that you'll see for starters when flag clicked and then Loop forever and simply drop in a pen arrays all block and run so the previous particle Trail is erased and then the next lines are redrawn each screen refresh now listen you can see here that the screen has to have been erased before the particles draw their lines so how did I know that this arrays would run first if you wondered that then well done that thought doesn't occur to everyone and the reason this erase runs first is because the forever Loop it is within is started before the forever Loop in any of the particle clones those revolutes don't start running until the Clones are created much later on than this green flag script and this gives the erase all blocks forever Loop priority to run first hey shall we get rid of those bright costumes now that we have these nice pen trails click into the particles Sprite and under the when I started clone you'll see the show block now we could just hide this Sprite instead but we're not going to do that because I presently want to do some Collision detection so instead drop in a set ghost effect to 100 that's fully transparent but still collidable useful there beautiful indeed okay still talking about Trails then you may be thinking it would be even nicer to have longer Trails than this if you want to experiment with that then let's try this back in the curtain Sprite create a fully black costume and then in place of the eraser drop in a pen stamp block this will draw that fully black rectangle right over our pen drawings and thus will result in the erasing of the entire scene as before we just need to ensure we position the curtain Sprite in the middle of the screen at zero zero ah but since the curtain is a Sprite it currently shows in front of all our beautiful pens so first we need to hide the curtain Sprite this doesn't stop it from stamping though so that's perfect see it now works just like the arrays block did except that now we have the option to only partially stamp this curtain using a set ghost effect block and setting it to 95 percent that's almost entirely faded away to nothing the fact of this is that everything we paint on the canvas takes a really long time to be erased that's so cool but this is way over the top for my purposes I'm going to set the ghost effect then to 30 percent interestingly you might not think you can see any difference here from our original arrays all solution but if I pause it there can you see the difference it's subtle but it does actually make things feel even more authentic with the movement which is what we are after feel free to play with this and the other values in this project and just have a whole lot of fun experimenting it's what I do but I'm afraid this is the end of this episode but not of this tutorial yes part two will be coming very soon and we'll be adding the collisions the particle bounce and some fantastic sound effects to really round off the package if you've enjoyed what you've seen so far then please do smash the like button under this video and don't forget subscribe to this channel checking that Bell icon to get notified as soon as the next video is released if you manage to create something cool with this tutorial then do post it in the studio Linked In the description of the video I'd love to see what you have made so thanks for watching have a great week ahead and scratch on guys [Music]
Info
Channel: griffpatch
Views: 181,363
Rating: undefined out of 5
Keywords: scratch coding, scratch explosion effect, scratch particles, scratch particle effects, scratch effects, scratch pen, scratch, scratch explosion, how to code particles, scratch firework, scratch fireworks, code fireworks, scratch burst, scratch physics, scratch physics game, scratch tutorials, add boom, fading pen, scratch fade, particles, scratch pen extension, draw lines in scratch, pen extension, pen blocks, scratch pen blocks, fireworks in scratch
Id: 3audNZs7uV8
Channel Id: undefined
Length: 19min 19sec (1159 seconds)
Published: Mon Dec 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.