Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How do i make shoting using the pen extansion?
- pHFilip77
-
Scratcher
4 posts
How do i make shoting using the pen extansion?
So i want to make a run n' gun game and the problem is i don't know how do i make shoting using the pen.
can someone please send the bloks i need to use?
can someone please send the bloks i need to use?
- -EmeraldThunder-
-
Scratcher
1000+ posts
How do i make shoting using the pen extansion?
Does something like this help?
https://scratch.mit.edu/projects/715630908/
It's a bit complicated, so I'll explain it.
Our first script contains the player control code:

At the start, we are resetting the bullet list, setting the shot cooldown to 0.2 seconds and setting the last variable to 0.
Then we're starting an infinite loop.
In this loop, we are allowing the player to move 10 pixels with the arrow keys (a boolean returns either 1 or 0 as an integer, I'm exploiting this for easy movement code, however you can change this later if you would like.)
Next we're aligning the player with our mouse pointer, turning right 90 degrees to account for the alignment in the costume editor.
After this, we are checking to see if the mouse is pressed or not.
If it is, we subtract the cooldown from the timer and see if it is greater than the last time we fired a bullet. This tells us if it has been 0.2 seconds since the last firing.
Then we're calling the Add bullet function with the X, Y and Direction parameters, the other parameters are to generate a random colour and can be removed.
Our next script is the Add bullet custom block.

In this script, to start off with, we are adding the given location to the list, plus a few pixels to move it to the top of the barrel.
We are subtracting 90 from the direction to account for the change we made when positioning the player.
We are using the mod function (finds the remainder of a division of two numbers) to force this number to be in the range of 0 to 360.
If we find the sine of a direction, this will give us the change in x required to move one step in that direction.
If we find the cosine of a direction, this will give us the change in y required to move one step in that direction.
We are multiplying this by 54 as that is the distance in pixels from the centre of the player to the top of the barrel.
I'm not going to explain the next block. All it does is converts our RGB to a hex value and is not required.
After this we are adding direction to the list, taking away 90 and forcing in into the 0-360 range.
In our next script, we are creating the loop for our bullet sprite.
The bullet sprite is responsible for rendering every bullet in the list.

In the first line, we are setting the bullet speed to 10.
Then we enter the bullet loop.
In this loop, we are first erasing all the pen on the screen and then calling the render all and update all functions.
The render all function draws all of our bullets to the screen.

Firstly, we are hiding the sprite; there's no need to do this with a blank sprite but, it is best practice.
Next we are setting the pen size to 10 which is the size our bullets will appear on the stage.
Next we are resetting the i variable, which is short for increment.
We are repeating this code block for a quarter of the length of this list. This is because every bullet takes up 4 items.
After this, we set the position to the first and second items of the current bullet; the position we added in the previous function.
Then, we are setting the pen colour to be the hex, the third item in the sub-list and creating a dot by putting the pen down and instantly putting it up.
Finally, we change i by 4 to move on to the next bullet.
Finally, we have the update all function. This moves the bullets across the screen and deletes them if they go out of bounds.

Firstly, we are setting the increment variable to 1 and iterating through every bullet.
Here we are, again, using the principle of using the sin and cos of an angle to find one step. We are adding the first and second elements to these values, multiplied by the bullet speed, to find the new position for that frame.
After this, we are checking to see if the bullet is out of bounds.
If you have any obstacles in your game, this is where your collision formula will go.
The abs function converts a number to be positive, which makes this script a lot simpler.
Since the scratch stage is 240x180, we are using these dimensions to check if the bullet is out of bounds.
If it is out of bounds, we will add the index of the bullet sub-list to the delete list.
The final thing to do, in this loop, is to increment the i variable to move to the next bullet.
In a new loop, we are repeating until the length of delete is 0.
Here we set i to the first item in the list, since we are deleting the item later we can just use a static index.
Next we are repeating 4 times, the number of properties of a bullet.
In this loop, we are deleting the item at i in the bullets list, deleting the entire bullet.
Finally, we are deleting the item at index 1 of the delete list.
If you have any more questions, feel free to ask.
https://scratch.mit.edu/projects/715630908/
It's a bit complicated, so I'll explain it.
Our first script contains the player control code:

At the start, we are resetting the bullet list, setting the shot cooldown to 0.2 seconds and setting the last variable to 0.
Then we're starting an infinite loop.
In this loop, we are allowing the player to move 10 pixels with the arrow keys (a boolean returns either 1 or 0 as an integer, I'm exploiting this for easy movement code, however you can change this later if you would like.)
Next we're aligning the player with our mouse pointer, turning right 90 degrees to account for the alignment in the costume editor.
After this, we are checking to see if the mouse is pressed or not.
If it is, we subtract the cooldown from the timer and see if it is greater than the last time we fired a bullet. This tells us if it has been 0.2 seconds since the last firing.
Then we're calling the Add bullet function with the X, Y and Direction parameters, the other parameters are to generate a random colour and can be removed.
Our next script is the Add bullet custom block.

In this script, to start off with, we are adding the given location to the list, plus a few pixels to move it to the top of the barrel.
We are subtracting 90 from the direction to account for the change we made when positioning the player.
We are using the mod function (finds the remainder of a division of two numbers) to force this number to be in the range of 0 to 360.
If we find the sine of a direction, this will give us the change in x required to move one step in that direction.
If we find the cosine of a direction, this will give us the change in y required to move one step in that direction.
We are multiplying this by 54 as that is the distance in pixels from the centre of the player to the top of the barrel.
I'm not going to explain the next block. All it does is converts our RGB to a hex value and is not required.
After this we are adding direction to the list, taking away 90 and forcing in into the 0-360 range.
In our next script, we are creating the loop for our bullet sprite.
The bullet sprite is responsible for rendering every bullet in the list.

In the first line, we are setting the bullet speed to 10.
Then we enter the bullet loop.
In this loop, we are first erasing all the pen on the screen and then calling the render all and update all functions.
The render all function draws all of our bullets to the screen.

Firstly, we are hiding the sprite; there's no need to do this with a blank sprite but, it is best practice.
Next we are setting the pen size to 10 which is the size our bullets will appear on the stage.
Next we are resetting the i variable, which is short for increment.
We are repeating this code block for a quarter of the length of this list. This is because every bullet takes up 4 items.
After this, we set the position to the first and second items of the current bullet; the position we added in the previous function.
Then, we are setting the pen colour to be the hex, the third item in the sub-list and creating a dot by putting the pen down and instantly putting it up.
Finally, we change i by 4 to move on to the next bullet.
Finally, we have the update all function. This moves the bullets across the screen and deletes them if they go out of bounds.

Firstly, we are setting the increment variable to 1 and iterating through every bullet.
Here we are, again, using the principle of using the sin and cos of an angle to find one step. We are adding the first and second elements to these values, multiplied by the bullet speed, to find the new position for that frame.
After this, we are checking to see if the bullet is out of bounds.
If you have any obstacles in your game, this is where your collision formula will go.
The abs function converts a number to be positive, which makes this script a lot simpler.
Since the scratch stage is 240x180, we are using these dimensions to check if the bullet is out of bounds.
If it is out of bounds, we will add the index of the bullet sub-list to the delete list.
The final thing to do, in this loop, is to increment the i variable to move to the next bullet.
In a new loop, we are repeating until the length of delete is 0.
Here we set i to the first item in the list, since we are deleting the item later we can just use a static index.
Next we are repeating 4 times, the number of properties of a bullet.
In this loop, we are deleting the item at i in the bullets list, deleting the entire bullet.
Finally, we are deleting the item at index 1 of the delete list.
If you have any more questions, feel free to ask.
Last edited by -EmeraldThunder- (July 20, 2022 08:49:24)
- pHFilip77
-
Scratcher
4 posts
How do i make shoting using the pen extansion?
@-EmeraldThunder- bro you dont know how much you helped ty. Currently i cant test it out but it this works ur a legend
Last edited by pHFilip77 (July 20, 2022 15:24:40)
- cIoudyness
-
Scratcher
500+ posts
How do i make shoting using the pen extansion?
linking a project with the helpful code would be awesome. copying this from screenshots seems like a humongous pain
- -EmeraldThunder-
-
Scratcher
1000+ posts
How do i make shoting using the pen extansion?
(#4)I did. It's at the top.
linking a project with the helpful code would be awesome. copying this from screenshots seems like a humongous pain
https://scratch.mit.edu/projects/715630908/
- Discussion Forums
- » Help with Scripts
-
» How do i make shoting using the pen extansion?


