Anda di halaman 1dari 5

#include<LiquidCrystal.

h>
//opensuptheLCDlibrary

#defineBUTTON_PIN13
//setsthepinforthebutton
#defineMAX_ALIENS7
//setsthemaximumnumbersofmeteors/aliensinthearray

intbuttonState=0
//createsthebuttonStatevariable
intiAlien1[MAX_ALIENS]={22,29,35,40,44,47,52}
intiAlien2[MAX_ALIENS]={25,31,33,38,42,49,54}
//creatstwoarraysforthelocationsofeachrowofmeteors/aliens
inticounter=0
//createsavariablethatwilltelluswhethertoprintameteororthecharacter

intcharLocation=0
//createsvariabletodeterminecharacterlocation

LiquidCrystallcd(12,11,5,4,3,2)
//whatpinstheLCDuses

bytecharacter[8]={
B00000,
B00110,
B10100,
B01111,
B01111,
B10100,
B00110,
B00000
}

bytemeteor[8]={
B00000,
B00100,
B01110,
B11001,
B10111,
B01010,
B00100,
B00000
}


byteCrash[8]={
B00100,
B10101,
B01110,
B01110,
B10101,
B00100,
B00000,
B00000
}

//setsupthreecustomcharcters(meteor/charcter/crash)

voidsetup(){
//putyoursetupcodehere,torunonce:
lcd.begin(16,2)
//tellsthearduinothatweareusinga16by2LCD
lcd.createChar(0,character)
lcd.createChar(1,meteor)
lcd.createChar(2,Crash)
//createsthreecustomcharcters
pinMode(BUTTON_PIN,INPUT)
//saysbuttonisaninput
}

voidloop(){
//loopcodetocontinuouslyrun
buttonState=digitalRead(BUTTON_PIN)
//readsthebuttonstateeachtimetoseeifitisHIGH(on)orLOW(off)
delay(150)
//waitssoourprogramdoesn'truntooquickly
lcd.clear()
//clearsscreensopreviousloopisgone

icounter++
//addsonetoourcountervariable
/*

Thecountervariableishowwedeterminewhethertoprintthecharacterorthemeteors.
Sincewecannotprintthembothatthesametimeatdifferentpartsofthescreen,
weprintthecharacterwhenwefirstgothroughttheloopandthenthenexttimethroughthe
loop,

weprintthemeteors.Thisisdonebyaddingonetoicountereachtimeandusingthemodulo(%)
whichtakes
theremainderoficounterwhendividedbytwo.Ifthereisnoremainder(icounter%2==0),then
icounteriseven,
andwetellittoprintthecharacter.Theelsestatement(whentheicounterwouldbeodd),will
printthemeteors.
Thatiswhyweseethemeteorsandcharactersflashing,becausetheyareactuallybeing
printedatdifferenttimes.

*/
if((icounter%2)==0){
//ificounteriseven,printthecharacter
if(buttonState==HIGH){
charLocation=1
lcd.setCursor(0,charLocation)
lcd.write(byte(0))
//ifthebuttonispressed,printthecharacterat(0,1)
}
else{
charLocation=0
lcd.setCursor(0,charLocation)
lcd.write(byte(0))
//ifthebuttonisn'tpressed,printthecharacterat(0,0)
}
}

else{
//ificounterisodd,printmeteors
for(intk=0k<MAX_ALIENSk++)
//makeavariable(k)thataccessesacertainspotinthearray
{

iAlien1[k]=iAlien1[k]1
iAlien2[k]=iAlien2[k]1
//subtract1fromthevalueinthearray,thiswillmakeitmoveonespacetotheleft,anditwill
"fall"towardsourcharacter
if(iAlien1[k]<=15){
lcd.setCursor(iAlien1[k],0)
lcd.write(byte(1))
if(iAlien1[k]<0){
iAlien1[k]=55
//printsthemeteorsinthefirstrowiftheyshouldappearonscreen(valuelessthan15)
}

}
if(iAlien2[k]<=15){
lcd.setCursor(iAlien2[k],1)
lcd.write(byte(1))
if(iAlien2[k]<0){
iAlien2[k]=55
//printsthemeteorsinthesecondrow
}
}


/*
Bothsetsofcodebelowareforgameover.Thefirstisaconditionforwhenthefirstrowof
meteorscollidewiththeplayer.
Thesecondisaconditionforwhenthesecondrowofmeteorscollidewiththeplayer.After
findingoutiftheycollide,bothcodesdothesamething,
buttheyprinttheexplosion/crashwheretheplayerlastwas.
Thescreeniscleared,andtheexplosionisdrawnwheretheplayerwas.Thescreeniscleared
againand"GameOver"appears.
Thenyourscoreappears,whichismeasuredinsecondsthatyousurvivedfor.
Thatisclearedandthen"PressResettoContinueisprinted."Whenyoupressreset,yourestart
theprogram,
andallofthemeteorsresetalongwithyourscore.

*/
if(iAlien1[k]==0&&charLocation==0){
lcd.clear()
lcd.setCursor(0,0)
lcd.write(byte(2))
delay(25)
lcd.clear()
lcd.setCursor(3,0)
lcd.print("GameOver")
delay(2000)
lcd.clear()
lcd.print("Score:")
lcd.setCursor(10,0)
lcd.print(millis()/1000)
delay(8000)
lcd.clear()
lcd.print("PressResetto")
lcd.setCursor(3,1)

lcd.print("Continue")
delay(1000000)
lcd.clear()

}
if(iAlien2[k]==0&&charLocation==1){
lcd.clear()
lcd.setCursor(0,1)
lcd.write(byte(2))
delay(25)
lcd.clear()
lcd.setCursor(3,0)
lcd.print("GameOver")
delay(2000)
lcd.clear()
lcd.print("Score:")
lcd.setCursor(10,0)
lcd.print(millis()/1000)
delay(3000)
lcd.clear()
lcd.print("PressResetto")
lcd.setCursor(3,1)
lcd.print("Continue")
delay(1000000)
lcd.clear()

}
}
}
}

Anda mungkin juga menyukai