1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Pac-Man Movements

Discussion in 'Programming' started by Qing Donnie Yoshi, Oct 26, 2019.

  1. Qing Donnie Yoshi

    Qing Donnie Yoshi Member

    Joined:
    Aug 26, 2019
    Posts:
    72
    OK so I did implement that but now he(also the rest) are just stuck going in one direction. should there be and if?
     
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,136
    Make sure you removed the code that's like

    {
    direction=180;
    sprite_index=sprite_left;
    }

    If the ghost is not changing directions, either means
    1. You have that old code still which is setting the direction manually
    2. The ghost is never align to the grid so can't change directions, which in theory shouldn't be happening because we're doing everything the same as with Pac-Man I think
    3. Um.. or some other issue LOL
     
  3. Qing Donnie Yoshi

    Qing Donnie Yoshi Member

    Joined:
    Aug 26, 2019
    Posts:
    72
    I think what it is is the way I've put the code in because I have it looking like this:

    ///Step Event
    if (x - grid/2) mod grid == 0 && (y-grid/2) mod grid == 0 {
    for(var paths, i=0; i<4; i++;)
    paths = -1;
    var n = 0;
    for(i=0; i<360; i+= 90;) {
    if i != (direction + 180) mod 360
    if !place_meeting(x+lengthdir_x(grid/2,i), y+lengthdir_y(grid/2,i), obj_Pac_Collision)
    paths[n++] = i;
    }
    if paths[0] == -1 { //this should NEVER happen, but just in case...
    direction = 270;
    exit;
    }
    if paths[1] == -1 { //this means there is only one path available
    direction = paths[0];
    exit;
    }
    var h = abs(targetX - x);
    var v = abs(targetY - y);
    var dx = point_direction(x,y,targetX,y);
    var dy = point_direction(x,y,x,targetY);
    if h > v {
    //Try to move horizontally
    for(i=0; i<4; i++;)
    if paths == dx {
    direction = dx;
    exit;
    }
    for(i=0; i<4; i++;)
    if paths == dy {
    direction = dy;
    exit;;
    }
    }
    else
    if v > h {
    //Try to move vertically
    for(i=0; i<4; i++;)
    if paths == dy {
    direction = dy;
    exit;
    }
    for(i=0; i<4; i++;)
    if paths == dx {
    direction = dx;
    exit;
    }
    }
    //If both distances are the same or both desired paths are unavailable, pick a random one
    i = irandom(3);
    while paths == -1
    i = ++i mod 4;
    direction = paths;
    }
    {
    move_wrap(1,1,2);
    }
    switch direction {
    case 0: sprite_index = sprite_right; break
    case 90: sprite_index = sprite_up; break
    case 180: sprite_index = sprite_left; break
    case 270: sprite_index = sprite_down; break
    }

    maybe I can put the switch part into create
     
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,136
    No, but put the switch in the End Step event.
     
  5. Qing Donnie Yoshi

    Qing Donnie Yoshi Member

    Joined:
    Aug 26, 2019
    Posts:
    72
    It still didn't work. there must be something wrong with the system.
     
  6. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,136
    So wait, everything worked fine until you added the switch? Just adding the switch shouldn't have broken anything. Go back to the code you had when the ghosts were working just fine.

    Blinky's Create event should initialize all the variables used by the Step Event. Inside Blinky, define his sprite_up, sprite_right, sprite_down, and sprite_left. Give Blinky the End Step event switch.
    Code:
    switch direction {
    case 0: sprite_index = sprite_right; break;
    case 90: sprite_index = sprite_up; break;
    case 180: sprite_index = sprite_left; break;
    case 270: sprite_index = sprite_down; break;
    }
    Put the movement code in Blinky's Step event. Set Blinky's target in the Begin Step event.

    In each of the other ghosts, set their own target in their own Begin Step events. In the Create Event for each of the other ghosts, FIRST call Blinky's Create Event using event_inherited(), then define their own sprite_right, sprite_up, sprite_left, and sprite_down. The other ghosts should have no other events -- just Create and Begin Step.


    Blinky (no parent):
    Create Event:
    1. Define all new variables used by the movement code
    2. Define sprite_right, sprite_up, sprite_left, and sprite_down
    Begin Step Event:
    1. Get the targetX and targetY coordinates
    Step Event:
    1. The AI movement code
    End Step Event:
    1. Switch statement specifying which sprite to use
    Other Ghosts (Blinky set as their parent):
    Create Event:
    1. event_inherited();
    2. Define sprite_right, sprite_up, sprite_left, and sprite_down
    Begin Step Event:
    1. Get the targetX and targetY coordinates
     
    Last edited: Dec 5, 2019
  7. Qing Donnie Yoshi

    Qing Donnie Yoshi Member

    Joined:
    Aug 26, 2019
    Posts:
    72
    So this is what it should look like?:

    Create:
    goHome = false;
    global.PowerPellet = false;
    grid = 32;
    speed = 4;
    sprite_up = Blinky_up;
    sprite_down = Blinky_down;
    sprite_right = Blinky_right;
    sprite_left = Blinky_left;

    Step:
    ///Step Event
    if (x - grid/2) mod grid == 0 && (y-grid/2) mod grid == 0 {
    for(var paths, i=0; i<4; i++;)
    paths = -1;
    var n = 0;
    for(i=0; i<360; i+= 90;) {
    if i != (direction + 180) mod 360
    if !place_meeting(x+lengthdir_x(grid/2,i), y+lengthdir_y(grid/2,i), obj_Pac_Collision)
    paths[n++] = i;
    }
    if paths[0] == -1 { //this should NEVER happen, but just in case...
    direction = 270;
    exit;
    }
    if paths[1] == -1 { //this means there is only one path available
    direction = paths[0];
    exit;
    }
    var h = abs(targetX - x);
    var v = abs(targetY - y);
    var dx = point_direction(x,y,targetX,y);
    var dy = point_direction(x,y,x,targetY);
    if h > v {
    //Try to move horizontally
    for(i=0; i<4; i++;)
    if paths == dx {
    direction = dx;
    exit;
    }
    for(i=0; i<4; i++;)
    if paths == dy {
    direction = dy;
    exit;;
    }
    }
    else
    if v > h {
    //Try to move vertically
    for(i=0; i<4; i++;)
    if paths == dy {
    direction = dy;
    exit;
    }
    for(i=0; i<4; i++;)
    if paths == dx {
    direction = dx;
    exit;
    }
    }
    //If both distances are the same or both desired paths are unavailable, pick a random one
    i = irandom(3);
    while paths == -1
    i = ++i mod 4;
    direction = paths;
    }
    {
    move_wrap(1,1,2);
    }

    Begin Step:
    ///Begin Step Event
    if goHome {
    targetX = 511//whatever the x-coordinate is for the ghost's home;
    targetY = 528//whatever the y-coordinate is for the ghost's home;
    speed = 4;
    }
    else
    if global.PowerPellet {
    targetX = 0;
    targetY = 0;
    speed = 4;
    }
    else {
    targetX = obj_Player_1.x;
    targetY = obj_Player_1.y;
    speed = 4;
    }

    End Step:
    switch direction {
    case 0: sprite_index = sprite_right; break;
    case 90: sprite_index = sprite_up; break;
    case 180: sprite_index = sprite_left; break;
    case 270: sprite_index = sprite_down; break;
    }
     
  8. Qing Donnie Yoshi

    Qing Donnie Yoshi Member

    Joined:
    Aug 26, 2019
    Posts:
    72
    It still didn't work sadly
     
  9. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,136
    Something bugging me about my code that I can't test right now...

    Change this part:

    var dx = point_direction(x,y,targetX,y);
    var dy = point_direction(x,y,x,targetY);


    To this:
    Code:
    var dx,dy;
    if targetX<x dx=180
    else if targetX>x dx = 0;
    else dx = 360;
    if targetY<y dy=90
    else if targetY>y dy=270
    else dy = 360;
     
  10. Qing Donnie Yoshi

    Qing Donnie Yoshi Member

    Joined:
    Aug 26, 2019
    Posts:
    72
    It's still not working for some reason
     
  11. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,136
    I'll try to not get distracted by YouTube tonight and see if there is a glitch and the code I gave you specifically.

    What is the size of Blinky's sprites? More importantly, what is the size of the bounding box or mask for Blinky? Remember, we had to change Pac-Man Sprite because that one was not set right and was too big for your grid. If Blinky is also too big for the grid, you need to make sure he has the same mask that fits the grid like Pac-Man does.
     
  12. Qing Donnie Yoshi

    Qing Donnie Yoshi Member

    Joined:
    Aug 26, 2019
    Posts:
    72
    Blinky is pretty much the same size as Pac-Man as well as the bounding box which is 32. But the thing is before you gave me said code He was moving just fine, just not animating.
     
    Last edited: Dec 10, 2019 at 10:13 PM

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice