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: main/main.cpp
- Revision:
- 15:2b98d03e7ab6
- Parent:
- 14:ce231137d2f2
- Child:
- 16:b0fb01fc21f8
diff -r ce231137d2f2 -r 2b98d03e7ab6 main/main.cpp
--- a/main/main.cpp Mon May 07 21:40:16 2018 +0000
+++ b/main/main.cpp Tue May 08 13:42:20 2018 +0000
@@ -11,247 +11,27 @@
#include "Gamepad.h"
#include "N5110.h"
#include "sprites.h"
-#include "main.h"
-
+#include "gameEngine.h"
+#include "player.h"
N5110 lcd(PTC9, PTC0, PTC7, PTD2, PTD1, PTC11);
Gamepad pad;
+gameEngine engine;
-void init();
-void startUp();
-void newGame();
-void gameOver();
int main()
{
- init();
- startUp();
+ engine.init();
+ engine.startUp();
while (1) {
- newGame();
- gameOver();
+ engine.newGame();
+ engine.gameOver();
}
}
-void init()
-{
-
-// initialise the lcd and gamepad
- lcd.init();
- lcd.setContrast(0.3);
- pad.init ();
- //set initial number of lives to 1
- lives = 1;
- //set initial location conditions for the player and car(s) and score
- x_player = 24;
- y_player = 29;
-
- x_car = 4;
- y_car = 0;
- x_car2 = 24;
- y_car2 = -40;
-
-//(re)set score to 0
- score = 0;
-
- srand(time(NULL));
-}
-
-
-void startUp()
-{
- //print start screen
- while ( pad.check_event(Gamepad::START_PRESSED) == false) {
- lcd.drawSprite (1,1,20,70,(int*) titlescreen);
- lcd.printString ("PRESS START", 3,5);
- lcd.printString ("SPEED", 3,3);
- pot_value = pad.read_pot();
- sprintf (speed, "%.1f", (pot_value*10)); //convert an integer to pointer
- lcd.printString(speed,55,3);
-
- lcd.refresh();
- wait(0.2);
- }
-}
-
-
-void newGame()
-{
- printf("newgamereached\n"); //checking if new game function reached
-
- lcd.clear();
-
- lcd.drawSprite(x_player,y_player,20,15,(int*) player); //print the player sprite
-
- //if X pressed, move player left, if B pressed, move player right
- if ( pad.check_event(Gamepad::X_PRESSED) == true) {
-
- x_player = x_player-20;
- }
- //car cannot go further left than the left lane etc
- if (x_player <4) {
- x_player = 4;
- }
- if ( pad.check_event(Gamepad::B_PRESSED) == true) {
-
- x_player = x_player+20;
- }
- if (x_player >44) {
- x_player = 44;
- }
-
-
- //print road lines
- lcd.drawLine(1,1,1,1500,2);
-
- lcd.drawLine(21,1,21,1500,2);
-
- lcd.drawLine(41,1,41,1500,2);
-
- lcd.drawLine(61,1,61,1500,2);
-
- //cars move down the road:
-
- lcd.drawSprite(x_car,y_car,20,15,(int*) car);
- y_car++;
- lcd.drawSprite(x_car2,y_car2,20,15,(int*) car2);
- y_car2++;
-
- // re-generate cars continously:
-
- int Index = rand() % 3; //picks a random number between 1 and 3
- printf ("Index = %d\n", Index);
- int arr[3] = {4, 24, 44}; //array corresponding to the x values of the 3 lanes of traffic
- int random_number = arr[Index]; //pick number at Index value from array, assign to random_number
- printf ("random_number = %d\n", random_number);
-
- int Index2 = rand() % 3;
- int arr2[3] = {4, 24, 44};
- int random_number2 = arr2[Index2];
-
-
- int YIndex = rand() % 5;
- int array[5] = {-20, -24, -28, -32, -36};
- int nextCar = array[YIndex];
-
- if (y_car == 50) {
- x_car = random_number;
- y_car = nextCar;
-
-
- }
-
- int YIndex2 = rand() % 5;
- int array2[5] = {-20, -24, -28, -32, -36};
- int nextCar2 = array2[YIndex2];
-
- if (y_car2 == 50) {
- x_car2 = random_number2;
- y_car2 = nextCar2;
-
-
- }
-
- //following if statement stops the car sprites overlapping
- if (y_car2>y_car-20 && y_car2<y_car+20 && x_car == x_car2 && y_car2>49) {
- x_car2 = random_number;
- }
-
- // make it so that after the player reaches a certain score, occasionally
- //(when both oncoming cars in the same lane) one car changes lane
- //this makes it harder for the player:
- if (score > 500 && y_car>5 && x_car == x_car2) {
- x_car = random_number2;
- }
-
-
-
-
-
- score++;//increase score
-
-// difficulty = read_pot();
- sprintf (str, "%d", (score/10)); //convert an integer to pointer
- lcd.printString(str,64,1);
-
-
- //if there is a collision, display "game over" screen
- if (x_car == x_player && y_car > 12 && y_car < 50) {
- printf("collision1\n");// to tell with which sprite the collision was
- y_car = y_car;
- y_car2 = y_car2;
- score=score;
- lives--;
- printf("lives=%d\n", lives);
-
- }
- if (x_car2 == x_player && y_car2 > 12 && y_car2 < 50) {
- printf("collision2\n");
- y_car2 = y_car2;
- y_car = y_car;
- score=score;
- lives--;
- printf("lives=%d\n", lives);
-
- }
-
- if ( pad.check_event(Gamepad::X_PRESSED) == true) {
-
- x_player = x_player-20;
- }
- //car cannot go further left than the left lane etc
- if (x_player <4) {
- x_player = 4;
- }
- if ( pad.check_event(Gamepad::B_PRESSED) == true) {
-
- x_player = x_player+20;
- }
- if (x_player >44) {
- x_player = 44;
- }
-
- pot_value = pad.read_pot();
- printf("pot_value = %f\n", pot_value);
- float wait_time = pot_value/10;
-
- lcd.refresh();
-
- wait(0.1-wait_time);
-
-
-
-
-
-
-
-
-
-
-}
-
-void gameOver()
-{
- printf("reached gameover\n");
- if (lives ==0) {
-
-
- while ( pad.check_event(Gamepad::START_PRESSED) == false) {
-
- printf("gameoverlives=%d\n", lives);
- lcd.clear();
-
- lcd.printString(str,64,1);
- lcd.printString ("GAME OVER", 3,1);
- lcd.printString ("PRESS START", 3,3);
- lcd.refresh();
- wait(0.2);
- }
- init ();
- }
-}