Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: DodgeEngine/DodgeEngine.cpp
- Revision:
- 1:5b7c6b79a1c6
- Child:
- 2:d14bccec9948
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/DodgeEngine/DodgeEngine.cpp Wed May 27 08:19:38 2020 +0000
@@ -0,0 +1,79 @@
+#include "DodgeEngine.h"
+
+DodgeEngine::DodgeEngine(){
+//initialise score and boat position
+ player_score = 0;
+ y_boat = 24;
+ x_boat = 76;
+
+}
+DodgeEngine::~DodgeEngine(){}
+
+//function which holds the games logic
+void DodgeEngine::logic(N5110 &lcd, Gamepad &pad)
+{
+ draw_screen(lcd);
+ player_movements(lcd, pad);
+ boat_logic(lcd, pad);
+}
+//Function for the boats logic
+void DodgeEngine::boat_logic(N5110 &lcd, Gamepad &pad)
+{
+
+ o1.set_y_boat(y_boat);
+ o1.set_x_boat(x_boat);
+ o1.sprite(lcd);
+ boat_respawn(pad); //respawns the boat when it hits the other side of the screen
+}
+
+
+void DodgeEngine::draw_screen(N5110 &lcd)
+{
+ lcd.clear();
+ lcd.drawLine(0,7, 84,7,1); //Top edge of display
+ lcd.drawLine(0,47, 83,47,1); //Bottom edge of display
+ lcd.printString("Dodge the boat",1,0);
+}
+void DodgeEngine::player_movements(N5110 &lcd, Gamepad &pad)
+{
+ swimmer.sprite(lcd);
+ swimmer.get_x1();
+ swimmer.get_y1();
+ swimmer.movement(pad);
+
+}
+
+void DodgeEngine::boat_respawn(Gamepad &pad)
+{
+ x_boat += -2; //boat speed
+
+//If statement which respawns boat (moves boat back to start of the screen)
+ if(x_boat < 0) {
+ x_boat = 76;
+ player_score += 1;
+ //y_boat gets a random position to move the boat back to
+ y_boat = ((rand()%(48))+8);
+ }
+}
+
+bool DodgeEngine::state(N5110 &lcd)
+{
+ //checks if collision has occured and ends the game loop if it has
+ if(cd.Collision(lcd, x_boat, y_boat)== 1) {
+ return true;}
+ else{return false;
+ }
+ }
+//Game over screen
+void DodgeEngine::game_over(N5110 &lcd, Gamepad &pad)
+{
+
+ lcd.clear();
+ lcd.printString("SMASH",30,1);
+ lcd.printString("Score: ", 0,3);
+
+ char score_buffer[14];
+ sprintf(score_buffer,"%2d",player_score);
+ lcd.printString(score_buffer,42,3);
+ lcd.refresh();
+}
\ No newline at end of file