Exemple #1
0
int main()
{
	al_init(); // Start Allegro
	ALLEGRO_DISPLAY *screen = NULL; // Main display
	ALLEGRO_EVENT_QUEUE *event_queue = NULL; // Event queue to maintain events
	ALLEGRO_TIMER *timer = NULL; // Timer to control game fps
	ALLEGRO_FONT *fontA = NULL;

	// Initializing screen, event queue and timer
	screen = al_create_display(SCREEN_W, SCREEN_H);
	event_queue = al_create_event_queue();
	timer = al_create_timer(1.0 / FPS); // Timer set to tick at 60 frames per second

	al_init_image_addon(); // Initialize Allegro image addon
	al_install_keyboard(); // Initialize Allegro keyboard
	al_start_timer(timer); // Start timer
	al_init_font_addon();
	al_init_ttf_addon();

	// Register event sources to be handled in the event queue
	al_register_event_source(event_queue, al_get_display_event_source(screen));
	al_register_event_source(event_queue, al_get_timer_event_source(timer));
	al_register_event_source(event_queue, al_get_keyboard_event_source());
	fontA = al_load_font("arial.ttf", 18, 0);
	GameManager gameManager; // Object creation
	gameManager.level1(); // Function call

	//Main game loop
	while (!keyboard[KEY_ESCAPE]) // Run while escape key is not pressed
	{
		ALLEGRO_EVENT event; // ALLEGRO_EVENT to hold events
		al_wait_for_event(event_queue, &event); // Wait for events to occur

		// Display handler
		// Draw if timer counter has incremented and there are no events to handle
		// and event queue is empty
		if (event.type == ALLEGRO_EVENT_TIMER && al_is_event_queue_empty(event_queue))
		{
			gameManager.DrawLevel(screen);
			al_flip_display(); // Display backbuffer on to the main screen
			al_clear_to_color(al_map_rgb(255, 0, 255)); // Clear display to black
		}

		//Event handlers
		if (event.type == ALLEGRO_EVENT_TIMER)
		{
			gameManager.UpdateLogic(); // Function call
		}
		else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			break; // End game loop on closing display window
		}
		else if (event.type == ALLEGRO_EVENT_KEY_DOWN)
		{
			gameManager.KeyboardEventUpdate(event, true); // Update routine on key press
		}
		else if (event.type == ALLEGRO_EVENT_KEY_UP)
		{
			gameManager.KeyboardEventUpdate(event, false); // Update routine on key release
		}
		al_draw_textf(fontA, al_map_rgb(238, 105, 29), SCREEN_W / 2, SCREEN_H / 2, ALLEGRO_ALIGN_CENTRE, "GAME OVER  Final score %i", score);
	} // End of Game Loop

	al_destroy_display(screen); // Destroy display
	return 0;
}
Exemple #2
0
int main()
{

	int count = 0;
	int t = 70;
	int clk = 0;


	al_init(); // Start Allegro
	ALLEGRO_DISPLAY *screen = NULL; // Main display
	ALLEGRO_EVENT_QUEUE *event_queue = NULL; // Event queue to maintain events
	ALLEGRO_TIMER *timer = NULL; // Timer to control game fps

	// Initializing screen, event queue and timer
	screen = al_create_display(SCREEN_W, SCREEN_H);
	event_queue = al_create_event_queue();
	timer = al_create_timer(1.0 / FPS); // Timer set to tick at 60 frames per second

	al_init_font_addon();	//Font addon initialized
	al_init_ttf_addon();	//TrueType Font initialized
	ALLEGRO_FONT *font24 = al_load_font("COPRGTB.ttf", 24, 0); // Set font to Arial, of size 24
	ALLEGRO_FONT *font36 = al_load_font("COPRGTB.ttf", 36, 0); // Set font to Arial, of size 24

	al_init_image_addon(); // Initialize Allegro image addon
	al_install_keyboard(); // Initialize Allegro keyboard
	al_start_timer(timer); // Start timer

	// Register event sources to be handled in the event queue
	al_register_event_source(event_queue, al_get_display_event_source(screen));
	al_register_event_source(event_queue, al_get_timer_event_source(timer));
	al_register_event_source(event_queue, al_get_keyboard_event_source());

	GameManager gameManager; // Object creation
	gameManager.InitLevel(); // Function call

	//Main game loop
	while (!keyboard[KEY_ESCAPE]) // Run while escape key is not pressed
	{
		ALLEGRO_EVENT event; // ALLEGRO_EVENT to hold events
		al_wait_for_event(event_queue, &event); // Wait for events to occur

		// Display handler
		// Draw if timer counter has incremented and there are no events to handle
		// and event queue is empty
		if (event.type == ALLEGRO_EVENT_TIMER && al_is_event_queue_empty(event_queue))
		{
			gameManager.DrawLevel(screen);
			al_flip_display(); // Display backbuffer on to the main screen
			al_clear_to_color(al_map_rgb(0, 0, 0)); // Clear display to black
		}


		//Event handlers
		if (event.type == ALLEGRO_EVENT_TIMER)
		{
			count++;
			clk++;

			gameManager.UpdateLogic(); // Function call

		}
		else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			count++;
			clk++;

			break; // End game loop on closing display window
		}
		else if (event.type == ALLEGRO_EVENT_KEY_DOWN)
		{
			count++;
			clk++;

			gameManager.KeyboardEventUpdate(event, true); // Update routine on key press
		}
		else if (event.type == ALLEGRO_EVENT_KEY_UP)
		{
			count++;
			clk++;

			gameManager.KeyboardEventUpdate(event, false); // Update routine on key release
		}

		if (clk >= 58 && count < 4118 && score < 145) // Updates time every second
		{
			t--;
			string str = "TIME:  " + std::to_string(t);
			char * cstr = new char[str.length() + 1];
			strcpy(cstr, str.c_str());
			al_draw_text(font24, al_map_rgb(255, 255, 0), 660, 575, 0, cstr);
			clk = 0;

			std::string scoreDisp = "SCORE:  " + std::to_string(score);
			char * scoreStr = new char[scoreDisp.length() + 1];
			std::strcpy(scoreStr, scoreDisp.c_str());
			al_draw_text(font24, al_map_rgb(255, 255, 0), 450, 575, 0, scoreStr);
		}
		else // Displays Timer
		{
			std::string str = "TIME:  " + std::to_string(t);
			char * cstr = new char[str.length() + 1];
			std::strcpy(cstr, str.c_str());
			al_draw_text(font24, al_map_rgb(255, 255, 0), 660, 575, 0, cstr);

			std::string scoreDisp = "SCORE:  " + std::to_string(score);
			char * scoreStr = new char[scoreDisp.length() + 1];
			std::strcpy(scoreStr, scoreDisp.c_str());
			al_draw_text(font24, al_map_rgb(255, 255, 0), 450, 575, 0, scoreStr);

		}


		if (count >= 4118 || score == 145) //Runs the game until 70 seconds 
		{
			if (score == 145)
			{
				std::string str = "WELL DONE M8!!";
				char * cstr = new char[str.length() + 1];
				std::strcpy(cstr, str.c_str());
				std::string str1 = "PRESS ESCAPE TO EXIT";
				char * cstr1 = new char[str1.length() + 1];
				std::strcpy(cstr1, str1.c_str());
				al_draw_text(font24, al_map_rgb(255, 255, 0), 250, 350, 0, cstr1);
				al_draw_text(font24, al_map_rgb(255, 255, 0), 300, 250, 0, cstr);
				al_flip_display(); // Display backbuffer on to the main screen
				al_clear_to_color(al_map_rgb(0, 0, 0)); // Clear display to black

			}
			else
			{
				std::string str = "GAME OVER M8!!";
				char * cstr = new char[str.length() + 1];
				std::strcpy(cstr, str.c_str());

				std::string str1 = "PRESS ESCAPE TO EXIT";
				char * cstr1 = new char[str1.length() + 1];
				std::strcpy(cstr1, str1.c_str());
				al_draw_text(font24, al_map_rgb(255, 255, 0), 250, 350, 0, cstr1);
				al_draw_text(font24, al_map_rgb(255, 255, 0), 300, 250, 0, cstr);
				al_flip_display(); // Display backbuffer on to the main screen
				al_clear_to_color(al_map_rgb(0, 0, 0)); // Clear display to black
			}
		}

	} // End of Game Loop

	al_destroy_display(screen); // Destroy display
	return 0;
}