지렁이게임 만들다가 자꾸 이유모를 버그가
라미
질문 제목 : 질문 내용 :
#include stdio.h
#include stdlib.h
#include windows.h
#include string.h
#include time.h
#define map_width 96
#define map_height 40
#define left 75
#define right 77
#define up 72
#define down 80
typedef struct jirung2{
int x;
int y;
}jirung2;
void gotoxy(int x, int y);
void drawmap();
void gamestart();
void jirungremove();
void printjirung(int direction,jirung2 food);
void jirungmove(int direction);
void changedirection(int *direction);
int crashcheck();
void putfood(jirung2 *food);
void extendslength();
int checkeatfood(jirung2 food);
jirung2 jirung[15];
int length;
int main(int argc, char *argv[])
{
system(mode con: cols=120 lines=40);
gotoxy(0,0);
gamestart();
getch();
return 0;
}
void gotoxy(int x, int y)
{
coord pos = {x, y};
setconsolecursorposition(getstdhandle(std_output_handle), pos);
}
void drawmap(){
int i;
gotoxy(0,0);
printf(┌);
for(i=1; imap_width/2; i++){
gotoxy(i*2,0);
printf(─);
}
gotoxy(map_width,0);
printf(┐);
for(i=1; imap_height-1; i++){
gotoxy(0,i);
printf(│);
gotoxy(map_width,i);
printf(│);
}
gotoxy(0,map_height-1);
printf(└);
for(i=1; imap_width/2; i++){
gotoxy(i*2,map_height);
printf(─);
}
gotoxy(map_width,map_height-1);
printf(┘);
}
void gamestart(){
int direction=1;
int i;
jirung2 food;
length=4;
jirung[0].x=5;
jirung[0].y=3;
jirung[1].x=4;
jirung[1].y=3;
jirung[2].x=3;
jirung[2].y=3;
jirung[3].x=2;
jirung[3].y=3;
drawmap();
putfood(&food);
printjirurintjirung(direction,food);
getch();
for(i=0; ;i++){
if(i%10==0){
jirungremove();
jirungmove(direction);
if(crashcheck()){
gotoxy(100,20);
printf(crashed!!);
}
i=0;
}
if(kbhit()) changedirection(&direction);
if(checkeatfood(food)){
extendslength();
putfood(&food);
}
sleep(10);
}
}
void printjirung(int direction,jirung2 food){
int i;
char shape[4];
gotoxy(food.x*2,food.y);
printf(★);
switch(direction){
case 0 : strcpy(shape,▲);
break;
case 1 :strcpy(shape,▶);
break;
case 2 : strcpy(shape,▼);
break;
case 3 : strcpy(shape,◀);
break;
}
for(i=0;ilength; i++){
gotoxy(jirung[i].x*2,jirung[i].y);
if(i==0) printf(%s,shape);
else printf(■);
}
}
void jirungmove(int direction){
int i;
for(i=length-1; i0; i--){
jirung[i]=jirung[i-1];
}
gotoxy(jirung[0].x*2,jirung[0].y);
printf(■);
switch(direction){
case 0 :
jirung[0].y--;
gotoxy(jirung[0].x*2,jirung[0].y);
printf(▲);
break;
case 1 : jirung[0].x++;
gotoxy(jirung[0].x*2,jirung[0].y);
printf(▶);
break;
case 2 : jirung[0].y++;
gotoxy(jirung[0].x*2,jirung[0].y);
printf(▼);
break;
case 3 : jirung[0].x--;
gotoxy(jirung[0].x*2,jirung[0].y);
printf(◀);
break;
}
}
void jirungremove(){
if(jirung[length-1].x==jirung[length-2].x && jirung[length-1].y==jirung[length-2].y) return ;
gotoxy(jirung[length-1].x*2,jirung[length-1].y);
p; printf( );
}
void changedirection(int *direction){
char ch;
ch=getch();
switch(ch){
case up : *direction=0;
break;
case right : *direction=1;
break;
case down : *direction=2;
break;
case left : *direction=3;
break;
}
}
int crashcheck(){
int i;
for(i=1; ilength; i++){
if(jirung[0].x==jirung[i].x && jirung[0].y==jirung[i].y) return 1;
}
if(jirung[0].x*22 || jirung[0].x*2=map_width || jirung[0].y1 || jirung[0].y=map_height-1) return 1;
return 0;
}
void putfood(jirung2 *food){
srand(time(null));
int i;
(*food).x=rand()%(map_width/2-2)+2;
(*food).y=rand()%(map_height-2)+1;
while(1){
for(i=0; ilength; i++){
if(jirung[i].x==(*food).x && jirung[i].y==(*food).y){
break;
}
if(i==length-1){
gotoxy((*food).x*2,(*food).y);
printf(★);
return ;
}
}
}
}
int checkeatfood(jirung2 food){
int i;
for(i=0; ilength; i++){
if(jirung[i].x==food.x && jirung[i].y==food.y) return 1;
}
return 0;
}
void extendslength(){
length++;
jirung[length-2]=jirung[length-1];
}지렁이게임 만드는데 먹이를 먹으면 자꾸왼쪽 위에 저부분이 지워지고 먹이를 먹은부분이 저렇게 뭔가 남네요 ㅠㅠ
도저히 이유를 모르겠는데 학교과제라 그냥 무시할수도없고 아 제발 도와주세요