Added crashDetection, obstacles and gameOver screen
This commit is contained in:
parent
f31ef37750
commit
aa36b36dfa
1 changed files with 154 additions and 32 deletions
182
main.cpp
182
main.cpp
|
@ -78,7 +78,7 @@ int crashWall(int posX, int posY){
|
|||
if(posX==(paMaxX-1)||posX==0){
|
||||
return 1;
|
||||
}
|
||||
if(posY==(paMaxX-1)||posY==0){
|
||||
if(posY==(paMaxY-1)||posY==0){
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
@ -92,13 +92,72 @@ int eatFruit(int posX, int posY, int fruitX, int fruitY){
|
|||
return 0;
|
||||
}
|
||||
}
|
||||
int crashSnake(int posX[100],int posY[100]){
|
||||
for(int i=1;posX[i]!=-1;i++){
|
||||
if(posX[0]==posX[i]){
|
||||
if(posY[0]==posY[i]){
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int crashBomb(int posX, int posY, int bombX[100],int bombY[100], bool bombShown[100]){
|
||||
for(int i=0;bombShown[i]==1;i++){
|
||||
if(bombShown[i]==1){
|
||||
if(posX==bombX[i]){
|
||||
if(posY==bombY[i]){
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
SetCursorPosition(0,paMaxY);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void game(){
|
||||
void gameOver(int score){
|
||||
for(int i=0;i<paMaxX;i++){
|
||||
|
||||
clock_t start_t,this_t;
|
||||
for(int j=0;j<paMaxY;j++){
|
||||
|
||||
SetCursorPosition(i,j);
|
||||
printf("\033[45m \033[0m"); //\033[41m
|
||||
}
|
||||
}
|
||||
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 ");
|
||||
SetCursorPosition((paMaxX/2)-34,(paMaxY/2)-6);
|
||||
printf("\033[41m \033[45m \033[41m \033[45m \033[41m \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)-5);
|
||||
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 \033[41m \033[45m ");
|
||||
SetCursorPosition((paMaxX/2)-34,(paMaxY/2)-4);
|
||||
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)-3);
|
||||
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 ");
|
||||
SetCursorPosition((paMaxX/2)-34,(paMaxY/2)-2);
|
||||
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);
|
||||
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 ");
|
||||
int scoreShift=0,temp=score;
|
||||
while(temp>=10){
|
||||
|
||||
scoreShift++;
|
||||
temp=temp/10;
|
||||
}
|
||||
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");
|
||||
}
|
||||
int game(){
|
||||
|
||||
clock_t start_t,this_t,start;
|
||||
start_t=clock();
|
||||
int posX[100],posY[100],fruitX,fruitY,snakeLength=2,ft=0;
|
||||
start=start_t;
|
||||
int posX[100],posY[100],fruitX,fruitY,snakeLength=2,ft=0,bombX[100],bombY[100],bombAmount=3,bombLoop=1;
|
||||
int tempX[2],tempY[2];
|
||||
bool bombShown[100];
|
||||
double thistimeMillisec;
|
||||
srand(time(NULL));
|
||||
/*for(int j=0;j<20;j++){
|
||||
|
@ -120,13 +179,30 @@ void game(){
|
|||
posX[i]=-1;
|
||||
posY[i]=-1;
|
||||
}
|
||||
posX[0]=95;
|
||||
posY[0]=25;
|
||||
posX[1]=95;
|
||||
posY[1]=25;
|
||||
posX[0]=paMaxX/2;
|
||||
posY[0]=paMaxY/2;
|
||||
posX[1]=paMaxX/2;
|
||||
posY[1]=(paMaxX/2)+1;
|
||||
srand(time(NULL));
|
||||
fruitX=rand()%98+1;
|
||||
fruitY=rand()%28+1;
|
||||
fruitX=rand()%96+2;
|
||||
fruitY=rand()%26+2;
|
||||
for(int i=0;i<100;i++){
|
||||
bombX[i]=rand()%98+1;
|
||||
bombY[i]=rand()%28+1;
|
||||
for(int j=0;j<i;j++){
|
||||
while(1){
|
||||
if(bombX[i]!=bombX[j]){
|
||||
if(bombY[i]!=bombY[j]){
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bombX[i]=rand()%98+1;
|
||||
bombY[i]=rand()%28+1;
|
||||
}
|
||||
}
|
||||
bombShown[i]=0;
|
||||
}
|
||||
while(1){ //เริ่มเกม
|
||||
this_t=clock();
|
||||
SetCursorPosition(posX[0],posY[0]);
|
||||
|
@ -135,14 +211,6 @@ void game(){
|
|||
drawFrame();
|
||||
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();//รับจากแป้นพิมพ์
|
||||
|
@ -150,25 +218,25 @@ void game(){
|
|||
break;
|
||||
}
|
||||
if(input=='a'||input==leftArrow){
|
||||
if((posX[0]>1)&&(lastInput!='d')){
|
||||
if((posX[0]>=1)&&(lastInput!='d')){
|
||||
moveSnake(-1,0,posX,posY,tempX,tempY);
|
||||
lastInput='a';
|
||||
}
|
||||
}
|
||||
else if(input=='d'||input==rightArrow){
|
||||
if((posX[0]<98)&&(lastInput!='a')){
|
||||
if((posX[0]<=98)&&(lastInput!='a')){
|
||||
moveSnake(1,0,posX,posY,tempX,tempY);
|
||||
lastInput='d';
|
||||
}
|
||||
}
|
||||
else if(input=='w'||input==upArrow){
|
||||
if((posY[0]>1)&&(lastInput!='s')){
|
||||
if((posY[0]>=1)&&(lastInput!='s')){
|
||||
moveSnake(0,-1,posX,posY,tempX,tempY);
|
||||
lastInput='w';
|
||||
}
|
||||
}
|
||||
else if(input=='s'||input==downArrow){
|
||||
if((posY[0]<28)&&(lastInput!='w')){
|
||||
if((posY[0]<=28)&&(lastInput!='w')){
|
||||
moveSnake(0,1,posX,posY,tempX,tempY);
|
||||
lastInput='s';
|
||||
}
|
||||
|
@ -176,38 +244,91 @@ void game(){
|
|||
else if(input=='b'){
|
||||
lastInput='0';
|
||||
}
|
||||
thistimeMillisec=(double)(this_t-start_t)/(double)CLOCKS_PER_SEC*15;
|
||||
thistimeMillisec=(double)(this_t-start_t)/(double)CLOCKS_PER_SEC*10;
|
||||
if(((int)thistimeMillisec%10)==1){
|
||||
if(lastInput=='a'){
|
||||
if(posX[0]>1){
|
||||
if(posX[0]>=1){
|
||||
moveSnake(-1,0,posX,posY,tempX,tempY);
|
||||
lastInput='a';
|
||||
}
|
||||
}
|
||||
else if(lastInput=='d'){
|
||||
if(posX[0]<98){
|
||||
if(posX[0]<=98){
|
||||
moveSnake(1,0,posX,posY,tempX,tempY);
|
||||
lastInput='d';
|
||||
}
|
||||
}
|
||||
else if(lastInput=='w'){
|
||||
if(posY[0]>1){
|
||||
if(crashWall(posX[0],posY[0])||crashSnake(posX,posY)){
|
||||
|
||||
break;
|
||||
}
|
||||
start_t=this_t;
|
||||
}
|
||||
thistimeMillisec=(double)(this_t-start)/(double)CLOCKS_PER_SEC*7;
|
||||
if(((int)thistimeMillisec%10)==1){
|
||||
if(lastInput=='w'){
|
||||
if(posY[0]>=1){
|
||||
moveSnake(0,-1,posX,posY,tempX,tempY);
|
||||
lastInput='w';
|
||||
}
|
||||
}
|
||||
else if(lastInput=='s'){
|
||||
if(posY[0]<28){
|
||||
if(posY[0]<=28){
|
||||
moveSnake(0,1,posX,posY,tempX,tempY);
|
||||
lastInput='s';
|
||||
}
|
||||
}
|
||||
start_t=this_t;
|
||||
start=this_t;
|
||||
}
|
||||
if(crashBomb(posX[0],posY[0],bombX,bombY,bombShown)){
|
||||
|
||||
break;
|
||||
}
|
||||
if(eatFruit(posX[0],posY[0],fruitX,fruitY)==1){
|
||||
snakeLength++;
|
||||
fruitX=rand()%96+2;
|
||||
fruitY=rand()%26+2;
|
||||
for(int k=0;bombShown[k]==1;k++){
|
||||
while(1){
|
||||
if(fruitX!=bombX[k]){
|
||||
if(fruitY!=bombY[k]){
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fruitX=rand()%96+2;
|
||||
fruitY=rand()%26+2;
|
||||
}
|
||||
}
|
||||
if(snakeLength<=100){
|
||||
posX[snakeLength-1]=0;
|
||||
posY[snakeLength-1]=0;
|
||||
}
|
||||
}
|
||||
SetCursorPosition(fruitX,fruitY);
|
||||
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){
|
||||
bombLoop++;
|
||||
bombShown[bombLoop-2]=1;
|
||||
bombAmount=bombLoop*3;
|
||||
|
||||
}
|
||||
for(int i=0;i<100;i++){
|
||||
|
||||
if(bombShown[i]==1){
|
||||
|
||||
SetCursorPosition(bombX[i],bombY[i]);
|
||||
printf("\033[1;97m\033[41m \033[0m");
|
||||
}
|
||||
|
||||
}
|
||||
//system("((speaker-test -t sine -f 700)& pid=$!; sleep 0.1s; kill -9 $pid) > /dev/null");
|
||||
}
|
||||
return snakeLength-2;
|
||||
}
|
||||
|
||||
|
||||
|
@ -222,6 +343,7 @@ void game(){
|
|||
|
||||
}*/
|
||||
int main () {
|
||||
game();
|
||||
gameOver(game());
|
||||
getchar();
|
||||
return(0);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue