James Cummins / Mbed 2 deprecated el17jnc

Dependencies:   mbed

Revision:
20:4a39a1a2be51
Child:
21:9d1447765ee1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BrickBreaker_Engine/BrickBreakerEngine.cpp	Wed Apr 17 14:34:08 2019 +0000
@@ -0,0 +1,77 @@
+#include "BrickBreakerEngine.h"
+#include <cmath>
+
+//Constructor
+BrickBreakerEngine::BrickBreakerEngine(){
+}
+
+//Destructor
+BrickBreakerEngine::~BrickBreakerEngine(){
+}
+
+//Initialiser
+void BrickBreakerEngine::init(int radius){
+    _ball_radius = radius;
+    _ball.init(_ball_radius);
+    srand(time(NULL));
+    _square_coord.x = 2 + rand()%80;
+    _square_coord.y = 8 + rand()%36;
+    _pause.init();
+}
+
+/////////////////Methods for different game modes////////////////////////////
+void BrickBreakerEngine::brickbreaker_mode(FXOS8700CQ &accelerometer, Gamepad &gamepad, N5110 &lcd, AnalogIn &randnoise, int fps){
+    for(int i = 0; i <45*fps; i++){
+        if(i == 1){ _score = 0; }       //reset score when game restarts 
+        _ball.read_input(accelerometer);
+        _ball.update();
+        Vector2D position = _ball.get_position();
+        printf("ball_x = %f | ball_y = %f\n", position.x, position.y);  //note: running with tests causes the game to run slow and take ~2min30s
+        check_square_collision(randnoise);
+        lcd.clear();
+        brickbreaker_draw(lcd);
+        lcd.refresh();
+        wait_ms(1000/fps);
+        if(gamepad.check_event(gamepad.BACK_PRESSED)){
+            int choice = _pause.pause_menu(gamepad, lcd, fps, i);
+            i = choice;
+        }
+    }
+}
+
+//////////////Methods for updating and rendering//////////////////
+
+void BrickBreakerEngine::brickbreaker_draw(N5110 &lcd){
+    _ball.draw(lcd);
+    lcd.drawRect(_square_coord.x, _square_coord.y, 5, 5, FILL_BLACK);
+    print_score(lcd);
+}
+
+/////////////Brickbreaker functionality/////////////////////
+
+void BrickBreakerEngine::generate_rand_square(AnalogIn &randnoise){
+    int rand = randnoise.read_u16();
+    Vector2D square_coords = {rand % 80, 8 + rand % 36};
+    _square_coord = square_coords;
+}
+
+void BrickBreakerEngine::check_square_collision(AnalogIn &randnoise){
+    int radius = _ball.get_radius();
+    Vector2D position = _ball.get_position();
+    if (abs(position.x - (_square_coord.x + 2)) <= (radius + 2) and 
+        abs(position.y - (_square_coord.y + 2)) <= (radius + 2)) {
+            _score++;
+            generate_rand_square(randnoise);
+    }
+}
+
+
+void BrickBreakerEngine::print_score(N5110 &lcd){
+    char buffer[2];
+    int score = _score;
+    sprintf(buffer, "%d", score);
+    lcd.printString(buffer, 72, 0);
+}
+
+/////////////////Pause Menu functions//////////////////////////
+