#include <LiquidCrystal.h>
#include <Streaming.h>
LiquidCrystal lcd( 18, 11, 19, 20, 21, 22, 23 );
int status = 15;
int position = 0;
int mod = 0;
unsigned long prev = 0;
int score = 0;
int astx = 16;
int asty = 0;
void setup( )
{
pinMode( status, OUTPUT );
digitalWrite( status, HIGH );
Serial.begin( 9600 );
randomSeed( 4 ); //fair dice roll
asty = random( 2 );
}
void loop( ) {
int x;
x = digitalRead( 8 );
if( x == 0 ) mod = 1;
if ( millis( ) - prev > 200 ) {
randomSeed( millis( ) );
astx--;
if( astx<=0 ) {
if( asty==position ) {
gameover( );
} else {
score++;
astx = 16;
asty = random( 2 );
}
}
if( astx>3 && random( 2 ) == 1) asty = random( 2 );
if( mod == 1 ) {
mod = 0;
position = position==1?0:1;
}
lcd.clear( );
prev = millis( );
position += mod;
lcd.setCursor( astx, asty );
lcd.print( "*" );
lcd.setCursor( 0, position );
lcd.print( "> " );
}
delay( 100 );
}
void gameover( ) {
lcd.setCursor( 0, 0 );
lcd.print( "GAME OVER!" );
lcd.setCursor( 0, 1 );
lcd.print( "Score: " );
lcd.print( score );
score = 0;
astx = 16;
asty = random( 2 );
delay( 5000 );
}