Monday, December 27, 2010

Zombie Snack - an Arduino LCD game

Zombie Snack... a simple game, inspired by some of the dice-roll combat games of years gone by.  Eat brains, impress the ladies.

Two buttons..
Button One (digital 2) is EAT, which sends you after your human prey.
Button Two (digital 3) is RUN, which chooses to walk away.

You have fifty hit points to kill and eat as many humans as you can, but humans vary in strength and they fight back!  If you reach zero hit points before you walk away, you die.. and score zero brains, no matter how many you had eaten until that point.  The goal is simple.. eat as many brains as you can while still having enough health to walk away!

Choose when to EAT and when to RUN carefully!

Press RESET to play again.

Potential future enhancements: Multiplayer, High Score Retention, and Specials (powerups for both Zombies and Humans!)

Full Code (in blue):


 //      ** Zombie Snack 1.0 **
//         by Paul Bishop
//           12/27/2010
// --------------------------------
// A Brain-Munching Game for
// Arduino.  Program assumes
// 16x2 LCD connected on digital
// lines 7-12, and button switches
// on digital 2 & 3, ideally with
// 10k pulldown resistors (below)
//
//             10k Ohm
// Pin >----+---/\/\/\-------GND
//          |
//       SWITCH
//          |
//          +-----/\/\/\-------5v
//               100 Ohm
//
// LCD pins may be changed, but switches
// drive interrupts and must be on pins 2 & 3.
//
// Button connected to Pin 2 :  "Eat"
// Button connected to Pin 3 :  "Run"

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // 4-bit mode for LCD
byte c1[8] = {
14,14,4,14,17,4,10,27}; // bitmap for human
int human;
int brains=0;
int hp=50;
int i,j,k=0;
int z;
volatile byte eat_press=0;
volatile byte run_press=0;
int action;



void setup() {
pinMode(3,INPUT); // Input is two buttons,
pinMode(2,INPUT); // "Eat" and "Run"
digitalWrite(3,HIGH); // Enable pullup resistors
digitalWrite(2,HIGH); //
attachInterrupt(0, eat, RISING); // handling buttons via ISR
attachInterrupt(1, run, RISING); // simplifies timing

randomSeed(analogRead(0)); // Seed random number gen

lcd.begin(16, 2); // set up the LCD's number of columns and rows
lcd.clear(); // and set our preferences
lcd.noAutoscroll();
lcd.noCursor();
lcd.createChar(0, c1); // 5x8 character bitmap from c1
lcd.setCursor(0, 0);
lcd.print("Zombie Snack 1.0"); // Splash screen.
lcd.setCursor(0, 1);
lcd.print(" Come-n-Get It!");
delay(3000);
}



void loop() {
lcd.setCursor(0,0);
lcd.print("Brains:");
lcd.print(brains);
lcd.print(" HP:");
lcd.print(hp);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Human! ");

for (int k=0; k<6; k++) {
lcd.setCursor(8,1);
for (i=0; i<k; i++) {
lcd.write(32);
};
lcd.write(0);
delay(100);
if (eat_press==1) {
attack();
};
if (run_press==1) {
game_over();
};
};

for (int k=5; k>-1; k--) {
lcd.setCursor(8,1);
for (i=0; i<k; i++) {
lcd.write(32);
};
lcd.write(0);
lcd.write(32);
delay(100);
if (eat_press==1) {
attack();
};
if (run_press==1) {
game_over();
};
};
}

void eat()
{
eat_press=1;
}

void run()
{
run_press=1;
}

void attack()
{
lcd.clear();
lcd.print("Zombie Attack!!");
randomSeed(analogRead(0));
human=random(10)+1;
lcd.setCursor(0,1);
lcd.print("Human- ");
lcd.print(human);
lcd.print(" hp!");

delay(3000);
while (human>0) {
lcd.clear();
lcd.print("Attack!!");
lcd.setCursor(0,1);
lcd.print("HP:");
lcd.print(hp);
lcd.print(" ");
for (k=0; k<(10-human);k++) {
lcd.print(" ");
};
for (k=0; k<human; k++) {
lcd.write(0);
};
action=random(2)+1;
if (action==1) { // Yeah! You Hit the Human!
lcd.setCursor(0,0);
lcd.print("CHOMP! Human -1");
human=human-1;
if (human==0) {
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Human Dead!");
lcd.setCursor(0,1);
lcd.print(">>> BRAINS! <<<");
brains=brains+1;
delay(2000);
};
};
if (action==2) { // Ouch! You got hit!
lcd.setCursor(0,0);
lcd.print("OUCH! Your HP -1");
hp=hp-1;
if (hp==0) {
game_over();
};
};
delay(1000);
};
eat_press=0;
lcd.setCursor(0,0);
lcd.print("Brains:");
lcd.print(brains);
lcd.print(" HP:");
lcd.print(hp);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Human! ");
}

void game_over()
{
run_press=0;
lcd.clear();
if (hp==0) {
brains=0;
lcd.setCursor(0,0);
lcd.print(">> You Died! <<");
lcd.setCursor(0,1);
lcd.print("Brains: 0");
}
else
{
lcd.setCursor(0,0);
lcd.print("Good Ghoul!");
lcd.setCursor(0,1);
lcd.print("Brains Eaten: ");
lcd.print(brains);
};

while (1) {
}; //loop forever

}

No comments:

Post a Comment