Leaderboards

This commit is contained in:
Late Night Defender 2021-11-15 22:00:51 +07:00
parent aa36b36dfa
commit b3108b71fd

338
main.cpp
View file

@ -5,6 +5,9 @@
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <string>
#include <string.h>
#include <string>
using namespace std; using namespace std;
#define termMaxX 145 #define termMaxX 145
@ -15,7 +18,7 @@ using namespace std;
#define downArrow 66 #define downArrow 66
#define rightArrow 67 #define rightArrow 67
#define leftArrow 68 #define leftArrow 68
#define clear() printf("\033[H\033[J")
void drawVoid(){ void drawVoid(){
@ -117,6 +120,14 @@ int crashBomb(int posX, int posY, int bombX[100],int bombY[100], bool bombShown[
} }
void gameOver(int score){ void gameOver(int score){
//clear();
//initscr();
cbreak();
noecho();
scrollok(stdscr, TRUE);
nodelay(stdscr, TRUE);
endwin();
for(int i=0;i<paMaxX;i++){ for(int i=0;i<paMaxX;i++){
for(int j=0;j<paMaxY;j++){ for(int j=0;j<paMaxY;j++){
@ -139,6 +150,8 @@ void gameOver(int score){
printf("\033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m "); printf("\033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m ");
SetCursorPosition((paMaxX/2)-34,(paMaxY/2)-1); SetCursorPosition((paMaxX/2)-34,(paMaxY/2)-1);
printf("\033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m "); printf("\033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m \033[41m \033[45m ");
printf("\e[?25h");
int scoreShift=0,temp=score; int scoreShift=0,temp=score;
while(temp>=10){ while(temp>=10){
@ -147,9 +160,292 @@ void gameOver(int score){
} }
SetCursorPosition(((paMaxX/2)-5)-(scoreShift/2),(paMaxY/2)+3); SetCursorPosition(((paMaxX/2)-5)-(scoreShift/2),(paMaxY/2)+3);
printf("Score: %d", score); printf("Score: %d", score);
SetCursorPosition((paMaxX/2)-11,(paMaxY/2)+8); SetCursorPosition((paMaxX/2)-19,(paMaxY/2)+6);
printf("\033[1;5mPRESS ANY KEY TO EXIT"); 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;i<paMaxX;i++){
for(int j=0;j<paMaxY;j++){
SetCursorPosition(i,j);
printf("\033[45m \033[0m"); //\033[41m
}
}
menuStarted++;
}
input=getch();
if(input==upArrow){
currentButtonState--;
if(currentButtonState<0){
currentButtonState=0;
}
}
if(input==downArrow){
currentButtonState++;
if(currentButtonState>2){
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<paMaxX;i++){
for(int j=0;j<paMaxY;j++){
SetCursorPosition(i,j);
printf("\033[0m ");
}
}
drawFrame();
if(line==0){
SetCursorPosition((paMaxX/2)-8,paMaxY/2);
printf("No scores found.");
SetCursorPosition(64,paMaxY-2);
printf("Press Enter to return to main menu.");
getchar();
fclose(fptr);
return;
}
rewind(fptr);
//fclose(fptr);
string playerName[line];
string scores[line];
for(int i=0;i<line;i++){
playerName[i]="";
scores[i]="";
}
//*fptr = *fopen(fullpath.c_str(),"r");
if(fptr == NULL) {
perror("Error in opening file");
return;
}
bool scoreName=0;
while(currentLine!=line){
c=fgetc(fptr);
if(scoreName==0){
playerName[currentLine]+=c;
}
if(scoreName==1){
scores[currentLine]+=c;
}
if(c==' ') scoreName=1;
if(c=='\n'){
scoreName=0;
currentLine++;
}
}
fclose(fptr);
drawFrame();
SetCursorPosition(5,3);
printf("Name");
SetCursorPosition(50,3);
printf("Score");
currentLine=0;
bool showLinesIndicator=1;
if(line<=20) showLinesIndicator=0;
cbreak();
noecho();
scrollok(stdscr, TRUE);
nodelay(stdscr, TRUE);
//int j=0;
while(1){
usleep(10000);
c = getch();
if(showLinesIndicator==1){
if(c==upArrow){
for(int i=1;i<paMaxX-1;i++){
for(int j=4;j<paMaxY-1;j++){
SetCursorPosition(i,j);
printf("\033[0m ");
}
}
if(currentLine!=0) currentLine--;
}
if(c==downArrow){
for(int i=1;i<paMaxX-1;i++){
for(int j=4;j<paMaxY-1;j++){
SetCursorPosition(i,j);
printf("\033[0m ");
}
}
if(currentLine+20!=line) currentLine++;
}
for(int i=currentLine;i<20+currentLine;i++){
SetCursorPosition(5,4+i-currentLine);
printf("%s", playerName[i].c_str());
SetCursorPosition(50,4+i-currentLine);
printf("%s", scores[i].c_str());
}
SetCursorPosition(1,paMaxY-2);
printf("lines %d-%d",currentLine+1,currentLine+21);
}
if(showLinesIndicator==0){
for(int i=currentLine;i<line;i++){
SetCursorPosition(5,4+i);
printf("%s", playerName[i].c_str());
SetCursorPosition(50,4+i);
printf("%s", scores[i].c_str());
}
}
SetCursorPosition(64,paMaxY-2);
printf("Press Enter to return to main menu.");
if(c=='\n'){
break;
}
}
}
int game(){ int game(){
clock_t start_t,this_t,start; clock_t start_t,this_t,start;
@ -165,7 +461,7 @@ int game(){
}*/ }*/
char input,lastInput; char input,lastInput;
initscr(); //initscr();
cbreak(); cbreak();
noecho(); noecho();
scrollok(stdscr, TRUE); scrollok(stdscr, TRUE);
@ -207,6 +503,15 @@ int game(){
this_t=clock(); this_t=clock();
SetCursorPosition(posX[0],posY[0]); SetCursorPosition(posX[0],posY[0]);
if(ft<5){//สร้างงูรอบแรกสุด if(ft<5){//สร้างงูรอบแรกสุด
for(int i=0;i<paMaxX;i++){
for(int j=0;j<paMaxY;j++){
SetCursorPosition(i,j);
printf("\033[0m ");
}
}
SetCursorPosition(posX[0],posY[0]);
drawSnake(); drawSnake();
drawFrame(); drawFrame();
ft++; ft++;
@ -259,7 +564,7 @@ int game(){
} }
} }
if(crashWall(posX[0],posY[0])||crashSnake(posX,posY)){ if(crashWall(posX[0],posY[0])||crashSnake(posX,posY)){
system("((speaker-test -t sine -f 700)& pid=$!; sleep 1s; kill -9 $pid) > /dev/null");
break; break;
} }
start_t=this_t; start_t=this_t;
@ -281,7 +586,7 @@ int game(){
start=this_t; start=this_t;
} }
if(crashBomb(posX[0],posY[0],bombX,bombY,bombShown)){ 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; break;
} }
if(eatFruit(posX[0],posY[0],fruitX,fruitY)==1){ if(eatFruit(posX[0],posY[0],fruitX,fruitY)==1){
@ -305,6 +610,7 @@ int game(){
posX[snakeLength-1]=0; posX[snakeLength-1]=0;
posY[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); SetCursorPosition(fruitX,fruitY);
printf("0"); 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; return snakeLength-2;
} }
@ -343,7 +649,21 @@ int game(){
}*/ }*/
int main () { int main () {
gameOver(game()); int mainMenuSelected,score;
initscr();
while(1){
mainMenuSelected=mainMenu();
if(mainMenuSelected==0){
score=game();
gameOver(score);
getchar(); getchar();
return(0); }
if(mainMenuSelected==1){
showScores();
}
if(mainMenuSelected==2){
return 0;
}
}
} }