ELEC2645 (2015/16) / Mbed 2 deprecated HarryPetrovicPong

Dependencies:   N5110 mbed

Fork of 2645_Physics_Engine_Example by Craig Evans

Revision:
1:6632d8423c65
Parent:
0:6a561e8d6713
--- a/main.cpp	Fri Mar 11 09:52:11 2016 +0000
+++ b/main.cpp	Thu May 05 14:18:21 2016 +0000
@@ -1,54 +1,109 @@
 #include "mbed.h"
 #include "N5110.h"
+#include "functions.h"
 
-#define BALLRADIUS 2
+void UserPaddle() //Paddle used by user
+
+{
+    lcd.setPixel(0,yp);
+    lcd.setPixel(0,yp+1);
+    lcd.setPixel(0,yp+2);
+    lcd.setPixel(0,yp+3);
+    lcd.setPixel(0,yp+4);
+    lcd.setPixel(0,yp+5);
+    lcd.setPixel(0,yp+6);
+    lcd.setPixel(0,yp+7);
+    lcd.setPixel(0,yp+8);
+    lcd.setPixel(0,yp+9);
 
-//          VCC,    SCE,  RST,    D/C,   MOSI,  SCLK,   LED
-N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
-// Can also power (VCC) directly from VOUT (3.3 V) -
-// Can give better performance due to current limitation from GPIO pin
-Ticker game_timer;
+}
+
+void ComputerPaddle() //Paddle controlled by the computer
 
-// struct used to store 2D vectors
-typedef struct vector_t vector_t;
-struct vector_t {
-    float x;
-    float y;
-};
+{
+    lcd.setPixel(83,yc);
+    lcd.setPixel(83,yc+1);
+    lcd.setPixel(83,yc+2);
+    lcd.setPixel(83,yc+3);
+    lcd.setPixel(83,yc+4);
+    lcd.setPixel(83,yc+5);
+    lcd.setPixel(83,yc+6);
+    lcd.setPixel(83,yc+7);
+    lcd.setPixel(83,yc+8);
+    lcd.setPixel(83,yc+9);
 
-// function prototypes
-void init_display();
-void init_ball();
-void game_timer_isr();
-void redraw_screen();
-void update_physics_engine();
-void check_collisions();
+}
 
-vector_t pos;  // ball position
-vector_t vel;  // ball velocity
-vector_t acc;  // ball acceleration
+void net()
+{
+    lcd.setPixel (42,0);
+    lcd.setPixel (42,2);
+    lcd.setPixel (42,4);
+    lcd.setPixel (42,6);
+    lcd.setPixel (42,8);
+    lcd.setPixel (42,10);
+    lcd.setPixel (42,12);
+    lcd.setPixel (42,14);
+    lcd.setPixel (42,16);
+    lcd.setPixel (42,18);
+    lcd.setPixel (42,20);
+    lcd.setPixel (42,22);
+    lcd.setPixel (42,24);
+    lcd.setPixel (42,26);
+    lcd.setPixel (42,28);
+    lcd.setPixel (42,30);
+    lcd.setPixel (42,32);
+    lcd.setPixel (42,34);
+    lcd.setPixel (42,36);
+    lcd.setPixel (42,38);
+    lcd.setPixel (42,40);
+    lcd.setPixel (42,42);
+    lcd.setPixel (42,44);
+    lcd.setPixel (42,46);
+    lcd.setPixel (42,48);
+}
 
-float refresh_rate = 10.0;  // how often to update display (Hz)
-float g_dt = 1.0F/refresh_rate;  // global to store time step (F makes it a float, gets rid of compiler warning)
-volatile int g_timer_flag = 0;
+
 
 int main()
 {
     wait(2.0);  // short delay for power to settle
     init_display(); // first need to initialise display
+    init_K64F();
+    menu();
+    wait(3);
+
     init_ball();
     // setup ticker
-    game_timer.attach(&game_timer_isr,g_dt);
+    game_timer.attach(&game_timer_isr,0.1);
 
     redraw_screen();  // draw initial screen
 
     while(1) {
+        sprintf(score1, "%d", playerScore);
+        lcd.printString(score1,30,0);
+        sprintf(score2, "%d", computerScore);
+        lcd.printString(score2,50,0);
+
 
         if ( g_timer_flag ) {  // ticker interrupt
             g_timer_flag = 0;  // clear flag
+            UserPaddle();
+            ComputerPaddle();
+            moveUserPaddle();
+            moveComputerPaddle();
+            paddleLimits();
+            ComputerpaddleLimits();
+            net();
+            Scoring();
+            victory();
+            loss();
+            resetBall();
             update_physics_engine();
             check_collisions();
             redraw_screen();
+            lcd.refresh();
+            refreshCells();
         }
 
         sleep();  // sleep until next interrupt
@@ -56,80 +111,10 @@
     }
 }
 
-void redraw_screen()
-{
-    lcd.clear();
-    lcd.drawCircle(pos.x,pos.y,BALLRADIUS,1);  // x,y,radius,black fill
-    lcd.refresh();  // update display
-}
 
-void check_collisions()
-{
-    // see if ball has hit the floor (subtract the radius since the position is the centre of the ball)
-    if ( pos.y >= 47 - BALLRADIUS ) {
-        pos.y = 47 - BALLRADIUS;  // need to force this or else ball can end up going 'underground'
-        vel.y = -0.89 * vel.y;  // y velocity is reflected and dampened 
-        // y accleration is still gravity
-    }
-
-    // has ball gone off the right-hand side?
-    if ( pos.x >= 83 - BALLRADIUS ) {
-        pos.x = 83 - BALLRADIUS;  // need to force this or else ball can end up going off screen
-        vel.x = -0.5 * vel.x;  // reflect and damp velocity
-        acc.x = -acc.x;  // reflect accleration
-    }
-    
-    // what about the left?
-    if ( pos.x <= BALLRADIUS ) {
-        pos.x = BALLRADIUS;  // need to force this or else ball can end up going off screen
-        vel.x = -0.5 * vel.x;  // reflect and damp velocity
-        acc.x = -acc.x;  // reflect accleration
-    }
-
-}
-
-void update_physics_engine()
-{
-    // from Newton's Laws
-
-    acc.x = 0.9F*acc.x;  // reduce a little due to air friction
-
-    // calc new velocity (assume 'unit' time)
-    vel.x = vel.x + acc.x; // * g_gt;
-    vel.y = vel.y + acc.y; // * g_gt;
-
-    // calc new position (assume 'unit' time)
-    pos.x = pos.x + vel.x;// * g_gt;
-    pos.y = pos.y + vel.y;// * g_dt;
-    
-    // should really multiply the above by the time-step,
-    // but since the pixel can only be a integer value,
-    // it makes the motion a little 'jumpy'.
-
-}
-
-void init_ball()
-{
-    // initial position (top-left)
-    pos.x = BALLRADIUS;
-    pos.y = BALLRADIUS;
-    // initial velocity - still
-    vel.x = 0.0;
-    vel.y = 0.0;
-    // initial acceleration - gravity and a bit of push to right
-    acc.x = 0.5;
-    acc.y = 2.0;  // +ve so ball accelerates to bottom of display (top of screen is y=0, bottom is y=47)
-    // should be 9.8, but can play with value to get a 'nice' ball movement
-}
 
 void game_timer_isr()
 {
     g_timer_flag = 1;
 }
 
-void init_display()
-{
-    lcd.init();
-    lcd.normalMode();      // normal colour mode
-    lcd.setBrightness(0.5); // put LED backlight on 50%
-}
\ No newline at end of file