Small cleanup

This commit is contained in:
Late Night Defender 2021-11-15 22:26:30 +07:00
parent b3108b71fd
commit 26d6b7940e

View file

@ -6,8 +6,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <string> #include <string>
#include <string.h>
#include <string>
using namespace std; using namespace std;
#define termMaxX 145 #define termMaxX 145
@ -22,10 +20,10 @@ using namespace std;
void drawVoid(){ void drawVoid(){
printf("\033[1;32m\033[40m \033[0m"); //\033[1;32m green text,\033[40m black bg, display " ",\033[0m set default printf("\033[1;32m\033[40m \033[0m");
} }
void drawSnake(){ void drawSnake(){
printf("\033[1;32m\033[40m*\033[0m"); //\033[1;32m green text,\033[40m black bg, display *,\033[0m set default printf("\033[1;32m\033[40m*\033[0m");
} }
void SetCursorPosition(int XPos, int YPos) { void SetCursorPosition(int XPos, int YPos) {
printf("\033[%d;%dH",YPos+1,XPos+1); printf("\033[%d;%dH",YPos+1,XPos+1);
@ -120,8 +118,6 @@ 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(); cbreak();
noecho(); noecho();
scrollok(stdscr, TRUE); scrollok(stdscr, TRUE);
@ -136,6 +132,7 @@ void gameOver(int score){
printf("\033[45m \033[0m"); //\033[41m printf("\033[45m \033[0m"); //\033[41m
} }
} }
//Draws GAME OVER
SetCursorPosition((paMaxX/2)-34,(paMaxY/2)-7); SetCursorPosition((paMaxX/2)-34,(paMaxY/2)-7);
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 "); 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 ");
SetCursorPosition((paMaxX/2)-34,(paMaxY/2)-6); SetCursorPosition((paMaxX/2)-34,(paMaxY/2)-6);
@ -151,7 +148,7 @@ void gameOver(int score){
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"); printf("\e[?25h");
//For score values with multiple digits
int scoreShift=0,temp=score; int scoreShift=0,temp=score;
while(temp>=10){ while(temp>=10){
@ -162,9 +159,7 @@ void gameOver(int score){
printf("Score: %d", score); printf("Score: %d", score);
SetCursorPosition((paMaxX/2)-19,(paMaxY/2)+6); SetCursorPosition((paMaxX/2)-19,(paMaxY/2)+6);
printf("Enter you name: "); printf("Enter you name: ");
SetCursorPosition((paMaxX/2)-1,(paMaxY/2)+6); //Save player scores at $HOME/.config/cursedSnakeGameScores
//cin>>score;
FILE *cmd=popen("echo -n $HOME", "r"); FILE *cmd=popen("echo -n $HOME", "r");
char result[100]={0x0}; char result[100]={0x0};
fgets(result, sizeof(result), cmd); fgets(result, sizeof(result), cmd);
@ -179,7 +174,7 @@ void gameOver(int score){
return; return;
} }
string playerName; string playerName;
//Asks for player name
scanf("%s",playerName.c_str()); scanf("%s",playerName.c_str());
fprintf(fptr,"%s %d\n", playerName.c_str(), score); fprintf(fptr,"%s %d\n", playerName.c_str(), score);
fclose(fptr); fclose(fptr);
@ -188,7 +183,6 @@ void gameOver(int score){
int mainMenu() { int mainMenu() {
int currentButtonState=0,menuStarted=0; int currentButtonState=0,menuStarted=0;
printf("\e[?25l");//ปิดcursor printf("\e[?25l");//ปิดcursor
//initscr();
cbreak(); cbreak();
noecho(); noecho();
scrollok(stdscr, TRUE); scrollok(stdscr, TRUE);
@ -301,7 +295,7 @@ int mainMenu() {
void showScores() { void showScores() {
endwin(); endwin();
int line=0,currentLine=0; int line=0,currentLine=0;
char c; char c; //UserInputCharacter
FILE *cmd=popen("echo -n $HOME", "r"); FILE *cmd=popen("echo -n $HOME", "r");
char result[100]={0x0}; char result[100]={0x0};
fgets(result, sizeof(result), cmd); fgets(result, sizeof(result), cmd);
@ -375,9 +369,7 @@ void showScores() {
currentLine++; currentLine++;
} }
} }
fclose(fptr); fclose(fptr);
drawFrame(); drawFrame();
SetCursorPosition(5,3); SetCursorPosition(5,3);
printf("Name"); printf("Name");
@ -390,18 +382,13 @@ void showScores() {
noecho(); noecho();
scrollok(stdscr, TRUE); scrollok(stdscr, TRUE);
nodelay(stdscr, TRUE); nodelay(stdscr, TRUE);
//int j=0;
while(1){ while(1){
usleep(10000); usleep(10000);
c = getch(); c = getch();
if(showLinesIndicator==1){ if(showLinesIndicator==1){
if(c==upArrow){ if(c==upArrow){
for(int i=1;i<paMaxX-1;i++){ for(int i=1;i<paMaxX-1;i++){
for(int j=4;j<paMaxY-1;j++){ for(int j=4;j<paMaxY-1;j++){
SetCursorPosition(i,j); SetCursorPosition(i,j);
printf("\033[0m "); printf("\033[0m ");
} }
@ -410,9 +397,7 @@ void showScores() {
} }
if(c==downArrow){ if(c==downArrow){
for(int i=1;i<paMaxX-1;i++){ for(int i=1;i<paMaxX-1;i++){
for(int j=4;j<paMaxY-1;j++){ for(int j=4;j<paMaxY-1;j++){
SetCursorPosition(i,j); SetCursorPosition(i,j);
printf("\033[0m "); printf("\033[0m ");
} }
@ -431,7 +416,6 @@ void showScores() {
} }
if(showLinesIndicator==0){ if(showLinesIndicator==0){
for(int i=currentLine;i<line;i++){ for(int i=currentLine;i<line;i++){
SetCursorPosition(5,4+i); SetCursorPosition(5,4+i);
printf("%s", playerName[i].c_str()); printf("%s", playerName[i].c_str());
SetCursorPosition(50,4+i); SetCursorPosition(50,4+i);
@ -456,17 +440,11 @@ int game(){
bool bombShown[100]; bool bombShown[100];
double thistimeMillisec; double thistimeMillisec;
srand(time(NULL)); srand(time(NULL));
/*for(int j=0;j<20;j++){
star[0][j]=rand()%4+2;//2-5
}*/
char input,lastInput; char input,lastInput;
//initscr();
cbreak(); cbreak();
noecho(); noecho();
scrollok(stdscr, TRUE); scrollok(stdscr, TRUE);
nodelay(stdscr, TRUE); nodelay(stdscr, TRUE);
//sleep(1);//รอโปรแกรมเปิด
start_color(); start_color();
init_pair(1,COLOR_WHITE, COLOR_BLACK); init_pair(1,COLOR_WHITE, COLOR_BLACK);
wbkgd(stdscr, COLOR_PAIR(1)); wbkgd(stdscr, COLOR_PAIR(1));
@ -492,7 +470,6 @@ int game(){
break; break;
} }
} }
bombX[i]=rand()%98+1; bombX[i]=rand()%98+1;
bombY[i]=rand()%28+1; bombY[i]=rand()%28+1;
} }
@ -504,9 +481,7 @@ int game(){
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 i=0;i<paMaxX;i++){
for(int j=0;j<paMaxY;j++){ for(int j=0;j<paMaxY;j++){
SetCursorPosition(i,j); SetCursorPosition(i,j);
printf("\033[0m "); printf("\033[0m ");
} }
@ -597,11 +572,9 @@ int game(){
while(1){ while(1){
if(fruitX!=bombX[k]){ if(fruitX!=bombX[k]){
if(fruitY!=bombY[k]){ if(fruitY!=bombY[k]){
break; break;
} }
} }
fruitX=rand()%96+2; fruitX=rand()%96+2;
fruitY=rand()%26+2; fruitY=rand()%26+2;
} }
@ -614,40 +587,22 @@ int game(){
} }
SetCursorPosition(fruitX,fruitY); SetCursorPosition(fruitX,fruitY);
printf("0"); printf("0");
//SetCursorPosition(0,29);for(int i=0;i<10;i++){printf("%d:%d %d |",i+1,bombX[i],bombY[i]);}
//SetCursorPosition(0,29);for(int a=0;bombShown[a]==1;a++){printf("%d ",bombShown[a]);}
//SetCursorPosition(0,29);for(int a=0;a<100;a++){printf("%d ",bombShown[a]);}
if(snakeLength-2>=bombAmount){ if(snakeLength-2>=bombAmount){
bombLoop++; bombLoop++;
bombShown[bombLoop-2]=1; bombShown[bombLoop-2]=1;
bombAmount=bombLoop*3; bombAmount=bombLoop*3;
} }
for(int i=0;i<100;i++){ for(int i=0;i<100;i++){
if(bombShown[i]==1){ if(bombShown[i]==1){
SetCursorPosition(bombX[i],bombY[i]); SetCursorPosition(bombX[i],bombY[i]);
printf("\033[1;97m\033[41m \033[0m"); printf("\033[1;97m\033[41m \033[0m");
} }
} }
//
} }
return snakeLength-2; return snakeLength-2;
} }
/*int mainmenu(){
for(int i=0;i<paMaxY;i++){
for(int j=0;j<paMaxX;j++){
SetCursorPosition(j,i);
printf("\033[100m \033[0m");
}
}
}*/
int main () { int main () {
int mainMenuSelected,score; int mainMenuSelected,score;
initscr(); initscr();
@ -656,7 +611,6 @@ int main () {
if(mainMenuSelected==0){ if(mainMenuSelected==0){
score=game(); score=game();
gameOver(score); gameOver(score);
getchar(); getchar();
} }
if(mainMenuSelected==1){ if(mainMenuSelected==1){