【GAMEMAKER】Random Level Generator

 Information about object: obj_player

Sprite: spr_idle_down
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:
No Physics Object
Create Event:
execute code:

///set up
enum player_state {
    idle,
    up,
    down,
    left,
    right
    }

dir=player_state.down;
is_moving=false;
image_speed=0.5;
Step Event:
execute code:

///keypress code
if (keyboard_check(vk_left))
{
    dir=player_state.left;
    is_moving=true;
}
else
if (keyboard_check(vk_right))
{
    dir=player_state.right;
    is_moving=true;
}
else
if (keyboard_check(vk_up))
{
    dir=player_state.up;
    is_moving=true;
}
else
if (keyboard_check(vk_down))
{
    dir=player_state.down;
    is_moving=true;
}
else
{
    is_moving=false;
}
execute code:

///movement
if is_moving
{
    switch (dir)
    {
        case player_state.up:
        if !position_meeting(x,y-4,obj_solid_base) y -= 4;
        break;

        case player_state.down:
        if !position_meeting(x,y+4,obj_solid_base) y += 4;
        break;

        case player_state.left:
        if !position_meeting(x-4,y,obj_solid_base) x -= 4;
        break;
   
        case player_state.right:
        if !position_meeting(x+4,y,obj_solid_base) x += 4;
        break;
    }
}
depth=-y;
execute code:

///animation
if is_moving
{
    switch (dir)
    {
        case player_state.up:
        sprite_index=spr_walk_up;
        break;

        case player_state.down:
        sprite_index=spr_walk_down;
        break;

        case player_state.left:
        sprite_index=spr_walk_left;
        break;
   
        case player_state.right:
        sprite_index=spr_walk_right;
        break;
    }
}
else
{
    switch (dir)
    {
        case player_state.up:
        sprite_index=spr_idle_up;
        break;

        case player_state.down:
        sprite_index=spr_idle_down;
        break;

        case player_state.left:
        sprite_index=spr_idle_left;
        break;
   
        case player_state.right:
        sprite_index=spr_idle_right;
        break;
    }
}

Information about object: obj_cactus
Sprite: spr_cactus
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: obj_elements_parent
Children:
Mask:
No Physics Object
Information about object: obj_flag
Sprite: spr_flag
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: obj_elements_parent
Children:
Mask:
No Physics Object
Information about object: obj_mushroom
Sprite: spr_mushroom
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: obj_elements_parent
Children:
Mask:
No Physics Object
Information about object: obj_elements_parent
Sprite:
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children
obj_cactus
obj_flag
obj_mushroom
Mask:
No Physics Object
Create Event:
execute code:

instance_create(x,y,obj_solid_base);
depth=-y;

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

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

randomize();
path=path_add();
alarm[0]=room_speed;

Alarm Event for alarm 0:
execute code:



//place wall borders
size=room_width/32-1;
for (var loop = 1; loop < size; loop += 1)
{
    instance_create(loop*32,32,obj_wall);
    instance_create(loop*32,0,obj_wall);
    instance_create(loop*32,968,obj_wall);
    instance_create(loop*32,1000,obj_wall);
    instance_create(32,32*loop+16,obj_wall);
    instance_create(0,32*loop+16,obj_wall);
    instance_create(968,32*loop+16,obj_wall);
    instance_create(1000,32*loop+16,obj_wall);
}
instance_create(16,16,obj_wall);
instance_create(16,984,obj_wall);
instance_create(984,16,obj_wall);
instance_create(984,1000,obj_wall);
alarm[1]=room_speed;
Alarm Event for alarm 1:
execute code:

repeat(5)//do this 5 times
{
    //get two random points atleast 100 from room edge
    var xx1=irandom_range(100,room_width-100);
    var yy1=irandom_range(100,room_height-100);
    var xx2=irandom_range(100,room_width-100);
    var yy2=irandom_range(100,room_height-100);
    //add points to a path
    path_add_point(path,xx1,yy1,3);
    path_add_point(path,xx2,yy2,3);
    //get length of path
    var size=floor(path_get_length(path)); show_debug_message(size);
    //divide by 64
    var division=floor(size/64); show_debug_message(division);
    //get total blocks to draw
    total=floor(size/division); show_debug_message(total);
    for (var i = 1; i < total; i += 1)
    {
        var point=(1/total)*i;//get a value based on i
        var placex=path_get_x(path,point);//get x at point on path
        var placey=path_get_y(path,point);//get y at point on path
        instance_create(placex,placey,obj_road);//create a road object
    }
    path_clear_points(path);
}

alarm[2]=room_speed;
Alarm Event for alarm 2:
execute code:

//draw some lakes
repeat(12)
{
    do
    {
        xx = random(room_width) div 32*32;
        yy = random(room_height) div 32 *32;
    }
    until (!position_meeting(xx, yy, all))
    instance_create(xx,yy,obj_water);
    instance_create(xx+32,yy,obj_water);
    instance_create(xx+32,yy+32,obj_water);
    instance_create(xx+64,yy,obj_water);
    instance_create(xx+64,yy+32,obj_water);
    instance_create(xx+64,yy+64,obj_water);
}
//draw some square lakes
repeat(12)
{
   do
    {
        xx = random(room_width) div 32*32;
        yy = random(room_height) div 32 *32;
    }
   until (!position_meeting(xx, yy, all))
    instance_create(xx,yy,obj_water);
    instance_create(xx+32,yy,obj_water);
    instance_create(xx,yy+32,obj_water);
    instance_create(xx+32,yy+32,obj_water);
}
alarm[3]=room_speed;

Alarm Event for alarm 3:
execute code:

//add some cactus / mushroom
repeat(20)
{
    do
    {
        xx = random(room_width-64) div 32 * 32 +32;
        yy = random(room_height-64) div 32* 32 + 32;
    }
    until (!position_meeting(xx, yy, all))
    instance_create(xx,yy,choose(obj_mushroom,obj_cactus));
}

path_delete(path);
instance_destroy();

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

No comments:

Top 3 UX Design Articles of 2024 to Remember

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

Contact Form

Name

Email *

Message *