Skip to content

simple LCD game…

A little shooting game sketch (it’s exactly only that)…

using the sparkfun LCD, 3 buttons and the Arduino…

Because I was too lazy to get some working random code I used an analog input port on which nothing is connected. Because the obstacles popped out too fast I did the real dirty “if (shuffle == 6)” to pull the hits down…

I didn’t clean up the code…just for someone starting to prog and needs some input… 🙂


#include <i2cmaster.h>
#include <SoftwareSerial.h>

#define txPin 3
#define seedPin 4
#define btnUp 8
#define btnDown 9
#define btnShoot 10
#define rxPin -1
#define DEFAULT_SERIAL_SPEED 9600

#define CLEAR 0x01
#define CURSOR_MOVE_RIGHT 0x14
#define CURSOR_MOVE_LEFT 0x10
#define CURSOR_BOX_BLINK_ON 0x0D
#define CURSOR_BOX_BLINK_OFF 0x0C
#define CURSOR_POS_LINE1 128
#define CURSOR_POS_LINE2 192
#define LINE_LENGTH 16
#define LINE1_END 144
#define LINE2_END 208
#define MAX_MISSING_OBSTACLES = 10;

SoftwareSerial lcd = SoftwareSerial(rxPin, txPin);
int line;
int bullets[LINE_LENGTH] = {0};
int obstacles[LINE_LENGTH] = {0};
int ship = 0;
int points = 0;
boolean gameOverFlag = false;
int missingObstaclesCount = 0;

void setup(){
Serial.begin(DEFAULT_SERIAL_SPEED);
Serial.println("Setup...");

// LC-Display part
// set controller pins
pinMode(txPin, OUTPUT);
// sparkfun default port speed
lcd.begin(DEFAULT_SERIAL_SPEED);
// initialize the game variables
initGame();
// use internal pull-ups for the control lines
digitalWrite(btnUp,HIGH);
digitalWrite(btnDown,HIGH);
digitalWrite(btnShoot,HIGH);
}

void initGame() {
// clear the display first
sendCmd(CLEAR);
// set position to first character
sendCmd(CURSOR_POS_LINE1);
line = CURSOR_POS_LINE1;
ship = line;
points = 0;
for(int i = 0; i < LINE_LENGTH; i++) { bullets[i] = 0; obstacles[i] = 0; } missingObstaclesCount = 0; } void sendCmd(byte cmd) { lcd.write(0xFE); lcd.write(cmd); } void moveBulletsArray() { for (int i = LINE_LENGTH-1; i > 0; i--) {
bullets[i] = bullets[i-1];
}
bullets[0] = 0;
}
void moveObstaclesArray() {
for (int i = LINE_LENGTH-1; i > 0; i--) {
obstacles[i] = obstacles[i-1];
}
obstacles[0] = 0;
}

void moveBullets() {
for (int i = 0; i < LINE_LENGTH; i++) { if (bullets[i] > 0) {
bullets[i]++;
sendCmd(bullets[i]);
lcd.print("-");
}
}
}

void moveObstacles() {
for (int i = 0; i < LINE_LENGTH; i++) { if (obstacles[i] > 0) {
obstacles[i]--;
sendCmd(obstacles[i]);
lcd.print("o");
}
}
}

void collisionDetect() {
for (int i = 0; i < LINE_LENGTH ; i++) { for (int y = 0; y < LINE_LENGTH; y++) { if (obstacles[i] == bullets[y] && obstacles[i] > 0) {
sendCmd(obstacles[i]);
lcd.print("*");
points+=10;
obstacles[i] = 0;
bullets[y] = 0;
}
}
if (obstacles[i] == ship) {
sendCmd(CLEAR);
lcd.print("GAME OVER!");
gameOverFlag = true;
delay(1000);
return;
}
if(obstacles[i] == CURSOR_POS_LINE1 || obstacles[i] == CURSOR_POS_LINE2) missingObstaclesCount++;

if (missingObstaclesCount > 10) {
sendCmd(CLEAR);
lcd.print("GAME OVER!");
gameOverFlag = true;
delay(1000);
return;
}

}
}

void loop(){
int iDown = digitalRead(btnDown);
int iUp = digitalRead(btnUp);
int iShoot= digitalRead(btnShoot);

if(gameOverFlag) {
sendCmd(CLEAR);
lcd.print("GAME OVER!");
sendCmd(CURSOR_POS_LINE2);
lcd.print("Points: ");
lcd.print(points);
delay(1000);
sendCmd(CURSOR_POS_LINE2);
lcd.print("Press Fire...");
delay(1000);
sendCmd(CURSOR_POS_LINE2);
lcd.print("to restart...");
delay(1000);
if(iShoot == LOW) {
gameOverFlag = false;
initGame();
}
return;
}

srand (analogRead(seedPin));
int shuffle = rand() % 10 + 1;

// print the ship
if(iDown==LOW) line = CURSOR_POS_LINE2;
else if(iUp==LOW) line = CURSOR_POS_LINE1;

moveBulletsArray();
moveObstaclesArray();

sendCmd(line);
lcd.print(">");
ship = line;

if (iShoot == LOW) {
bullets[0] = line+1;
}
if (shuffle == 6) {
int sline = CURSOR_POS_LINE1;
srand (analogRead(4));
int shuffleLine = rand() % 10+1;
if (shuffleLine % 2) sline = CURSOR_POS_LINE2;
obstacles[0] = sline + 16;
}

moveBullets();
collisionDetect();
moveObstacles();
collisionDetect();

if (points > 200) {
delay(50);
} else if (points > 100) {
delay(100);
} else if (points > 30) {
delay(150);
} else {
delay(200);
}
sendCmd(CLEAR);
}

Post a Comment

You must be logged in to post a comment.