const string GAME_TIMER = "GameTimer";
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SPIDER_RADIUS = 25;
const int SPIDER_SPEED = 3;
const int FLY_RADIUS = 10;
* Draw the spider and the fly to the screen.
* @param spider_x is the x location of the spider
* @param spider_y is the y location of the spider
* @param fly_appeared tells us if we need to draw the fly
* @param fly_x the x location of the fly
* @param fly_y the y location of the fly
void draw_game(int spider_x, int spider_y, bool fly_appeared, int fly_x, int fly_y)
clear_screen(color_white());
fill_circle(color_black(), spider_x, spider_y, SPIDER_RADIUS);
fill_circle(color_dark_green(), fly_x, fly_y, FLY_RADIUS);
// Set the spider in the center of the screen
int spider_x = SCREEN_WIDTH / 2;
int spider_y = SCREEN_HEIGHT / 2;
int fly_x = rnd(SCREEN_WIDTH), fly_y = rnd(SCREEN_HEIGHT);
bool fly_appeared = false;
long appear_at_time = 1000 + rnd(2000);
open_window("Fly Catch", SCREEN_WIDTH, SCREEN_HEIGHT);
create_timer(GAME_TIMER);
while (!quit_requested())
if (key_down(RIGHT_KEY) && spider_x + SPIDER_RADIUS < SCREEN_WIDTH)
spider_x += SPIDER_SPEED;
if (key_down(LEFT_KEY) && spider_x - SPIDER_RADIUS > 0)
spider_x -= SPIDER_SPEED;
if (key_down(DOWN_KEY) && spider_y + SPIDER_RADIUS < SCREEN_HEIGHT)
spider_y += SPIDER_SPEED;
if (key_down(UP_KEY) && spider_y - SPIDER_RADIUS > 0)
spider_y -= SPIDER_SPEED;
// Check if the fly should appear
if (!fly_appeared && timer_ticks(GAME_TIMER) > appear_at_time)
// Give it a new random position
fly_x = rnd(SCREEN_WIDTH);
fly_y = rnd(SCREEN_HEIGHT);
escape_at_time = timer_ticks(GAME_TIMER) + 2000 + rnd(5000);
else if (fly_appeared && timer_ticks(GAME_TIMER) > escape_at_time)
appear_at_time = timer_ticks(GAME_TIMER) + 1000 + rnd(2000);
if (circles_intersect(spider_x, spider_y, SPIDER_RADIUS, fly_x, fly_y, FLY_RADIUS))
appear_at_time = timer_ticks(GAME_TIMER) + 1000 + rnd(2000);
draw_game(spider_x, spider_y, fly_appeared, fly_x, fly_y);
// Get any new user interactions