Created
April 7, 2013 18:17
-
-
Save unsign3d/5331710 to your computer and use it in GitHub Desktop.
Just an arduino's demo of a space shooter in a 16x2 lcd display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <LiquidCrystal.h> | |
byte nave_t[8] = { | |
B01000, | |
B11100, | |
B11110, | |
B01111, | |
B11110, | |
B11100, | |
B01000, | |
}; | |
byte proiettile_t[8] = { | |
B00000, | |
B00000, | |
B00000, | |
B01110, | |
B00000, | |
B00000, | |
B00000, | |
}; | |
byte masso_t[8] = { | |
B00000, | |
B00000, | |
B01100, | |
B01100, | |
B01100, | |
B00000, | |
B00000, | |
}; | |
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); | |
const int A = 8; | |
const int B = 9; | |
const byte nave = byte(0); | |
const byte proiettile = byte(1); | |
const byte masso = byte(2); | |
const int DELAY_ON_BUTTON = 200; | |
const int SPAWN_TIME = 3000; | |
int ENEMY_SPEED = 1500; | |
const int SHOOTING_INTERVAL = 1000; | |
int A_status; | |
int B_status; | |
int ship_status = 0; | |
int colpi[][2] = {{0,0},{0,0},{0,0},{0,0},{0,0}}; | |
int massi[][2] = {{16,0},{16,0},{16,0},{16,0},{16,0}}; | |
unsigned long prev_millis = 0; | |
unsigned long prev_spawn = 0; | |
unsigned long prev_enemy = 0; | |
unsigned long prev_speed = 0; | |
void setup() { | |
boolean shooted = false; | |
pinMode(A, INPUT); | |
pinMode(B, INPUT); | |
lcd.createChar(nave, nave_t); | |
lcd.createChar(proiettile, proiettile_t); | |
lcd.createChar(masso, masso_t); | |
lcd.begin(16, 2); | |
lcd.write(nave); | |
} | |
void loop() { | |
unsigned long currentMillis = millis(); | |
A_status = digitalRead(A); | |
B_status = digitalRead(B); | |
if(shooted()){ | |
shoot(); | |
} | |
if(moved()){ | |
moveShip(); | |
} | |
move_bullets(currentMillis); | |
spawn_enemy(currentMillis); | |
move_enemy(currentMillis); | |
} | |
boolean shooted(){ | |
if(A_status == HIGH){ | |
delay(DELAY_ON_BUTTON); | |
return true; | |
} | |
return false; | |
} | |
boolean moved(){ | |
if(B_status == HIGH){ | |
delay(DELAY_ON_BUTTON); | |
return true; | |
} | |
return false; | |
} | |
void moveShip(){ | |
if(ship_status == 0){ | |
//svuoto il contenitore di prima | |
lcd.setCursor(0,0); | |
lcd.print(" "); | |
//scrivo la nave | |
lcd.setCursor(0,1); | |
lcd.write(nave); | |
//aggiorno la posizione | |
ship_status = 1; | |
} else { | |
lcd.setCursor(0,1); | |
lcd.print(" "); | |
lcd.setCursor(0,0); | |
lcd.write(nave); | |
ship_status = 0; | |
} | |
} | |
void move_bullets(unsigned long int ctime){ | |
if(ctime-prev_millis >= SHOOTING_INTERVAL){ | |
boolean flag = true; | |
for(int i = 0; i<5 && flag; i++){ | |
if(colpi[i][0] < 15 && colpi[i][0] > 0){ | |
lcd.setCursor(colpi[i][0],colpi[i][1]); | |
lcd.print(" "); | |
colpi[i][0]++; | |
check_collisions(); | |
lcd.setCursor(colpi[i][0],colpi[i][1]); | |
lcd.write(proiettile); | |
} else if(colpi[i][0] > 14){ | |
lcd.setCursor(colpi[i][0],colpi[i][1]); | |
lcd.print(" "); | |
colpi[i][0] = 0; | |
} | |
} | |
prev_millis = ctime; | |
} | |
} | |
void shoot(){ | |
boolean flag = true; | |
for(int i = 0; i<5 && flag; i++){ | |
//i colpi non possono mai essere in 0 | |
//quindi se un colpo è in zero può essere inizializzato | |
if(colpi[i][0] == 0){ | |
colpi[i][1] = ship_status; | |
colpi[i][0] = 1; | |
//prevengo lo spostamento di un proiettile | |
lcd.setCursor(colpi[i][0], colpi[i][1]); | |
lcd.write(proiettile); | |
flag=false; | |
} | |
} | |
check_collisions(); | |
} | |
//è un po'come shoot() ma abbiamo da fare | |
//anche altre cose | |
void spawn_enemy(unsigned long int ctime){ | |
boolean flag = true; | |
int row = random(0,2); | |
if(ctime-prev_spawn >= SPAWN_TIME){ | |
for(int i = 0; i<5 && flag; i++){ | |
//i massi non possono mai essere in 0 se no il gioco è perso | |
//quindi se un masso è in zero può essere inizializzato | |
if(massi[i][0] == 16){ | |
massi[i][1] = row; | |
massi[i][0] = 15; | |
//prevengo lo spostamento di un proiettile | |
lcd.setCursor(massi[i][0], massi[i][1]); | |
lcd.write(masso); | |
flag=false; | |
} | |
} | |
prev_spawn = ctime; | |
} | |
} | |
void move_enemy(unsigned long int ctime){ | |
if(ctime-prev_enemy >= ENEMY_SPEED){ | |
for(int i = 0; i<5; i++){ | |
if(massi[i][0] < 16 && massi[i][0] > 0){ | |
lcd.setCursor(massi[i][0],massi[i][1]); | |
lcd.print(" "); | |
lcd.setCursor(--massi[i][0],massi[i][1]); | |
lcd.write(masso); | |
} else if(massi[i][0] == 0){ | |
die(); | |
} | |
} | |
prev_enemy = ctime; | |
} | |
} | |
void check_collisions(){ | |
for(int i = 0; i<5 ; i++){ | |
for(int ii=0; ii<5; ii++){ | |
if( (colpi[i][0] == massi[ii][0]) && (colpi[i][1] == massi[ii][1])){ | |
lcd.setCursor(colpi[i][0],colpi[i][1]); | |
lcd.print(" "); | |
colpi[i][0] = 0;colpi[i][1] = 0; | |
massi[ii][0] = 16;massi[ii][1] = 0; | |
} | |
} | |
} | |
} | |
void ind_difficulty(unsigned long int ctime){ | |
if(ctime-prev_enemy >= 30000){ | |
ENEMY_SPEED -= 200; | |
} | |
} | |
void die(){ | |
lcd.setCursor(0,0); | |
lcd.print("You lost"); | |
while(1){delay(1000);} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment