Mbed project for a FRDM-K64F microcontroller. Game made out of five sub-games including flappy birds, snake, stack, trivia and space invaders.
Fork of The_Children_of_Cronos_el15mggr by
Diff: main.cpp
- Revision:
- 2:8b95be711648
- Parent:
- 1:101e060118fa
- Child:
- 3:151ac98b6252
--- a/main.cpp Sat Apr 29 20:21:07 2017 +0000 +++ b/main.cpp Wed May 03 22:20:43 2017 +0000 @@ -8,122 +8,186 @@ #include "cerinealib.h" #include "taurolib.h" #include "array.h" +#include "game.h" +#include "SDFileSystem.h" +Serial serial(USBTX, USBRX); +SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); +FILE *fp; +int first_pos=0,second_pos=0,third_pos=0; +//////////////////////////////////////////////////// +int main() +{ + init(); + // intro(); + introlights.detach(); + while(menuselection) { + pad.leds_on(); + float pott=pad.read_pot(); + lcd.setBrightness(pott); + menuscreen(); + menuselect(); + } + +} +///////////////////////////////////////////////////////////////// +void newgame() +{ + while(selection) { + pad.leds_on(); + float pott=pad.read_pot(); + totalscore=scoreg1+scoreg2+scoreg3+scoreg4+scoreg5; + game(); + select(); + if (r) { + selection=false; + menuselection=true; + } + } +} + +//////////////////////////////////////////////////////////////////// + + +void menuscreen() +{ + drawselectmenu(); + Direction d = pad.get_direction(); + if (d==S || a) { + wait(0.1); + if (d==S || a) { + statemenu = games[statemenu].nextstate[1];} + } + if (d==N || y) { + wait(0.1); + if(d==N || y) { + statemenu = games[statemenu].nextstate[0]; + }} + outputmenu=games[statemenu].position; +} + + +void game() +{ + drawselectscreen(); + Direction d = pad.get_direction(); + if (d==S || a) { + wait(0.1); + if (d==S || a) { + state = games[state].nextstate[1]; +} + } + if (d==N || y) { + wait(0.1); + if(d==N || y) { + state = games[state].nextstate[0]; + wait(0.1); + }} + output=games[state].position; + +} + +void menuselect() +{ + if(l && outputmenu==0) { + menuselection=false; + selection=true; + newgame(); + + } + if(l && outputmenu==1) { + menuselection=false; + savescore(); + + } + if(l && outputmenu==2) { + menuselection=false; + highscr=true; + highscores(); + + } + if(l && outputmenu==3) { + menuselection=false; + tut=true; + tutorial(); + + } + if(l && outputmenu==4) { + menuselection=false; + stor=true; + story(); + + } +} -DigitalOut x (PTC17); -DigitalOut y (PTC12); -DigitalOut a (PTB9); -DigitalOut b (PTD0); -DigitalOut l (PTB18); -DigitalOut r (PTB3); -DigitalOut start (PTC5); -DigitalOut mute(PTB18); -DigitalOut notmute(PTB3); -DigitalOut back(PTB19); - -Stymphalianlib stym; -N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); -Gamepad pad; -hydralib hyd; -aurigaslib aurig; -cerinealib cer; -taurolib tau; - -Ticker alienmove; - -void init(); -void intro(); -void game(); -void drawselectscreen(); -void selected(); -void gameone(); -bool game1=true; -void gametwo(); -bool game2=true; -void gamethree(); -bool game3=true; -void gamefour(); -bool game4=true; -void gamefive(); -bool game5=true; - -struct State{ - int position; - int nextstate[2]; - }; - -State games[5]={ - {0,{4,1}}, - {1,{0,2}}, - {2,{1,3}}, - {3,{2,4}}, - {4,{3,0}} - }; -int state=0; -int output=0; -int direction=1; - -int main() +void select() { - wait(2); - init(); - intro(); - gamefive(); + if(start && output==0) { + selection=false; + game1=true; + gameone(); + } + if(start && output==1) { + selection=false; + game2=true; + gametwo(); + } + if(start && output==2) { + selection=false; + game3=true; + gamethree(); + } + if(start && output==3) { + selection=false; + game4=true; + gamefour(); + } + if(start && output==4) { + selection=false; + game5=true; + gamefive(); + } } -void intro() -{ - Bitmap sprite(randulfe,15,72); - sprite.render(lcd,6,0); - sprite.print(); - lcd.printString("presents",5,4); - lcd.refresh(); - wait(2); - lcd.printString(".",60,4); - lcd.refresh(); - wait(2); - lcd.printString(".",65,4); - lcd.refresh(); - wait(2); - lcd.printString(".",70,4); - lcd.refresh(); - wait(2); - } - - -void game(){ - drawselectscreen(); - Direction d = pad.get_direction(); - if (d==E){state = games[state].nextstate[1];wait(0.1);} - if (d==W){state = games[state].nextstate[0];wait(0.1);} - if(pad.check_event(pad.B_PRESSED)){state = games[state].nextstate[1];wait(0.1);} - if(pad.check_event(pad.X_PRESSED)){state = games[state].nextstate[0];wait(0.1);} - output=games[state].position; - - -} - -void gamefive() -{ - while(game5){ - tau.game(lcd,pad,a,start);}} - +//////////////////////GAMES///////////////////////////////////////////////////// +void gamefive() +{ + while(game5) { + float pott=pad.read_pot(); + lcd.setBrightness(pott); + tau.game(lcd,pad,a,start,b,x); + if(back) { + if (tau.score()>scoreg5) { + scoreg5=tau.score(); + } + game5=false; + selection=true; + } + } +} void gamefour() { while(game4) { float pott=pad.read_pot(); lcd.setBrightness(pott); + if(back) { + game4=false; + selection=true; + } if(!cer.die()) { - cer.commands(pad); + cer.commands(b,a,x,y,pad); cer.updatescr(); cer.draw(lcd); cer.velocities(); } else if(cer.die()) { - cer.gameover(lcd,start,pad); + cer.gameover(lcd,start); + if (cer.score()>scoreg4) { + scoreg4=cer.score(); + } } } } @@ -133,35 +197,50 @@ while(game3) { float pott=pad.read_pot(); lcd.setBrightness(pott); + if(back) { + game3=false; + selection=true; + } if(aurig.touch()) { srand( time( NULL ) ); aurig.pipegenerator(pad,start); - aurig.updatescr(a,b,x,y); + aurig.updatescr(a,b,x,y,pad); aurig.draw(lcd); wait(0.05); } else { - aurig.gameover(lcd,pad,back); + aurig.gameover(lcd,pad,l,r); + if (aurig.score()>scoreg3) { + scoreg3=aurig.score(); + } } } } void gametwo() { - //alienmove.attach(&move, 1.0); + alienmove.attach(&aliens, 1.0); while(game2) { float pott=pad.read_pot(); lcd.setBrightness(pott); + if(back) { + game2=false; + selection=true; + alienmove.detach(); + } if (!stym.die()) { stym.leds(pad); stym.commands(pad,l,b,x,r); - stym.updatescr(); + stym.touch(pad); srand(time(NULL)); stym.auxiliar(); - stym.touch(); + stym.updatescr(); stym.draw(lcd); wait(0.08); } else if(stym.die()) { stym.gameover(lcd,start,pad); + if (stym.score()>scoreg2) { + scoreg2=stym.score(); + } } } } @@ -169,11 +248,20 @@ void gameone() { + while(game1) { + float pott=pad.read_pot(); + lcd.setBrightness(pott); + if(back) { + game1=false; + selection=true; + } if (hyd.border()) { hyd.leds(pad); hyd.commands(pad,a,b,x,y); + hyd.pausegame(l,r,lcd); if (hyd.eat()) { + //pad.tone(NOTE_GS6,0.5); srand( time( NULL ) ); hyd.auxiliar(); } @@ -183,33 +271,316 @@ hyd.velocities(); } else { hyd.gameover(lcd,start,pad); + if (hyd.score()>scoreg1) { + scoreg1=hyd.score(); + } } } } -void drawselectscreen(){ +/////////////////////////////////////////////////////////////////////////////// + + +void drawselectscreen() +{ + lcd.clear(); + + if(output==0) { + lcd.drawRect(0,8,84,8,FILL_BLACK); + } + if(output==1) { + lcd.drawRect(0,16,84,8,FILL_BLACK); + } + if(output==2) { + lcd.drawRect(0,24,84,8,FILL_BLACK); + } + if(output==3) { + lcd.drawRect(0,32,84,8,FILL_BLACK); + } + if(output==4) { + lcd.drawRect(0,40,84,8,FILL_BLACK); + } + lcd.printString("DEMETER",0,1); + lcd.printString("ZEUS",0,2); + lcd.printString("HERA",0,3); + lcd.printString("POSEIDON",0,4); + lcd.printString("HADES",0,5); + lcd.refresh(); +} +void drawselectmenu() +{ + lcd.clear(); + lcd.printString("MENU",30,0); + + if(outputmenu==0) { + lcd.drawRect(0,16,84,8,FILL_BLACK); + lcd.printString("GAME",0,2); + lcd.printString("SAVE SCORE",0,3); + lcd.printString("HIGH SCORES",0,4); + lcd.printString("TUTORIAL",0,5); + } + if(outputmenu==1) { + lcd.drawRect(0,24,84,8,FILL_BLACK); + lcd.printString("GAME",0,2); + lcd.printString("SAVE SCORE",0,3); + lcd.printString("HIGH SCORES",0,4); + lcd.printString("TUTORIAL",0,5); + } + if(outputmenu==2) { + lcd.drawRect(0,32,84,8,FILL_BLACK); + lcd.printString("GAME",0,2); + lcd.printString("SAVE SCORE",0,3); + lcd.printString("HIGH SCORES",0,4); + lcd.printString("TUTORIAL",0,5); + } + if(outputmenu==3) { + lcd.drawRect(0,40,84,8,FILL_BLACK); + lcd.printString("GAME",0,2); + lcd.printString("SAVE SCORE",0,3); + lcd.printString("HIGH SCORES",0,4); + lcd.printString("TUTORIAL",0,5); + } + if(outputmenu==4) { + lcd.drawRect(0,40,84,8,FILL_BLACK); + lcd.printString("SAVE SCORE",0,2); + lcd.printString("HIGH SCORES",0,3); + lcd.printString("TUTORIAL",0,4); + lcd.printString("STORY",0,5); + } + lcd.refresh(); +} + + +void intro() +{ + Bitmap sprite(randulfe,15,72); + sprite.render(lcd,6,0); + sprite.print(); + lcd.printString("presents",5,4); + lcd.refresh(); + wait(1); + lcd.printString(".",60,4); + lcd.refresh(); + wait(1); + lcd.printString(".",65,4); + lcd.refresh(); + wait(1); + lcd.printString(".",70,4); + lcd.refresh(); + wait(1); + lcd.clear(); + Bitmap sprite1(gamename,15,72); + Bitmap sprite2(gamename1,17,72); + Bitmap sprite3(gamename2,10,13); + sprite1.render(lcd,0,0); + sprite3.render(lcd,25,15); + sprite2.render(lcd,4,26); + sprite1.print(); + sprite3.print(); + sprite2.print(); + lcd.refresh(); + wait(8); lcd.clear(); - lcd.drawRect(7,3,20,15,FILL_TRANSPARENT); - lcd.drawRect(32,3,20,15,FILL_TRANSPARENT); - lcd.drawRect(57,3,20,15,FILL_TRANSPARENT); - lcd.drawRect(20,21,20,15,FILL_TRANSPARENT); - lcd.drawRect(45,21,20,15,FILL_TRANSPARENT); - if(output==0){lcd.drawRect(7,3,20,15,FILL_BLACK);} - if(output==1){lcd.drawRect(32,3,20,15,FILL_BLACK);} - if(output==2){lcd.drawRect(57,3,20,15,FILL_BLACK);} - if(output==3){lcd.drawRect(18,21,20,15,FILL_BLACK);} - if(output==4){lcd.drawRect(43,21,20,15,FILL_BLACK);} - lcd.printChar('1',14,1); - lcd.printChar('2',39,1); - lcd.printChar('3',64,1); - lcd.printChar('4',27,3); - lcd.printChar('5',52,3); - lcd.refresh(); + lcd.printString("In menu scr:",0,0); + lcd.printString("select:l",0,1); + lcd.printString("Back:r",0,2); + lcd.printString("In game scr:",0,3); + lcd.printString("select:start",0,4); + lcd.printString("Back:back",0,5); + lcd.refresh(); + wait(6); +} +void story() +{ + while(stor) { + lcd.clear(); + lcd.printString("NEXT",60,5); + if(outputstory==0) { + lcd.printString("Hey there! :)",0,0); + lcd.printString("Welcome to the",0,1); + lcd.printString("Olympus.Hestia",0,2); + lcd.printString("God of home,",0,3); + lcd.printString("needs your hlp",0,4); + } + if(outputstory==1) { + lcd.printString("for protecting",0,0); + lcd.printString("the sacred",0,1); + lcd.printString("fire.But first",0,2); + lcd.printString("you need to",0,3); + lcd.printString("prove to the",0,4); + } + if(outputstory==2) { + lcd.printString("ancient gods",0,0); + lcd.printString("(siblings of",0,1); + lcd.printString("Hestia) that",0,2); + lcd.printString("you're a brave",0,3); + lcd.printString("soldier.",0,4); + } + + if(outputstory==3) { + lcd.printString("You must pass",0,0); + lcd.printString("their challen-",0,1); + lcd.printString("-ges!Get ",0,2); + lcd.printString("ready for the",0,3); + lcd.printString("adventure!!!",0,4); + } + lcd.refresh(); + + Direction d = pad.get_direction(); + if (d==E || (pad.check_event(pad.B_PRESSED))) { + statestory = games[statestory].nextstate[1]; + wait(0.1); + } + if (d==W || (pad.check_event(pad.X_PRESSED))) { + statestory = games[statestory].nextstate[0]; + wait(0.1); + } + outputstory=games[statestory].position; + if(r) { + stor=false; + menuselection=true; + } } +} +void highscores() +{ + fp = fopen("/sd/topscore.txt", "r"); + fscanf(fp, "%d",&first_pos); + fscanf(fp, "%d",&second_pos); + fscanf(fp, "%d",&third_pos); + fclose(fp); + while(highscr){ + lcd.clear(); + lcd.printString("HIGH SCORES",9,0); + sprintf(buffer1,"1st=%2d ",first_pos); + lcd.printString(buffer1,0,2); + sprintf(buffer2,"2nd=%2d ",second_pos); + lcd.printString(buffer2,0,3); + sprintf(buffer3,"3rd=%2d ",third_pos); + lcd.printString(buffer3,0,4); + sprintf(buffer0,"Your score=%2d ",totalscore); + lcd.printString(buffer0,0,5); + lcd.refresh(); + if(r){ + highscr=false; + menuselection=true;}} +} + +void savescore() +{ + fp = fopen("/sd/topscore.txt", "r"); + fscanf(fp, "%d",&first_pos); + fscanf(fp, "%d",&second_pos); + fscanf(fp, "%d",&third_pos); + fclose(fp); + fp = fopen("/sd/topscore.txt", "w"); + if(totalscore>third_pos && totalscore<second_pos){ + third_pos=totalscore; + } + if(totalscore>second_pos && totalscore<first_pos){ + second_pos=totalscore; + } + if(totalscore>first_pos){ + first_pos=totalscore; + } + fprintf(fp, "%d",first_pos); // ensure data type matches + fprintf(fp, "%d",second_pos); // ensure data type matches + fprintf(fp, "%d",third_pos); // ensure data type matches + fclose(fp); // ensure you close the file after writing + lcd.clear(); + lcd.printString("SAVED",20,2); + lcd.refresh(); + wait(2); + menuselection=true; +} + + +void tutorial() +{ + while(tut) { + lcd.clear(); + lcd.printChar('1',0,0); + lcd.printChar('2',16,0); + lcd.printChar('3',32,0); + lcd.printChar('4',48,0); + lcd.printChar('5',64,0); + + if(outpututorial==0) { + lcd.drawRect(0,0,9,9,FILL_BLACK); + lcd.printString("Y,A,X,B:",0,2); + lcd.printString("UP,DWN,LFT,RGT",0,3); + lcd.printString("L,R PAUSE",0,5); + } + if(outpututorial==1) { + lcd.drawRect(16,0,9,9,FILL_BLACK); + lcd.printString("A,B,JOY:",0,2); + lcd.printString("MOVE RGT/LFT",0,3); + lcd.printString("L,R SHOOT",0,5); + } + if(outpututorial==2) { + lcd.drawRect(32,0,9,9,FILL_BLACK); + lcd.printString("A,B,X,Y:",0,2); + lcd.printString("JUMP",0,3); + } + if(outpututorial==3) { + lcd.drawRect(48,0,9,9,FILL_BLACK); + lcd.printString("A/B/X/Y:",0,2); + lcd.printString("LEAVE BLOCK",0,3); + } + if(outpututorial==4) { + lcd.drawRect(64,0,9,9,FILL_BLACK); + lcd.printString("JOY:",0,2); + lcd.printString("SELECT",0,3); + lcd.printString("A:CONTINUE",0,5); + } + lcd.refresh(); + + Direction d = pad.get_direction(); + if (d==E || (pad.check_event(pad.B_PRESSED))) { + statetutorial = games[statetutorial].nextstate[1]; + wait(0.1); + } + if (d==W || (pad.check_event(pad.X_PRESSED))) { + statetutorial = games[statetutorial].nextstate[0]; + wait(0.1); + } + outpututorial=games[statetutorial].position; + if(r) { + tut=false; + menuselection=true; + } + + + } +} + +void lights() +{ + + if(k==1 && lit>6) { + k=-1; + } + if(k==-1 && lit<0) { + k=1; + } + lit+=k; + + + for(int i=1; i<lit; i++) { + pad.led(i,1); + } +} + +void aliens() +{ + stym.move(); +} + void init() { + introlights.attach(&lights,1); tau.init(); cer.init(); aurig.init();