【GameMaker】Predicting the Motion of an Object using motion predict script

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

//set initial values
strength=0;
angle=0;
global.player=1;//set player to go first
Alarm Event for alarm 0:
execute code:

global.player=2;//allow next player to go
Step Event:
execute code:

if global.player==1//check if players go
{
    strength=distance_to_point(mouse_x,mouse_y)/50;//calculate strength based on distance to mouse
    angle=point_direction(x,y,mouse_x,mouse_y);//get the angle
    if mouse_check_button_released(mb_left)//do this when mouse released
    {
        bullet=instance_create(x,y,obj_bullet_1);//create a bullet and assign
        bullet.speed=strength;//set its speed
        bullet.direction=angle;//set direction
        global.player=0;//prevent shooting
        alarm[0]=room_speed*2;//start an alarm
        
    }
}
Draw Event:
execute code:

draw_self();
if global.player==1// do this if players go
{
    draw_text(x,y-40,"strength="+string(strength));
    draw_text(x,y-80,"angle="+string(angle));
    draw_line(x,y,mouse_x,mouse_y);
}

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

//set initial values
strength=0;
angle=0;

Alarm Event for alarm 0:
execute code:

global.player=1;//allow next player to go
Step Event:
execute code:

if global.player==2//check if players go
{
    strength=distance_to_point(mouse_x,mouse_y)/50;//calculate strength based on distance to mouse
    angle=point_direction(x,y,mouse_x,mouse_y);//get the angle
    if mouse_check_button_released(mb_left)//do this when mouse released
    {
        bullet=instance_create(x,y,obj_bullet_2);//create a bullet and assign
        bullet.speed=strength;//set its speed
        bullet.direction=angle;//set direction
        global.player=0;//prevent shooting
        alarm[0]=room_speed*2;//start an alarm
        
    }
}
Draw Event:
execute code:

draw_self();
if global.player==2// do this if players go
{
    draw_text(x,y-40,"strength="+string(strength));
    draw_text(x,y-80,"angle="+string(angle));
    draw_line(x,y,mouse_x,mouse_y);
}


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

alarm[0]=1;
gravity=0.08//apply gravity effect
points=ds_list_create();
Alarm Event for alarm 0:
execute code:

points=motion_predict(obj_bullet_1,50);
alarm[0]=room_speed/2;
Collision Event with object obj_player_2:
execute code:

show_message("player 1 wins");
game_restart();
Draw Event:
execute code:

draw_self();//draw the bullet
if !ds_list_empty(points)
{
    size=ds_list_size(points);//get size of ds_list
    for (var i = 0; i < size; i += 2)//2 steps at a time
    {
        draw_point(points[|i],points[|i+1]);//get value to draw points
    }
}


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

alarm[0]=1;
points=ds_list_create();
gravity=0.08//apply gravity effect
Alarm Event for alarm 0:
execute code:

points=motion_predict(obj_bullet_2,50);
alarm[0]=room_speed/2;
Step Event:
execute code:

gravity=0.08//apply gravity effect

Collision Event with object obj_player_1:
execute code:

show_message("player 1 wins");
game_restart();
Draw Event:
execute code:

draw_self();//draw the bullet
if !ds_list_empty(points)
{
    size=ds_list_size(points);//get size of ds_list
    for (var i = 0; i < size; i += 2)//2 steps at a time
    {
        draw_point(points[|i],points[|i+1]);//get value to draw points
    }
}

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

No comments:

Renowned Water Expert Reynold Aquino Offers Exclusive Water Softener Discounts

Los Angeles, California - November 21, 2024 - World-renowned water expert Reynold Aquino is excited to announce exclusive discounts on prem...

Contact Form

Name

Email *

Message *