Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How do I make a snake?
- Mrrecycle
-
Scratcher
6 posts
How do I make a snake?
I want to make something that has the same mechanic of something like the Slither.io snakes. Please help.
- Parametric
-
Scratcher
91 posts
How do I make a snake?
That's gonna be pretty difficult, but I'll try anyway.
1) So first what you'll wanna do is make a sprite with two separate costumes, the head & a small segment of the body. It's also preferable that both are facing the right in the paint editor & are around the centre of the crosshair.
2) Then what you'll wanna do is make a variable for how long the snake is. Remember to select “for this sprite only” when making the variable or else it won't work.
3) Now it's time to start programming. Let's start with what the actual sprite will do.
All this script will do is set it to the right costume & make it follow the mouse.
4) Now for the fun part, make the snake grow. This will be really tricky & chances are I'll mess something up here, but hopefully you should be able to follow my line of reasoning.
So you'll want to create three lists: x-history, y-history & d-history.
5) Next, you'll want to constantly insert the sprite's current co-ordinates at the beginning of each of these lists, like so:
So you'll want to create three lists: x-history, y-history & d-history.
6) Now that we've set all that up, it's time for the snake to actually grow. Now, we'll need a signal for the snake to actually gain a segment. To do this, we'll need to make a brodcast indicating when it grows. We'll just call this “indicator” for now.
7) Set the script up so when the indicator is 1, the length changes by 1 & the sprite makes a clone of itself.
Remember that whenever you turn this indicator on, you must immediately turn it off afterwards, otherwise it will extend infinitely.
8) So now we make the snake grow. To do this, we must consider the length of the body costume. To find this, just go to the paint editor & under the body costume's name, you'll see two numbers with an x in between them. The first one of those numbers is your costume length.
Anyway, here's what you do
What this will do is calculate how far back it can remember its x & y positions & direction & then go back to those co-ordinates a certain amount depending on what length the clone is set to.
9) Now it's time to make the snake's death. All you'll need to do for this one is put a certain colour at the tip of the head costume that isn't used anywhere else (let's just say red for example.
Then all you have to do is this.
& that's it. Now you should have your game engine practically complete.
Bye
1) So first what you'll wanna do is make a sprite with two separate costumes, the head & a small segment of the body. It's also preferable that both are facing the right in the paint editor & are around the centre of the crosshair.
2) Then what you'll wanna do is make a variable for how long the snake is. Remember to select “for this sprite only” when making the variable or else it won't work.
3) Now it's time to start programming. Let's start with what the actual sprite will do.
when green flag clickedTry different numbers for the movement speed & see which one you're comfortable with. For now I'll just refer to it as “movement speed”.
set [length v] to [0]
switch costume to [head v]
forever
point towards [mouse pointer v]
move ((movement speed)) steps
end
All this script will do is set it to the right costume & make it follow the mouse.
4) Now for the fun part, make the snake grow. This will be really tricky & chances are I'll mess something up here, but hopefully you should be able to follow my line of reasoning.
So you'll want to create three lists: x-history, y-history & d-history.
when green flag clicked
set [length v] to [0]
delete (all v) of [x-history v]
delete (all v) of [y-history v]
delete (all v) of [d-history v]
switch costume to [head v]
forever
point towards [mouse pointer v]
move ((movement speed)) steps
end
5) Next, you'll want to constantly insert the sprite's current co-ordinates at the beginning of each of these lists, like so:
So you'll want to create three lists: x-history, y-history & d-history.
when green flag clicked
set [length v] to [0]
delete (all v) of [x-history v]
delete (all v) of [y-history v]
delete (all v) of [d-history v]
switch costume to [head v]
forever
point towards [mouse pointer v]
move ((movement speed)) steps
insert ((x position)) at (1 v) of [x-history v]
insert ((y position)) at (1 v) of [y-history v]
insert ((direction)) at (1 v of [d-history v]
end
6) Now that we've set all that up, it's time for the snake to actually grow. Now, we'll need a signal for the snake to actually gain a segment. To do this, we'll need to make a brodcast indicating when it grows. We'll just call this “indicator” for now.
7) Set the script up so when the indicator is 1, the length changes by 1 & the sprite makes a clone of itself.
when green flag clicked
set [length v] to [0]
delete (all v) of [x-history v]
delete (all v) of [y-history v]
delete (all v) of [d-history v]
switch costume to [head v]
forever
point towards [mouse pointer v]
move ((movement speed)) steps
insert ((x position)) at (1 v) of [x-history v]
insert ((y position)) at (1 v) of [y-history v]
insert ((direction)) at (1 v of [d-history v]
if <[(indicator)] = [1]> then
change [length v] by (1)
create clone of [myself v]
end
end
Remember that whenever you turn this indicator on, you must immediately turn it off afterwards, otherwise it will extend infinitely.
8) So now we make the snake grow. To do this, we must consider the length of the body costume. To find this, just go to the paint editor & under the body costume's name, you'll see two numbers with an x in between them. The first one of those numbers is your costume length.
Anyway, here's what you do
when green flag clicked
set [length v] to [0]
delete (all v) of [x-history v]
delete (all v) of [y-history v]
delete (all v) of [d-history v]
switch costume to [head v]
forever
point towards [mouse pointer v]
move ((movement speed)) steps
insert ((x position)) at (1 v) of [x-history v]
insert ((y position)) at (1 v) of [y-history v]
insert ((direction)) at (1 v of [d-history v]
if <[(indicator)] = [1]> then
change [length v] by (1)
create clone of [myself v]
end
end
when I start as a clone
switch costume to [body v]
forever
go to x: ((item ((((costume length) / (movement speed)) * (length) v) of [x-history v] :: list)) y: ((item ((((costume length) / (movement speed)) * (length) v) of [y-history v] :: list))
point in direction ((item ((((costume length) / (movement speed)) * (length) v) of [d-history v] :: list))
end
What this will do is calculate how far back it can remember its x & y positions & direction & then go back to those co-ordinates a certain amount depending on what length the clone is set to.
9) Now it's time to make the snake's death. All you'll need to do for this one is put a certain colour at the tip of the head costume that isn't used anywhere else (let's just say red for example.
Then all you have to do is this.
when green flag clicked
set [length v] to [0]
delete (all v) of [x-history v]
delete (all v) of [y-history v]
delete (all v) of [d-history v]
switch costume to [head v]
forever
point towards [mouse pointer v]
move ((movement speed)) steps
insert ((x position)) at (1 v) of [x-history v]
insert ((y position)) at (1 v) of [y-history v]
insert ((direction)) at (1 v of [d-history v]
if <[(indicator)] = [1]> then
change [length v] by (1)
create clone of [myself v]
end
end
when I start as a clone
switch costume to [body v]
forever
go to x: ((item ((((costume length) / (movement speed)) * (length) v) of [x-history v] :: list)) y: ((item ((((costume length) / (movement speed)) * (length) v) of [y-history v] :: list))
point in direction ((item ((((costume length) / (movement speed)) * (length) v) of [d-history v] :: list))
if <touching color [#eb586d] ?> then
stop [all v]
end
end
& that's it. Now you should have your game engine practically complete.
Bye
Last edited by Parametric (Dec. 13, 2017 01:53:40)
- Mrrecycle
-
Scratcher
6 posts
How do I make a snake?
Dang, you are a pro, Parametric. But, could you also tell me how to make it so that each of those segments can have health and help me make a tail and have all the segments go up to replace the segment in front of it once the segment in front of it has been destroyed? Thanks!
- Discussion Forums
- » Help with Scripts
-
» How do I make a snake?

