I'm building a 2d game based on the alien invaders code snippet from aum1. I figured i would start at the beginning, and work my way through. Right now i just have the movement for the game worked out. The enemies constantly move down towards where the player is, and he has to shoot some of them with a missile in order to catch some of them with a bubble to spell words out of the letters on them.
The problem i'm having is that after running the game for a while, the missiles start moving super fast, and the player ship and alien ships start moving painfully slow, and the collision detection stops working some of the time.
I'm not sure where the error is happening, so i'll include all my functions.
Code:
function move_ship()
{
while(1)
{
if (key_pressed(75) == on && player_ship_pan.pos_x > 0 && key_pressed(42) == on )
{
wait(2);
player_ship_pan.pos_x -=3;
}
else {break;}
}
while(1)
{
if (key_pressed(77) == on && player_ship_pan.pos_x < 924 && key_pressed(42) == on )
{
wait(2);
player_ship_pan.pos_x +=3;
}
else {break;}
}
while(1)
{
if (key_pressed(75) == on && player_ship_pan.pos_x > 0 && key_pressed(42) == off )
{
wait(4);
player_ship_pan.pos_x -=3;
}
else {break;}
}
while(1)
{
if (key_pressed(77) == on && player_ship_pan.pos_x < 924 && key_pressed(42) == off )
{
wait(4);
player_ship_pan.pos_x +=3;
}
else {break;}
}
}
////////////////////////////////////////////////////////////////////////////////////////////
// letters movement
function set_pos_y_letters()
{
xpos= 0;
ypos = 1;
while(xpos<41)
{
str_cpy(pos_y_assign_str, pos_y_array.string[xpos]);
str_cat(pos_y_assign_str, " -= 100 * ypos;");
execute(pos_y_assign_str);
xpos +=1;
ypos+=1;
}
xpos = 0;
while(xpos<41)
{
backward:
ypos = 125 * int(random(7));
position_x_tracking_array[xpos] = ypos;
str_for_num(pos_x_track_str, ypos);
str_cpy(pos_x_assign_str, pos_x_array.string[xpos]);
str_cat(pos_x_assign_str, " = ypos");
execute(pos_x_assign_str);
xpos +=1;
}
}
function move_mobs()
{
while(1)
{
xmobs= 0;
while(xmobs<41)
{
str_cpy(pos_y_assign_str, pos_y_array.string[xmobs]);
str_cat(pos_y_assign_str, " += 1;");
execute(pos_y_assign_str);
str_cpy(move_y_str, "ymobs =");
str_cat(move_y_str, pos_y_array.string[xmobs]);
execute(move_y_str);
if (ymobs > 568)
{
str_cpy(pos_y_assign_str, pos_y_array.string[xmobs]);
str_cat(pos_y_assign_str, " -= 4100;");
execute(pos_y_assign_str);
}
xmobs +=1;
}
wait(10/level);
}
}
function shoot_rocket ()
{
while(1)
{
IF (key_space == on && player_ship_pan.VISIBLE == ON)
{
missile_pan.POS_X = player_ship_pan.POS_X;
missile_pan.POS_y = player_ship_pan.POS_y;
missile_pan.VISIBLE = ON; // show the rocket
//snd_play (missile_fire, 100, 0); // this is not working
WHILE (missile_pan.POS_Y > 10)
{
missile_pan.POS_Y -= 2; // move the rocket upwards
WAIT(1);
}
missile_pan.VISIBLE = OFF; // hide the rocket
}
WAIT (1);
}
}
function shoot_bubble ()
{
WHILE (1)
{
IF (player_ship_pan.VISIBLE == ON)
{
bubble_pan.POS_X = player_ship_pan.POS_X;
bubble_pan.POS_y = player_ship_pan.POS_y - 100;
bubble_pan.VISIBLE = ON; // show the rocket
//PLAY_SOUND rocket_snd,100;
WHILE (bubble_pan.POS_Y > 10)
{
bubble_pan.POS_Y -= 1; // move the bubble upwards
WAIT(1);
}
bubble_pan.VISIBLE = OFF; // hide the rocket
}
WAIT (1);
}
}
function set_collisions ()
{
collision_loop_count = 0;
while(1)
{
if (collision_loop_count >= 42)
{
collision_loop_count = 0;
}
str_cpy(collision_string, "enemy_pan_x_num = ");
str_cat(collision_string, pos_x_array.string[collision_loop_count]);
str_cat(collision_string, ";");
execute(collision_string);
str_cpy(collision_string2, "enemy_pan_y_num = ");
str_cat(collision_string2, pos_y_array.string[collision_loop_count]);
str_cat(collision_string2, ";");
execute(collision_string2);
if (enemy_pan_x_num <= missile_pan.pos_x
&& missile_pan.visible == on
&& enemy_pan_x_num >= missile_pan.POS_X - 125
&& enemy_pan_y_num >= missile_pan.POS_Y - 100) // enemy is hit
{
str_cpy(collision_string3, enemy_array.string[collision_loop_count]);
str_cat(collision_string3, ".visible = off;");
execute(collision_string3);
missile_pan.visible = off;
missile_pan.pos_x = 1200;
}
collision_loop_count += 1;
WAIT(1);
}
}