【GameMaker】 Sokoban

/// soko_move(dx, dy) script
// Attempts to move the current instance, puhsing things around as needed.

// calculate new coordinates:
var dx = argument0, nx = x + dx;
var dy = argument1, ny = y + dy;

if (place_meeting(nx, ny, obj_wall))
{
    // if there's a wall there, definitely can't move there
    return false;
}
else if (place_free(nx, ny)) 
{
    // if there's nothing there, just move there
    x = nx; y = ny;
    return true;
}
else
{
    // otherwise attempt to move every pushable object touched:
    with (obj_pushable) if (place_meeting(x - dx, y - dy, other))
    {
       // (if any object fails to move, stop trying to move them)
       if (!soko_move(dx, dy)) return false;
     
   }
    // if it's now possible to move to the position, do that
    if (place_free(nx, ny))
    {
        x = nx; y = ny;
        return true;
    }
    else return false;
}


Information about object: obj_pushable
Sprite:
Solid: true
Visible: true
Depth: 0
Persistent: false
Parent:
Children
obj_block
obj_player
Mask:
No Physics Object

Information about object: obj_block
Sprite: spr_block
Solid: true
Visible: true
Depth: 0
Persistent: false
Parent: obj_pushable
Children:
Mask:
No Physics Object
Begin Step Event:
execute code:

global.collisions=0;
End Step Event:
execute code:

if position_meeting(x,y,obj_hole)
{
    global.collisions++;
}


Information about object: obj_player
Sprite: spr_player
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: obj_pushable
Children:
Mask:
No Physics Object
Create Event:
execute code:

move_dx = 0;
move_dy = 0;
move_amt = 0;
global.collisions=0;//set up

Step Event:
execute code:

if (move_amt > 0)
{
    // moving towards destination
    if (soko_move(move_dx, move_dy))
    {
        move_amt -= 1;
    }
    else move_amt = 0; // if hit a wall, stop moving
}
else
{
    var spd = 4; // movement speed (grid size should divide by it w/o remainder)
    move_amt = 32 div spd; // calculate number of steps for movement
    move_dx = 0;
    move_dy = 0;
    if (keyboard_check(vk_left)) && !((position_meeting(x-18,y,obj_block) && (position_meeting(x-50,y,obj_block))))
    {
        move_dx = -spd;
    }
    else if (keyboard_check(vk_right))&& !((position_meeting(x+18,y,obj_block) && (position_meeting(x+50,y,obj_block))))
    {
        move_dx = spd;
    }
    else if (keyboard_check(vk_up)) && !((position_meeting(x,y-18,obj_block) && (position_meeting(x,y-50,obj_block))))
    {
        move_dy = -spd;
    }
    else if (keyboard_check(vk_down))&& !((position_meeting(x,y+18,obj_block) && (position_meeting(x,y+50,obj_block))))
    {
        move_dy = spd;
    }
    else move_amt = 0; // don't move if no buttons are pressed
}
End Step Event:
execute code:

if global.collisions==instance_number(obj_hole) && move_amt <= 0//check if all crates in place and stopped movi
{
    show_message("You Win");
    room_goto_next();//goto next room
    
}
Draw Event:
execute code:

draw_self();
draw_text(20,20,"Done="+string(global.collisions)+" of "+string(instance_number(obj_hole)));


Information about object: obj_wall
Sprite: spr_wall
Solid: true
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object


Information about object: obj_hole
Sprite: spr_hole
Solid: false
Visible: true
Depth: 3
Persistent: false
Parent:
Children:
Mask:
No Physics Object


Information about object: obj_restart
Sprite: spr_restart
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Mouse Event for Left Released:
execute code:

room_restart();


Information about object: obj_game_end
Sprite: sprite6
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Mouse Event for Left Released:
execute code:

game_restart();

No comments:

Top 3 UX Design Articles of 2024 to Remember

Based on most subscriptions ͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­...

Contact Form

Name

Email *

Message *