Ben Evans University Second Year Project. Game Called Defender.
Hello, soldier, you have been specially selected as the defender of planet earth.
Your mission, if you choose to accept it. Fly around the planet and pulverise invading alien ships for as long as you can. Stop the aliens abducting the innocent people on the ground. Be warned if an alien ship manages to abduct a person and take them to top of the screen, they will no longer move randomly and will begin to hunt you down. This sounds like a challenge you were trained for.
But don’t worry soldier you’re not going into battle empty-handed. Your ship is equipped with a state of the art laser beam that has unlimited ammo and four smart bombs that will destroy anything on the screen. The ship also has three lives so use them wisely.
As time goes on more alien ships will arrive on planet earth increasing the difficulty of your mission. And remember the landscape bellow loops around so if you continually fly in the same direction you go to your original position. Good luck soldier.
GameEngine/GameEngine.cpp
- Committer:
- evanso
- Date:
- 2020-05-25
- Revision:
- 81:78c461e6770b
- Parent:
- 80:870bc6b4bf08
- Child:
- 82:3211b31e9421
File content as of revision 81:78c461e6770b:
#include "GameEngine.h" /** Define acelleromter object */ FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL); /** Define SD card object */ SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); GameEngine::GameEngine() { //attach ticker to isr ticker.attach(callback(this, &GameEngine::lcd_frame_time_isr), 0.04 ); } GameEngine::~GameEngine() { } void GameEngine::init() { // Initalise objects pad.init(); lcd.init(); spaceship.init(); map.init(pad); menu.init(); accelerometer.init(); saved.init(); setting.init(); h_score.init(); //set first screen to main menu current_menu_part_= menu.get_current_menu_part(); //Launch screen lcd.printString("Evans Studios",3,2); lcd.printString("Presents:",15,3); lcd.refresh(); wait(3); play_music(); // Set seed value srand(pad.read_adc()*64000); //Had to inlcude save test here as getting the error sd couldnt be created #ifdef SD_TEST saved.run_save_test(sd,lcd); h_score.run_save_test(sd,lcd); #endif } void GameEngine::lcd_frame_time_isr(){ // set ISR flag lcd_frame_time_flag_ = 1; } // Menu Control----------------------------------------------------------------- void GameEngine::game_select_part(){ switch (current_menu_part_) { case main_menu: run_menu(); break; case play: run_play(); break; case settings: run_settings(); break; case saved_games: run_saved_games(); break; case high_score: run_highscore(); break; } } void GameEngine::run_menu(){ lcd.setContrast(pad.read_pot1()); lcd.clear(); // Scrolls, draws and selcts menu parts read_joystick_direction(); menu.menu_scroll(d_); menu.draw_part(lcd); menu.select_part(pad.A_pressed()); current_menu_part_ = menu.get_current_menu_part(); //sets sound and music setting sound_fx_ = setting.get_sound_method(); music_fx_ = setting.get_music_method(); stop_music(); //printf("sound%d\n",sound_fx_ ); //stops double button press if(pad.A_pressed()){ wait(0.3); } //inititialise the game if play if (current_menu_part_ == play){ play_init(); } lcd.refresh(); } // Menu Play ------------------------------------------------------------------- void GameEngine::run_play(){ //stops melody playing pad.play_melody(2, melody_off, melody_off_note_time, 1, false); int paused_flag = false; //Main gameplay loop to run plable part of game, pause and save screens while (1) { // Timer interupt to set frame rate of game if (lcd_frame_time_flag_) { // resets ISR flag lcd_frame_time_flag_ = 0; //Pause button pressed if (pad.start_pressed() && paused_counter_ > 10 ){ paused_flag = !paused_flag; paused_counter_ = 0 ; } //Stoped double press of pause button paused_counter_++; //Draws pause screen if paused button presed if (paused_flag){ run_paused_game(); //Otherwise draw gameplay screen }else{ gameplay_loop(); } // break out of run play loop when lives = 0 or exite flag set if(!spaceship_lives_||exit_flag_){ draw_game_over_screen(); reset_map(); pad.leds_off(); break; } } // MCU put to sleep between each frame to save power sleep(); } //stop double press of A wait(0.4); play_music(); } void GameEngine::play_music(){ if(music_fx_ == music_on){ pad.play_melody(88, star_wars, star_wars_note_time, 108, true); } } void GameEngine::stop_music(){ if(music_fx_ == music_off){ pad.play_melody(2, melody_off, melody_off_note_time, 50, false); } } void GameEngine::play_init(){ // Define variables spawn_time_multipler_ = 1; alien_number_ = 5; spawn_alien_counter_ = 0; spaceship_lives_ = 3; reset_map_counter_ = 0; smart_bomb_counter_ = 4; create_people(); points_ = 0; smart_bomb_timer_ = 0; bullet_timer_ = 0; lcd_frame_time_flag_ = 0; paused_counter_ = 25; //Flags for screen control exit_flag_ = false; run_save_a_game_flag_ = false; spaceship_destroyed_ = false; saved.init(); } void GameEngine::gameplay_loop() { // clear screen and set contrast lcd.setContrast(pad.read_pot1()); lcd.clear(); // creats aliens and people spawn_aliens(); spawn_people(); //If spaceship is destroyed stop joystick input, dont draw spaceship sprite spaceship_not_detroyed(); // Draws objects map.draw_map(lcd, d_); draw_aliens(); draw_bullets(); draw_explosions(); draw_people(); hud.draw_HUD(lcd, spaceship_lives_, points_, smart_bomb_counter_); spaceship_lives_leds(); reset_map_timer(); // refresh's screen lcd.refresh(); } void GameEngine::spaceship_not_detroyed(){ if (!spaceship_destroyed_){ //changes the control method depedning whats selected in the setting if(setting.get_control_method() == joy ){ read_joystick_direction(); }else { read_accelerometer_direction(accelerometer.get_roll_angle(), accelerometer.get_pitch_angle()); } //Stops movement of spaceship and map after destroyed spaceship.movement(d_); spaceship.draw(lcd); create_weapons_bullets(); create_weapons_smart_bomb(); } } void GameEngine::draw_game_over_screen(){ //Draws game over screen then back to menu lcd.clear(); lcd.printString("Game Over",15,0); //draws score/highscore part of screen char buffer[11]; sprintf(buffer,"Score = %d",points_); lcd.printString(buffer,12,2); h_score.set_error(false); //Prints new high score if a high score and no error if(calculate_new_score()&& !h_score.get_error()){ lcd.printString("NEW HIGH SCORE",0,4); if(music_fx_ == music_on){ pad.play_melody(61, take_on_me, take_on_me_note_time, 140, false); } }else if(music_fx_ == music_on){ pad.play_melody(23, god_farther, god_farther_note_time, 80, false); } lcd.refresh(); wait(6); current_menu_part_ = main_menu; } void GameEngine::run_paused_game(){ //Lunch save a game screen if (pad.B_pressed()||run_save_a_game_flag_){ run_save_a_game_flag_ = true; run_save_a_game(); //Draw pause screen }else{ draw_pause_screen(); paused_flag_ = true; } //Exit to main menu if (pad.A_pressed()||saved.get_error()){ current_menu_part_ = main_menu; play_music(); exit_flag_ = true; saved.set_error(false); } } void GameEngine::draw_pause_screen(){ // clear screen and set contrast lcd.setContrast(pad.read_pot1()); lcd.clear(); //Draw strings lcd.printString("Paused!",24,0); lcd.printString("Exit(A)",24,3); lcd.printString("Save&Exit(B)",6,4); // refresh's screen lcd.refresh(); } void GameEngine::run_save_a_game(){ //Draw and interact with save ya game screen read_joystick_direction(); saved.saved_games_scroll(d_); saved.save_game_screen(lcd); //Save game to save file that is displayed if (pad.A_pressed()){ //Define struct data SavedGamesData data; //Set values in struct data and saves them selected file data.score = points_; data.lives = spaceship_lives_; data.smart_bombs = smart_bomb_counter_; data.alien_number = alien_number_; saved.add_saved_data(sd, data, lcd); //exit to main menu current_menu_part_ = main_menu; exit_flag_ = true; } } // Menu Setting ---------------------------------------------------------------- void GameEngine::run_settings(){ lcd.clear(); lcd.setContrast(pad.read_pot1()); // Scrolls, draws and changes different parts read_joystick_direction(); setting.settings_scroll(d_); setting.display_settings_screen(lcd, pad.read_pot1()); setting.change_setting(pad.A_pressed()); //stops music emedently changed music_fx_ = setting.get_music_method(); stop_music(); //go back to main menu if b if (pad.B_pressed()){ current_menu_part_ = main_menu; } lcd.refresh(); } // Menu Saved Games ------------------------------------------------------------ void GameEngine::run_saved_games(){ // Draws and interact with save screen read_joystick_direction(); saved.saved_games_scroll(d_); saved.display_saved_games(lcd); //go back to main menu if (pad.B_pressed()){ current_menu_part_ = main_menu; } //Load save, if no file press go back to main menu if (pad.A_pressed()){ wait(0.035); saved_games_overide_init(); if(saved.get_error()) { saved.set_error(false); current_menu_part_ = main_menu; }else{ current_menu_part_ = play; } } } void GameEngine::saved_games_overide_init(){ play_init(); //Creats data object add sett it values to data saved in the file SavedGamesData displayed_game; displayed_game = saved.read_saved_data(sd, lcd); //Set the variables to value in the displayed saved struct points_ = displayed_game.score; spaceship_lives_ = displayed_game.lives; smart_bomb_counter_= displayed_game.smart_bombs; alien_number_ = displayed_game.alien_number; } // Menu High score ------------------------------------------------------------- void GameEngine::run_highscore(){ lcd.clear(); lcd.printString("High Scores",9,0); lcd.printString("Back(B)",21,5); //sets error to false as may of become true from privuous error h_score.set_error(false); //draws score/highscore part of screen char buffer[11]; sprintf(buffer,"Score = %d",h_score.read_high_score(sd)); lcd.printString(buffer,9,3); // If error reading high score just say no high score or sd card if (h_score.get_error()){ h_score.no_high_scores(lcd); wait(3); current_menu_part_ = main_menu; } //go back to main menu when b pressed if (pad.B_pressed()){ current_menu_part_ = main_menu; } lcd.refresh(); } bool GameEngine::calculate_new_score(){ bool new_high_score = false; //gets old high score and return true if current score is greater int saved_high_score = h_score.read_high_score(sd); if (points_ > saved_high_score){ new_high_score = true; h_score.save_new_high_score(sd, lcd, points_); } return new_high_score; }