Fly Catch Revisited
In the control flow chapter we got started building a small game. The logic for this was all contained within the main function. With functions and procedures we can start to think about breaking this down.
Looking at the game code we can identify steps to update game, handle input, and draw game. The challenge at the moment is that many of these require us to change values in a number of variables. We do not currently have the tools to do this within separate procedures (but that will come in a couple of chapters).
For now, we can move the steps for draw game into a procedure. This will help us get started organising this, and we can progress toward more organised code as we learn some new tools and techniques.
Draw Game
We have the code for draw game outlined in our logic, so this should be a simple copy and paste action. You then need to add in the parameters that will tell draw game the details that it needs. You should end up with something like this:
Procedure: Draw the gameParameters: - spider_x, spider_y - integers for the location of the spider - fly_appeared - boolean to say if we draw the fly or not - fly_x, fly_y - where to draw the flySteps: Clear the screen white Fill a black circle using spider_x, spider_y, and SPIDER_RADIUS
if fly appeared Fill a dark green circle using fly_x, fly_y, and FLY_RADIUS
Refresh the screen to show it to the user
Have a go at coding this. The code in main is still too long to make it really understandable, but we are making progress. With just a few more tools we will have this all under control. So let’s wrap this chapter up, so you can move on to the next step, where we can start to organise our data.
- #include "splashkit.h"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){// Draw the gameclear_screen(color_white());// Draw the spiderfill_circle(color_black(), spider_x, spider_y, SPIDER_RADIUS);if (fly_appeared){// Draw the flyfill_circle(color_dark_green(), fly_x, fly_y, FLY_RADIUS);}// Show it to the userrefresh_screen(60);}int main(){// Set the spider in the center of the screenint spider_x = SCREEN_WIDTH / 2;int spider_y = SCREEN_HEIGHT / 2;// Create the flyint fly_x = rnd(SCREEN_WIDTH), fly_y = rnd(SCREEN_HEIGHT);bool fly_appeared = false;long appear_at_time = 1000 + rnd(2000);long escape_at_time = 0;open_window("Fly Catch", SCREEN_WIDTH, SCREEN_HEIGHT);create_timer(GAME_TIMER);start_timer(GAME_TIMER);// The event loopwhile (!quit_requested()){// Handle Inputif (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;}// Update the Game// Check if the fly should appearif (!fly_appeared && timer_ticks(GAME_TIMER) > appear_at_time){// Make the fly appearfly_appeared = true;// Give it a new random positionfly_x = rnd(SCREEN_WIDTH);fly_y = rnd(SCREEN_HEIGHT);// Set its escape timeescape_at_time = timer_ticks(GAME_TIMER) + 2000 + rnd(5000);}else if (fly_appeared && timer_ticks(GAME_TIMER) > escape_at_time){fly_appeared = false;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)){fly_appeared = false;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 interactionsprocess_events();}return 0;}