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")
}
}