본문 바로가기

옛날옛적+맛보기 공부/c언어

c언어로 구현한 스네이크게임(지렁이 게임)

개발툴은 dev c++ 이며 작년에 구현한 게임입니다.

 

 

#include<stdio.h>//72 up, 80 down, 75 left, 77 right

#include<stdlib.h>

#include<windows.h>

#include<conio.h>

#include<time.h>

int A[100]={0,},B[100]={0,},totalX=0,totalY=0,lon=4,chack=20,u=0;   //전역변수

typedef struct Ranker{

int a;

char b[5];

struct Ranker *next;

}Ranker;

void gotoxy(int x, int y); //콘솔커서 이동 함 수 

void swap(int *a,int *b);   //스왑함수 

void screan();  // 배경 함수

void head(int *hendle,int *back, int *headx,int *heady); // 지렁이 머리부분표현 함수 

void move(int headx,int heady);  // 지렁이 꼬리함수 (꼬리함수를 이용하여 몸통을 표현 

void gameover(int *hendle,int *back, int *headx,int *heady,int *scan,int *z,int * sl,int *st);   //game over 시 등장 함수

void randem(int *x,int *y);  // 난수 함수

void textcolor(int color_number);  //출력문자 색변경

void mouse(); // 콘솔 커서 모양 변경함수

int QUX(int a, int b);   // 자료구조 큐를 활용한 지렁이의 몸통들의 x좌표 

int QUY(int a, int b);   // 자료구조 큐를 활용한 지렁이의 몸통들의 y좌 표  

int scaning(int headx,int heady);

int main(){

int i,chack2;

char c[20]="NULL";

Ranker *ranker;

Ranker *rank;

Ranker *p;

for(i=0;i<10;i++){

p=(Ranker*)malloc(sizeof(Ranker));

p->a=0;

strncpy(p->b,c,4);

if(i==0){

rank=p;

}

else{

ranker->next=p;

}

ranker=p;

}

mouse();

int hendle=77,back=77,headx=8,heady=12,scan=0,x,y,z=0,sl=200,st=15;  //변수들

screan();

while(1){

if(z<=0){

randem(&x,&y);

z++;

}

move(headx,heady);

head(&hendle,&back,&headx,&heady);

scan=scaning(headx,heady);

if(scan==1){

gameover(&hendle,&back,&headx,&heady,&scan,&z,&sl,&st);

int d=lon-4;

gotoxy(10,12);

printf("이름을 세 글자만  입력해 주세요 :");

gets(c);

system("cls");

ranker=rank;

for(i=0;i<10;i++){

if(ranker->a<d){

u++;

if(u==1){

chack=i;

}

swap(&ranker->a,&d);

strcpy(ranker->b,c);

ranker=ranker->next;

}

else{

ranker=ranker->next;

}

}

u=0;

ranker=rank;

if(chack==0){while(1){

system("cls");

gotoxy(16,8);

Sleep(300);

printf("1등: %s",ranker->b);

gotoxy(36,8);

printf("%d",ranker->a);

Sleep(500);

u++;

if(u==5){

u=0;

break;

}

}

}

else{

gotoxy(16,8);

printf("1등: %s",ranker->b);

gotoxy(36,8);

printf("%d",ranker->a);

Sleep(500);}

for(i=0;i<9;i++){

ranker=ranker->next;

if(chack==i+1){while(1){

gotoxy(16,9+i);

printf("                                    ");

gotoxy(16,9+i);

Sleep(300);

printf("%d등: %s",i+2,ranker->b);

gotoxy(36,9+i);

printf("%d",ranker->a);

Sleep(500);

u++;

if(u==5){

u=0;

break;

}

}

}

else{gotoxy(16,9+i);

printf("%d등: %s",i+2,ranker->b);

gotoxy(36,9+i);

printf("%d",ranker->a);

Sleep(500); }

}

chack=20;

while(1){

if(kbhit()){

chack2=getch();

if(chack2==13){

chack2=0;

break;

}

}

Sleep(100);

}

totalX=0,totalY=0,lon=4;

system("cls");

screan();

strcpy(c,"NULL");

}

Sleep(sl);

if(headx==x&&heady==y){

lon++;

z--;

if(st>0){

sl=sl-st;

st--;

}

}

}

system("pause");

}

 

 

 

void gotoxy(int x, int y) 

 COORD Pos = {x - 1, y - 1}; 

 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos); 

 

 

 

void swap(int *a,int *b){

int c;

c=*a;

*a=*b;

*b=c;

}

 

 

 

void screan()

