Commit 6f7308e1 authored by AlaskaLinuxUser's avatar AlaskaLinuxUser

Game State 3/4, game play and pause function.

parent 816c0fb0
......@@ -39,8 +39,9 @@ int gameAsteroidTwo = 25; // Second asteroid starting space.
int gameAsteroidThree = 17; // Third asteroid starting space.
int gameAsteroidFour = 23; // Fourth Asteroid starting space.
int gameDelay = 0; // A time delay factor.
String gameStringUpper = "001|>-************************";
String gameStringLower = "001|>-************************";
bool gameShoot = true; // true, you can shoot now.
bool gamePaused = false; // the game is not paused currently.
int gameTime = 0; // A variable to smooth out the pause button.
/*
* Game states:
......@@ -99,6 +100,27 @@ int read_LCD_buttons(){ // read the buttons
return btnNONE; // when all others fail, return this.
}
int set_gameSpeed (int gameScore){
int a = 1;
if (gameScore < 10){
a = 1;
} else if (gameScore < 25) {
a = 2;
}else if (gameScore < 50) {
a = 3;
} else if (gameScore < 75) {
a = 4;
}
return a;
}
int get_random (int addOn) {
srand(millis()+addOn); // Use time since turned on and add on number as seed for random number.
int r = rand() % 10; // Returns a random integer between 0 and 10.
r += 10; // Add 10 to keep it off of the display screen.
return r;
}
void setup(){
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0); // set the LCD cursor position
......@@ -121,8 +143,9 @@ void loop(){
gameAsteroidTwo = 25; // Second asteroid starting space.
gameAsteroidThree = 17; // Third asteroid starting space.
gameAsteroidFour = 23; // Fourth Asteroid starting space.
gameStringUpper = "001|>-************************";
gameStringLower = "001|>-************************";
gameShoot = true; // true, you can shoot now.
gamePaused = false; // the game is not paused currently.
gameTime = 0; // A variable to smooth out pause function.
lcd.setCursor(0,1); // move to the begining of the second line
if (gameLineNum != 0){
......@@ -275,56 +298,63 @@ void loop(){
if (gameState == 3) { // Play state.
// The player has entered play mode.
gameTime++;
if (gameTime > 1){
gameTime = 0;
gamePaused = false;
}
gameShoot = true; // you have not shot yet this round.
gameAsteroidOne--; // First asteroid starting space.
gameAsteroidTwo--; // Second asteroid starting space.
gameAsteroidThree--; // Third asteroid starting space.
gameAsteroidFour--; // Fourth Asteroid starting space.
if (gameAsteroidOne == 0) {
if (gameAsteroidOne == -1) {
if (gamePosition == 0) {
// Game over, you lose.
gameState = 6;
gameLineNum = 0;
} else {
gameScore++; // get a point!
gameSpeed++; // speed up the game.
gameAsteroidOne = 20;
gameSpeed = set_gameSpeed(gameScore);
gameAsteroidOne = get_random(4);
}
} // end gameAsteroidOne.
if (gameAsteroidTwo == 0) {
if (gameAsteroidTwo == -1) {
if (gamePosition == 0) {
// Game over, you lose.
gameState = 6;
gameLineNum = 0;
} else {
gameScore++; // get a point!
gameSpeed++; // speed up the game.
gameAsteroidTwo = 20;
gameSpeed = set_gameSpeed(gameScore);
gameAsteroidTwo = get_random(2);
}
} // end gameAsteroidTwo.
if (gameAsteroidThree == 0) {
if (gameAsteroidThree == -1) {
if (gamePosition == 1) {
// Game over, you lose.
gameState = 6;
gameLineNum = 0;
} else {
gameScore++; // get a point!
gameSpeed++; // speed up the game.
gameAsteroidThree = 20;
gameSpeed = set_gameSpeed(gameScore);
gameAsteroidThree = get_random(3);
}
} // end gameAsteroidThree.
if (gameAsteroidFour == 0) {
if (gameAsteroidFour == -1) {
if (gamePosition == 1) {
// Game over, you lose.
gameState = 6;
gameLineNum = 0;
} else {
gameScore++; // get a point!
gameSpeed++; // speed up the game.
gameAsteroidFour = 20;
gameSpeed = set_gameSpeed(gameScore);
gameAsteroidFour = get_random(1);
}
} // end gameAsteroidFour.
......@@ -346,6 +376,7 @@ void loop(){
shipLower = ">";
}
// Upper blocks.
lcd.setCursor(0,0);
lcd.print("00");
lcd.setCursor(2,0);
......@@ -355,26 +386,45 @@ void loop(){
lcd.setCursor(4,0);
lcd.print(shipUpper);
// The score
String currentScore = "000";
if (gameScore < 10){
currentScore = "00" + gameScore;
} else if (gameScore < 100) {
currentScore = "0" + gameScore;
} else {
currentScore = gameScore;
for (int a = 5; a < 17; a++){
lcd.setCursor(a,0);
if (a == gameAsteroidOne+5){
lcd.print("*");
} else if (a == gameAsteroidTwo+5){
lcd.print("*");
} else {
lcd.print(" ");
}
gameStringUpper = "-UPPER*****";
gameStringLower = "-LOWER*****";
}
lcd.setCursor(5,0);
lcd.print(gameStringUpper);
lcd.setCursor(5,1);
lcd.print(gameStringLower);
// Lower blocks.
lcd.setCursor(0,1);
lcd.print(gameScore);
if (gameScore < 10) {
lcd.setCursor(1,1);
lcd.print(" ");
} else if (gameScore < 100) {
lcd.setCursor(2,1);
lcd.print(" ");
}
lcd.setCursor(3,1);
lcd.print("|");
lcd.setCursor(4,1);
lcd.print(shipLower);
for (int a = 5; a < 17; a++){
lcd.setCursor(a,1);
if (a == gameAsteroidThree+5){
lcd.print("*");
} else if (a == gameAsteroidFour+5){
lcd.print("*");
} else {
lcd.print(" ");
}
}
gameDelay = 0;
gameDelay = 0;
while (gameDelay < 1000){
// Add a delay in the loop.
delay(1);
......@@ -385,11 +435,31 @@ void loop(){
case btnRIGHT:{
// Shoot command
if (gameBullets > 0 && gameShoot) {
if (gamePosition == 0){
lcd.setCursor(5,0);
lcd.print("------------");
gameBullets--;
gameShoot = false; // You already shot.
gameAsteroidOne = get_random(4);
gameAsteroidTwo = get_random(2);
} else {
lcd.setCursor(5,1);
lcd.print("------------");
gameBullets--;
gameShoot = false; // You already shot.
gameAsteroidThree = get_random(3);
gameAsteroidFour = get_random(1);
}
}
break;
}
case btnLEFT:{
if (!gamePaused) {
gameState = 4;
gameLineNum = 0;
gamePaused = true;
}
break;
}
case btnUP:{
......@@ -411,37 +481,19 @@ void loop(){
if (gameState == 4) { // Pause state.
// The player paused the game.
lcd.setCursor(9,1); // move cursor to second line "1" and 9 spaces over
lcd.print(millis()/1000); // display seconds elapsed since power-up
lcd.setCursor(0,1); // move to the begining of the second line
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key){ // depending on which button was pushed, we perform an action
lcd_key = read_LCD_buttons(); // read the buttons
case btnRIGHT:{ // push button "RIGHT" and show the word on the screen
lcd.print("RIGHT ");
break;
}
switch (lcd_key){
case btnLEFT:{
lcd.print("LEFT "); // push button "LEFT" and show the word on the screen
break;
}
case btnUP:{
lcd.print("UP "); // push button "UP" and show the word on the screen
break;
}
case btnDOWN:{
lcd.print("DOWN "); // push button "DOWN" and show the word on the screen
break;
gameState = 3;
gameLineNum = 0;
break;
}
case btnSELECT:{
lcd.print("SELECT"); // push button "SELECT" and show the word on the screen
break;
}
case btnNONE:{
lcd.print("NONE "); // No action will show "None" on the screen
break;
gameState = 0;
gameLineNum = 0;
break;
}
}
} // End gameState 4, pause state.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment