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;
// Set the spider in the center of the screen
int spiderX = SCREEN_WIDTH / 2;
int spiderY = SCREEN_HEIGHT / 2;
int flyX = rnd(SCREEN_WIDTH), flyY = rnd(SCREEN_HEIGHT);
bool flyAppeared = false;
long appearAtTime = 1000 + rnd(2000);
open_window("Fly Catch", SCREEN_WIDTH, SCREEN_HEIGHT);
create_timer(GAME_TIMER);
while (!quit_requested())
if (key_down(RIGHT_KEY) && spiderX + SPIDER_RADIUS < SCREEN_WIDTH)
if (key_down(LEFT_KEY) && spiderX - SPIDER_RADIUS > 0)
// Check if the fly should appear
if (! flyAppeared && timer_ticks(GAME_TIMER) > appearAtTime)
// Give it a new random position
flyX = rnd(SCREEN_WIDTH);
flyY = rnd(SCREEN_HEIGHT);
escapeAtTime = timer_ticks(GAME_TIMER) + 2000 + rnd(5000);
else if (flyAppeared && timer_ticks(GAME_TIMER) > escapeAtTime)
appearAtTime = timer_ticks(GAME_TIMER) + 1000 + rnd(2000);
// Test if the spider and fly are touching
if (circles_intersect(spiderX, spiderY, SPIDER_RADIUS, flyX, flyY, FLY_RADIUS))
appearAtTime = timer_ticks(GAME_TIMER) + 1000 + rnd(2000);
clear_screen(color_white());
fill_circle(color_black(), spiderX, spiderY, SPIDER_RADIUS);
fill_circle(color_dark_green(), flyX, flyY, FLY_RADIUS);
// Get any new user interactions