• 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!

I need a little help with programming an object to travel in a maze.

Ok, so you guys know that Rats! game where you are using bombs, gas, sterilizers in order to keep the rat population down and eliminate them all before they overwhelm you and you lose the game right?
Well, I had a lot of fun with that game, and I want to RE-Create that game and adjust it to MY tastes.
Right now I have some squares (masks for the rats. Don't have sprites for the rats yet) that are able to move around a maze. It can pick one of four directions when it gets to an intersection a certain variable that counts down reaches zero.
I will show you my code real quick.
I apologize if I'm using too many screens as of course gml requires you to split codes for objects in tothings like create step alarm draw, and such.

GML:
//obj_rat create event

dir=choose(90,180,270,0); //0° is right, 90° is up, 180° is left and 270° is down.
ptm = -1; //path to move. this is counted down in order to keep a direction. then check and see if there is an opening in
//front or on the sides of it.
GML:
//obj_rat begin step

if ptm == 0
{

dir=choose(90,180,270,0); //0° is right, 90° is up, 180° is left and 270° is down.

choosepath(x,y); // this is a user defined function that creates another object that determines how many spaces the rat should move before deciding to change direction.
alarm[0] = 60;
}

if ptm > 0

{


switch (dir)

{
    case 90: { if !place_meeting(x,y-1,obj_wall)  {y-=mspeed; ptm -= mspeed;}  if place_meeting(x,y-1,obj_wall) {ptm = 0;} break;}
    case 180: { if !place_meeting(x-1,y,obj_wall) {x-=mspeed; ptm -= mspeed;} if place_meeting(x-1,y,obj_wall) {ptm = 0;} break;}
    case 270: { if !place_meeting(x,y+1,obj_wall)  {y+=mspeed; ptm -= mspeed;} if place_meeting(x,y+1,obj_wall) {ptm = 0;} break;}
    case 0: {if !place_meeting(x+1,y,obj_wall)   {x+=mspeed; ptm -= mspeed;} if place_meeting(x+1,y,obj_wall) {ptm = 0;} break;}
}

}
GML:
//script choosepath

function choosepath(x,y){
   
   
    pathchooser = instance_create_layer(x,y,layer,obj_rat_pathchooser);
   
   

   

}
GML:
// obj_rat_pathchooser create event

belongto = instance_place(x,y,obj_rat); // this is here to add a number to the
//object instance that created it. it's not perfect of course, as what if the
//obj_rat.ptm becomes 0 and there are TWO rats overlapping eachother and
//this object is created?


dir = belongto.dir; //direction. I am afraid of the "black box" brought on by
//built in variables.
spacesmoved = 0; //this is determined by how many multiples of 32 this object
//has moved before it reached a wall.



   

        switch (dir) //0° is right, 90° is up, 180° is left and 270° is down.

                    {
                        case 90: {movespaces(0,-32,0,-1);}  break;
                        case 180: {movespaces(-32,0,-1,0);} break;
                        case 270: {movespaces(0,32,0,1);}  break;
                        case 0: {movespaces(32,0,1,0);} break;
                    }

belongto.ptm = 32*irandom_range(1,spacesmoved);
belongto.cancreate = true;
instance_destroy();
GML:
//the move spaces function

function movespaces(xadd,yadd,xnext,ynext){

do {x+=xadd; y+=yadd;  spacesmoved += 1;}  until (place_meeting(x+xnext,y+ynext,obj_wall));

if spacesmoved <= 1 && place_meeting(x+xnext,y+ynext,obj_wall)

{
with (belongto) {ptm = 0;}
instance_destroy();

}

}

ok, my problem with this code is that it's RARE for the rat to make its way to the middle of the level. They seem to love hanging around on the edge of the level.
even though that's quite typical for rats, I want my level to be HARDer and I would rather it not be easy to wipe all the rodents out with one or two bombs that would clear an entire line.
is it possible for anybody to help me program the rats so that they would turn more often where there is the option to keep straight or turn left or right?
 
Last edited:

Nidoking

Member
I don't really see the point of calculating a distance in advance if you want to go that way. Just go forward until there's an intersection, and then choose a direction based on the available directions. If you want to make certain choices more likely, use a weighted random instead of choose. I did this for a Pac-Man style game once, and weighted directions based on where the player was and whether they would take the ghost closer to the player or farther away, with different weights for each situation and each type of intersection.
 

TheouAegis

Member
Study up on the Pac-Man AI. However in the case of Pac-Man, the ghosts were not allowed to move backwards, but a rat is very flexible and agile as long as it is not fat, so a rat should be allowed to move backwards, but only at a dead end. By the sounds of it, whereas the ghost had Pac-Man as a Target, or Ikea spaces in front of Pac-Man as their target, he would want you to Target to be the center of the maze, or any appetizing traps the player might set.
 
wouldn't you have to place an object in every single intersection for the rat to come into contact with in order to activate a variable and then make the choice of whether to turn or keep straight? Well that sounds fine I guess. But it probably would be a nightmare to do when editing the room of new levels.
 

TheouAegis

Member
um, no. Your maze will most likely be grid aligned. So you check when the rat is in the middle of a cell, then check which directions are available, then decide where you want to go.
 

Nidoking

Member
wouldn't you have to place an object in every single intersection for the rat to come into contact with in order to activate a variable and then make the choice of whether to turn or keep straight?
Why would you need to do that? There are four directions for movement, right? Up, down, left, right. You can check each direction to see if movement in that direction is possible. If three or four directions are available, you're at an intersection. If only two directions are available, then you're not.
 
Top