add main menu, wall crash detect and score counting
This commit is contained in:
parent
2e6ec34afb
commit
f31ef37750
1 changed files with 54 additions and 13 deletions
67
main.cpp
67
main.cpp
|
@ -9,6 +9,8 @@ using namespace std;
|
||||||
|
|
||||||
#define termMaxX 145
|
#define termMaxX 145
|
||||||
#define termMaxY 36
|
#define termMaxY 36
|
||||||
|
#define paMaxX 100
|
||||||
|
#define paMaxY 30
|
||||||
#define upArrow 65
|
#define upArrow 65
|
||||||
#define downArrow 66
|
#define downArrow 66
|
||||||
#define rightArrow 67
|
#define rightArrow 67
|
||||||
|
@ -54,19 +56,17 @@ void moveSnake(int deltaX, int deltaY, int posX[100], int posY[100], int tempX[2
|
||||||
drawSnake();
|
drawSnake();
|
||||||
sw=0;
|
sw=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
void drawFrame(){
|
void drawFrame(){
|
||||||
|
|
||||||
for(int i=0;i<30;i++){
|
for(int i=0;i<paMaxY;i++){
|
||||||
for(int j=0;j<100;j++){
|
for(int j=0;j<paMaxX;j++){
|
||||||
if(i==0||i==29){
|
if(i==0||i==(paMaxY-1)){
|
||||||
SetCursorPosition(j,i);
|
SetCursorPosition(j,i);
|
||||||
printf("\033[45m \033[0m");
|
printf("\033[45m \033[0m");
|
||||||
}
|
}
|
||||||
else if(j==0||j==99){
|
else if(j==0||j==(paMaxX-1)){
|
||||||
SetCursorPosition(j,i);
|
SetCursorPosition(j,i);
|
||||||
printf("\033[45m \033[0m");
|
printf("\033[45m \033[0m");
|
||||||
}
|
}
|
||||||
|
@ -74,10 +74,30 @@ void drawFrame(){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main () {
|
int crashWall(int posX, int posY){
|
||||||
|
if(posX==(paMaxX-1)||posX==0){
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if(posY==(paMaxX-1)||posY==0){
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int eatFruit(int posX, int posY, int fruitX, int fruitY){
|
||||||
|
if(posX==fruitX&&posY==fruitY){
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void game(){
|
||||||
|
|
||||||
clock_t start_t,this_t;
|
clock_t start_t,this_t;
|
||||||
start_t=clock();
|
start_t=clock();
|
||||||
int posX[100],posY[100],fruitX,fruitY,ft=0;
|
int posX[100],posY[100],fruitX,fruitY,snakeLength=2,ft=0;
|
||||||
int tempX[2],tempY[2];
|
int tempX[2],tempY[2];
|
||||||
double thistimeMillisec;
|
double thistimeMillisec;
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
@ -91,14 +111,12 @@ int main () {
|
||||||
noecho();
|
noecho();
|
||||||
scrollok(stdscr, TRUE);
|
scrollok(stdscr, TRUE);
|
||||||
nodelay(stdscr, TRUE);
|
nodelay(stdscr, TRUE);
|
||||||
sleep(1);//รอโปรแกรมเปิด
|
//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));
|
||||||
printf("\e[?25l");//ปิดcursor
|
printf("\e[?25l");//ปิดcursor
|
||||||
for(int i=0;i<100;i++){
|
for(int i=0;i<100;i++){
|
||||||
|
|
||||||
posX[i]=-1;
|
posX[i]=-1;
|
||||||
posY[i]=-1;
|
posY[i]=-1;
|
||||||
}
|
}
|
||||||
|
@ -112,12 +130,21 @@ int main () {
|
||||||
while(1){ //เริ่มเกม
|
while(1){ //เริ่มเกม
|
||||||
this_t=clock();
|
this_t=clock();
|
||||||
SetCursorPosition(posX[0],posY[0]);
|
SetCursorPosition(posX[0],posY[0]);
|
||||||
|
|
||||||
if(ft<5){//สร้างงูรอบแรกสุด
|
if(ft<5){//สร้างงูรอบแรกสุด
|
||||||
drawSnake();
|
drawSnake();
|
||||||
drawFrame();
|
drawFrame();
|
||||||
ft++;
|
ft++;
|
||||||
}
|
}
|
||||||
|
if(eatFruit(posX[0],posY[0],fruitX,fruitY)==1){
|
||||||
|
snakeLength++;
|
||||||
|
fruitX=rand()%98+1;
|
||||||
|
fruitY=rand()%28+1;
|
||||||
|
posX[snakeLength-1]=posX[0];
|
||||||
|
posY[snakeLength-1]=posY[0];
|
||||||
|
|
||||||
|
}
|
||||||
|
SetCursorPosition(0,0);
|
||||||
|
printf("\033[1;97\033[45mScore: %d\033[0m", snakeLength-2);
|
||||||
input=getch();//รับจากแป้นพิมพ์
|
input=getch();//รับจากแป้นพิมพ์
|
||||||
if(input=='x'){
|
if(input=='x'){
|
||||||
break;
|
break;
|
||||||
|
@ -149,7 +176,6 @@ int main () {
|
||||||
else if(input=='b'){
|
else if(input=='b'){
|
||||||
lastInput='0';
|
lastInput='0';
|
||||||
}
|
}
|
||||||
|
|
||||||
thistimeMillisec=(double)(this_t-start_t)/(double)CLOCKS_PER_SEC*15;
|
thistimeMillisec=(double)(this_t-start_t)/(double)CLOCKS_PER_SEC*15;
|
||||||
if(((int)thistimeMillisec%10)==1){
|
if(((int)thistimeMillisec%10)==1){
|
||||||
if(lastInput=='a'){
|
if(lastInput=='a'){
|
||||||
|
@ -182,5 +208,20 @@ int main () {
|
||||||
printf("0");
|
printf("0");
|
||||||
//system("((speaker-test -t sine -f 700)& pid=$!; sleep 0.1s; kill -9 $pid) > /dev/null");
|
//system("((speaker-test -t sine -f 700)& pid=$!; sleep 0.1s; kill -9 $pid) > /dev/null");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*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 () {
|
||||||
|
game();
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue