• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Snake and automatic size increasing.

PeXi81

Member
I have object called obj_snakehead and I want it to increase size every time it moves. Kinda like it makes line when it moves. How can I make it when using drag and drop? I´m making maze like game where it starts with only snake head object. When it starts to move I would like it to make line behind it. So not like ordinary (when it eats food it grows) but when it moves it grows automatically. How can I make that happend? If you still don´t understand what I ment...when it makes to the end there should be long line behind it. And of course...if head part touches the following line...Game Over! Please help me with this.
 

TheouAegis

Member
Code:
if x mod sprite_width == 0 and y mod sprite_height == 0 {
    with instance_create_depth(xstart, ystart, depth, obj_Snake_Body) {
        //set image_index based on angle from (xstart,ystart) to (x,y)
    }
    xstart = x;
    ystart = y;
}
Not in D&D cuz I'm at work on my phone.
 

PeXi81

Member
Code:
if x mod sprite_width == 0 and y mod sprite_height == 0 {
    with instance_create_depth(xstart, ystart, depth, obj_Snake_Body) {
        //set image_index based on angle from (xstart,ystart) to (x,y)
    }
    xstart = x;
    ystart = y;
}
Not in D&D cuz I'm at work on my phone.
Thanks! :)
 

PeXi81

Member
That worked fine but how can I make obj_snakehead to collide with obj_snakebody and then THE END? I've tried everything but nothing works. I get closest when I managed to get this work BUT...collision happens right after game starts so there's nothing but body left.
 

TheouAegis

Member
When you move obj_snakehead, check if there is obj_snakebody at x,y. You don't collide per se, you just check if they have overlapping coordinates.
 

PeXi81

Member
Ok but where should I put that code which checks if there is overlap situation? Because that's the reason why it won't work. What kind piece of code it should be that checks this out? At first I thought that I should have alarm event for this but that's not the thing. So can you please help with this too? :)
 

PeXi81

Member
Create:

if x mod sprite_width == 0 and y mod sprite_height == 0 {
with instance_create_depth(xstart, ystart, depth, obj_snakebody) {
//set image_index based on angle from (xstart,ystart) to (x,y)
}
xstart = x;
ystart = y;
}

-----------------------------------------------------------------------------------
Step (Movement):

/// @description Movement
// Step Movement

// Rotate to left
if(keyboard_check(vk_left))
{
image_angle= direction + 3;
direction= direction + 3;
}

// Rotate to right
if(keyboard_check(vk_right))
{image_angle= direction - 3;
direction= direction - 3;
}

// Snake will move if press up keyboard
if(keyboard_check(vk_up))
if (move == 1){
speed=4;
}
move =1;

// Make body
if(move==1){
var ints= instance_create_layer(obj_snakehead.x,obj_snakehead.y, "instances",obj_snakebody);
}
move_wrap(true,true,sprite_width/2);
 

TheouAegis

Member
if x mod sprite_width == 0 and y mod sprite_height == 0 {
with instance_create_depth(xstart, ystart, depth, obj_snakebody) {
//set image_index based on angle from (xstart,ystart) to (x,y)
}
xstart = x;
ystart = y;
}
This part should be here:

// Make body
if(move==1){
And this part...

if(move==1){
var ints= instance_create_layer(obj_snakehead.x,obj_snakehead.y, "instances",obj_snakebody);
}
...is just creating infinite snakebody because you never set move to 0.


// Snake will move if press up keyboard
if(keyboard_check(vk_up))
if (move == 1){
speed=4;
}
move =1;
You are missing a set of brackets, so move is set to 1 every step. If you add the brackets...
Code:
// Snake will move if press up keyboard
if(keyboard_check(vk_up)) {
if (move == 1){
speed=4;
}
move =1;}
...speed will not change for the first frame. This is not a big deal. What is a big deal is you not only never set move to 0, you also never set speed to 0. Nothing in your code tells the snakehead to stop, so all of those conditionals are pointless.


// Rotate to left
if(keyboard_check(vk_left))
{
image_angle= direction + 3;
direction= direction + 3;
}

// Rotate to right
if(keyboard_check(vk_right))
{image_angle= direction - 3;
direction= direction - 3;
}
Is this for a maze game? I don't think I've ever seen a maze game that increases direction by 3. By 90, yes, but not by 3. That opens up the possibility (practically a guarantee) that x mod anything and y mod anything will never be 0. Which, again, I have never seen in a maze game. You could of course use floor(x) mod sprite_width==0 and floor(y) mod sprite_height==0, but still, your snakehead is going to be running into a lot of walls.

How big is the snakebody sprite? If it:s either 4 pixels wide or 4 pixels high, then fine. You could create snakebody every step and set its image_angle to your image_angle. That's going to be a lot of snakebody instances slowing down the game, though.

As for colliding, since we now know you are facing float issues, you could try for your collision detection
Code:
with obj_snakebody {
    if abs(x-other.x) < 2 && abs(y-other.y) < 2 {
        move = 0;
        speed = 0;
        show_message("Game Over")
    }
}
 

PeXi81

Member
As a matter of fact this should be maze type of game. That's why body increases infinitely. This is also for testing purposes. I'm just facing issues with body part because head part is just going right through it. What I wanted is when head part collides with objects: Game Over! That works. But when it supposed to collide with that body part...it does nothing but just go through it.
 
Top