{

int i,j;

for(i=0;i<24;i++){

if(i==0||i==23){

gotoxy(2,i+1);

for(j=0;j<24;j++){

printf("■");

}

printf("\n"); 

}

else{

gotoxy(2,i+1);

printf("■");

gotoxy(48,i+1);

printf("■\n");

}

}

}

 

 

 

void head(int *hendle,int *back, int *headx,int *heady){

int x=*headx,y=*heady;

if(kbhit()){

*hendle=getch();

if(*hendle==224){

*hendle=getch();

}

}

if(*hendle==72){

if(*back==80){

*hendle=*back;

*heady=*heady+2;

}

*heady=*heady-1;

}

else if(*hendle==80){

if(*back==72){

*hendle=*back;

*heady=*heady-2;

}

*heady=*heady+1;

}

else if(*hendle==75){

if(*back==77){

*hendle=*back;

*headx=*headx+4;

}

*headx=*headx-2;

}

else if(*hendle==77){

if(*back==75){

*hendle=*back;

*headx=*headx-4;

}

*headx=*headx+2;

}

gotoxy(*headx,*heady);

textcolor(11);

printf("■");

gotoxy(x,y);

printf("□");

*back=*hendle;

}

 

 

 

void move(int headx,int heady){

if(totalX>=lon){

QUX(1,headx);

headx=QUX(0,headx);

QUY(1,heady);

heady=QUY(0,heady);

gotoxy(headx,heady);

printf(" ");

}

else{

QUX(1,headx);

QUY(1,heady);

}

}

 

 

 

void gameover(int *hendle,int *back, int *headx,int *heady,int *scan,int *z,int * sl,int *st){

int x=4,i=28,y=12,h;

while(1){

system("cls");

textcolor(15);

gotoxy(6,15);

printf("랭커 등록을 하시려면 'R'키를 눌러주세요.");

screan();

if(x<27){

gotoxy(x,y);

textcolor(11);

printf("G a m e   O v e r");

x=x+2;

}

else{

gotoxy(i,y);

textcolor(11);

printf("G a m e   O v e r");

i=i-2;

if(i==4){

x=x-24;

i=28;

}

}

Sleep(100);

if(kbhit()){

h=getch();

if(h==114){

system("cls");

*hendle=77,*back=77,*headx=8,*heady=12,*scan=0,*z=0,*sl=200,*st=15;

break;

}

}

}

}

 

 

 

 

void mouse(){

CONSOLE_CURSOR_INFO cursorInfo = { 0, };

    cursorInfo.dwSize = 1;

    cursorInfo.bVisible = FALSE;

    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);

}

int QUX(int a, int b){

int i,z;

if(a==1){

A[totalX]=b;

totalX++;

}

else if(a==0){

z=A[0];

A[0]=0;

for(i=0;i<totalX;i++){

swap(&A[i],&A[i+1]);

}

totalX--;

return z;

}

}

 

 

 

 

int QUY(int a, int b){

int i,z;

if(a==1){

B[totalY]=b;

totalY++;

}

else if(a==0){

z=B[0];

B[0]=0;

for(i=0;i<totalY;i++){

swap(&B[i],&B[i+1]);

}

totalY--;

return z;

}

}

 

 

 

 

int scaning(int headx,int heady){

int i;

if(heady==1||heady==24&&headx>=2&&headx<=48){

return 1;

}

else if(heady>=2&&heady<=24&&headx==2||headx==48){

return 1;

}

for(i=0;i<lon+1;i++){

if(headx==A[i]&&heady==B[i]){

return 1;

}

}

}

 

 

 

 

void randem(int *x,int *y){

int i;

srand(time(NULL));

*x=rand()%41+4;

if(*x%2==1){

*x=*x+1;

}

*y=rand()%21+2;

for(i=0;i<lon+1;i++){

if(*x==A[i]&&*y==B[i]){

while(1){

*x=rand()%41+4;

if(*x%2==1){

*x=*x+1;

}

*y=rand()%21+2;

if(*x!=A[i]&&*y!=B[i])break;

}

}

}

gotoxy(*x,*y);

textcolor(12);

printf("◎");

}

 

 

 

 

void textcolor(int color_number){

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color_number);

}

 

 

굉장히 지저분합니다 ㅠㅠ 원래는 연결리스트의 큐를 이용했어야했는데 당시 제가 연결리스트의 이해도가 많이 떨어졌었어서 배열로 구현하였습니다.

'옛날옛적+맛보기 공부 > c언어' 카테고리의 다른 글

visual studio 에서의 설정문제  (0) 2018.12.26