From b3108b71fd7b01d381461c0f9e58c82721d6b51f Mon Sep 17 00:00:00 2001 From: SashaPGT Date: Mon, 15 Nov 2021 22:00:51 +0700 Subject: [PATCH] Leaderboards --- main.cpp | 340 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 330 insertions(+), 10 deletions(-) diff --git a/main.cpp b/main.cpp index 9530195..02a5b08 100644 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,9 @@ #include #include #include +#include +#include +#include using namespace std; #define termMaxX 145 @@ -15,7 +18,7 @@ using namespace std; #define downArrow 66 #define rightArrow 67 #define leftArrow 68 - +#define clear() printf("\033[H\033[J") void drawVoid(){ @@ -117,6 +120,14 @@ int crashBomb(int posX, int posY, int bombX[100],int bombY[100], bool bombShown[ } void gameOver(int score){ + //clear(); + //initscr(); + cbreak(); + noecho(); + scrollok(stdscr, TRUE); + nodelay(stdscr, TRUE); + endwin(); + for(int i=0;i=10){ @@ -147,9 +160,292 @@ void gameOver(int score){ } SetCursorPosition(((paMaxX/2)-5)-(scoreShift/2),(paMaxY/2)+3); printf("Score: %d", score); - SetCursorPosition((paMaxX/2)-11,(paMaxY/2)+8); - printf("\033[1;5mPRESS ANY KEY TO EXIT"); + SetCursorPosition((paMaxX/2)-19,(paMaxY/2)+6); + printf("Enter you name: "); + SetCursorPosition((paMaxX/2)-1,(paMaxY/2)+6); + //cin>>score; + + FILE *cmd=popen("echo -n $HOME", "r"); + char result[100]={0x0}; + fgets(result, sizeof(result), cmd); + pclose(cmd); + string fullpath=result; + fullpath+="/.config/cursedSnakeGameScores"; + + FILE *fptr = fopen(fullpath.c_str(),"a"); + if (fptr == NULL) + { + printf("Could not open file"); + return; + } + string playerName; + + scanf("%s",playerName.c_str()); + fprintf(fptr,"%s %d\n", playerName.c_str(), score); + fclose(fptr); } + +int mainMenu() { + int currentButtonState=0,menuStarted=0; + printf("\e[?25l");//ปิดcursor + //initscr(); + cbreak(); + noecho(); + scrollok(stdscr, TRUE); + nodelay(stdscr, TRUE); + char input; + while(1){ + usleep(50000); + //ShowGameLogoOnFirstTime + + SetCursorPosition((paMaxX/2)-44,(paMaxY/2)-12); + printf("\033[45m \033[107m#####\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m######\033[45m \033[107m#####\033[45m \033[107m#######\033[45m \033[107m######\033[45m \033[107m#####\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m########"); + SetCursorPosition((paMaxX/2)-44,(paMaxY/2)-11); + printf("\033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m####\033[41m \033[107m##\033[45m \033[107m##\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m "); + SetCursorPosition((paMaxX/2)-44,(paMaxY/2)-10); + printf("\033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#######\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m "); + SetCursorPosition((paMaxX/2)-44,(paMaxY/2)-9); + printf("\033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m######\033[45m \033[107m#####\033[45m \033[107m#####\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m###\033[45m \033[107m######\033[45m "); + SetCursorPosition((paMaxX/2)-44,(paMaxY/2)-8); + printf("\033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#####\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#######\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m "); + SetCursorPosition((paMaxX/2)-44,(paMaxY/2)-7); + printf("\033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m##\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m "); + SetCursorPosition((paMaxX/2)-44,(paMaxY/2)-6); + printf("\033[45m \033[107m#####\033[45m \033[107m#####\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#####\033[45m \033[107m#######\033[45m \033[107m######\033[45m \033[107m#####\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m#\033[45m \033[107m########"); + SetCursorPosition((paMaxX/2)-44,(paMaxY/2)-5); + printf("\033[45m GAME\033[0m"); + if(menuStarted<=3){ + for(int i=0;i2){ + currentButtonState=2; + } + } + if(input=='\n'){ + return currentButtonState; + } + //PlayButtonHighlighted + if(currentButtonState==0){ + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)-1); + printf("\033[107m "); + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+0); + printf("\033[107;31m Play \033[0m"); + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+1); + printf("\033[107m "); + } + //PlayButton + else{ + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)-1); + printf("\033[45m "); + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+0); + printf("\033[45m Play "); + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+1); + printf("\033[45m "); + } + //LeaderboardsHighlighted + if(currentButtonState==1){ + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+3); + printf("\033[107m "); + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+4); + printf("\033[107;31m Scores \033[0m"); + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+5); + printf("\033[107m "); + } + //LeaderboardsButton + else{ + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+3); + printf("\033[45m "); + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+4); + printf("\033[45m Scores "); + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+5); + printf("\033[45m "); + } + //ExitHighlighted + if(currentButtonState==2){ + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+6); + printf("\033[107m "); + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+7); + printf("\033[107;31m Exit \033[0m"); + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+8); + printf("\033[107m "); + } + //ExitButton + else{ + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+6); + printf("\033[45m "); + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+7); + printf("\033[45m Exit "); + SetCursorPosition((paMaxX/2)-7,(paMaxY/2)+8); + printf("\033[45m "); + } + } +} + +void showScores() { + endwin(); + int line=0,currentLine=0; + char c; + FILE *cmd=popen("echo -n $HOME", "r"); + char result[100]={0x0}; + fgets(result, sizeof(result), cmd); + pclose(cmd); + string fullpath=result; + fullpath+="/.config/cursedSnakeGameScores"; + + FILE *fptr = fopen(fullpath.c_str(),"r"); + if(fptr == NULL) { + perror("Error in opening file"); + return; + } + do { + c = fgetc(fptr); + if( feof(fptr) ) { + break ; + } + if(c=='\n'){ + + line++; + } + } while(1); + for(int i=0;i /dev/null"); break; } start_t=this_t; @@ -281,7 +586,7 @@ int game(){ start=this_t; } if(crashBomb(posX[0],posY[0],bombX,bombY,bombShown)){ - + system("((speaker-test -t sine -f 700)& pid=$!; sleep 1s; kill -9 $pid) > /dev/null"); break; } if(eatFruit(posX[0],posY[0],fruitX,fruitY)==1){ @@ -305,6 +610,7 @@ int game(){ posX[snakeLength-1]=0; posY[snakeLength-1]=0; } + system("((speaker-test -t sine -f 700)& pid=$!; sleep 0.1s; kill -9 $pid) > /dev/null"); } SetCursorPosition(fruitX,fruitY); printf("0"); @@ -326,7 +632,7 @@ int game(){ } } - //system("((speaker-test -t sine -f 700)& pid=$!; sleep 0.1s; kill -9 $pid) > /dev/null"); + // } return snakeLength-2; } @@ -343,7 +649,21 @@ int game(){ }*/ int main () { - gameOver(game()); - getchar(); - return(0); + int mainMenuSelected,score; + initscr(); + while(1){ + mainMenuSelected=mainMenu(); + if(mainMenuSelected==0){ + score=game(); + gameOver(score); + + getchar(); + } + if(mainMenuSelected==1){ + showScores(); + } + if(mainMenuSelected==2){ + return 0; + } + } }