간단한 게임 만들다가 질문
꽃가람
질문 제목 : 질문 내용 :
#include stdio.h
#include stdlib.h
#include windows.h
#include time.h
#include conio.h
#define width 60
#define height 30
#define start_length 5
#define up 72
#define down 80
#define left 75
#define right 77
typedef struct snake{
int x;
int y;
}snake;
void increase(snake *snake);
void gamestart();
int crashcheck(snake * snake);
void drawmap();
void showstate(int score);
void putfood(snake * food, snake * snake);
void gotoxy(int x, int y);
void initsnake(snake *snake, snake *food);
int length=start_length;
int main(void)
{
system(mode con cols=80 lines=30);
gotoxy(30,15);
printf(press any key\n);
getch();
system(cls);
gamestart();
system(cls);
printf(탈락 ㅅㄱ);
getch();
return ;
}
void gamestart(){
snake food;
int direction=0;
int i;
int game_speed=160;
int score=0;
snake* snake = (snake*)malloc(sizeof(snake) * start_length);
srand(time(null));
initsnake(snake,&food);
while(1){
switch(getch()){
case up : if(direction!=1) direction=0;
break;
case down : if(direction!=0) direction=1;
break;
case left : if(direction!=3) direction=2;
break;
p; case right : if(direction!=2) direction=3;
break;
}
while(!kbhit()){
gotoxy(snake[0].x,snake[0].y); printf(■);
if (!(snake[length-1].x == snake[length-2].x && snake[length-1].y == snake[length-2].y)){
gotoxy( snake[length-1].x, snake[length-1].y);printf( );
}
for(i=length-1; i0; i--){
snake[i]=snake[i-1];
}
switch(direction){
case 0 :
gotoxy(snake[0].x, --snake[0].y);
printf(▲);
break;
case 1 :
gotoxy(snake[0].x, ++snake[0].y);
printf(▼);
break;
case 2 :
gotoxy(snake[0].x-=2, snake[0].y);
printf(◀);
break;
case 3 :
gotoxy(snake[0].x+=2, snake[0].y);
printf(▶);
break;
}
if(food.x==snake[0].x && food.y==snake[0].y){
increase(snake);
game_speed-=2;
score+=length*5;
 sp; showstate(score);
putfood(&food,snake);
}
if(crashcheck(snake)){
free(snake);
return ;
}
score+=length-start_length;
sleep(game_speed);
}
}
}
void drawmap(){
int i;
gotoxy(0,0);
printf(┌);
for(i=2; i=width-4; i+=2){
gotoxy(i,0);
printf(─);
}
gotoxy(width-2,0);
printf(┐);
for(i=1; iheight-1; i++){
gotoxy(0,i);
printf(│);
gotoxy(width-2,i);
printf(│);
}
gotoxy(0,height-1);
printf(└);
for(i=2; i=width-4; i+=2){
gotoxy(i,height-1);
printf(─);
}
gotoxy(width-2,height);
printf(┘);
}
void showstate(int score){
gotoxy(64,10);
printf(현재 뱀 길이);
gotoxy(68,12);
printf(%d,length);
gotoxy(64,15);
printf(현재 점수);
gotoxy(68,17);
printf(%d,score);
gotoxy(64,20);
printf(이동 : 방향키);
}
void putfood(snake * food, snake * snake){
int i;
srand(time(null));
do{
food-x=rand()%28*2+2;
food-y=rand()%28+1;
for(i=0; ilength; i++){
if(food-x==snake[i].x && food-y==snake[i].y) break;
if(i==length-1){
gotoxy(food-x, food-y);
printf(●);
return ;
}
}
}while(1);
}
void gotoxy(int x, int y)
{
coord pos = {x,y};
setconsolecursorposition(getstdhandle(std_output_handle),pos);
}
int crashcheck(snake * snake){
int i;
if(snake[0].x2 || snake[0].xwidth-4 || snake[0].y1 || snake[0].yheight-2) return 1;
for(i=1; ilength; i++){
if(snake[i].x==snake[0].x && snake[i].y==snake[0].y) return 1;
}
return 0;
}
void initsnake(snake *snake, snake *food){
int i;
drawmap();
for(i=0; istart_length; i++){
if(i==0){
snake[0].x=30;
snake[0].y=20;
gotoxy(30,20);
&n printf(▲);
}
else{
snake[i].x=30;
snake[i].y=20+i;
gotoxy(snake[i].x,snake[i].y);
printf(■);
}
}
showstate(0);
putfood(food,snake);
}
void increase(snake *snake){
int i;
length++;
snake *tmp=snake;
snake=(snake *)malloc(sizeof(snake)*length);
for(i=0; ilength; i++){
snake[i]=tmp[i];
if(i==length-1){
snake[i]=snake[i-1];
}
}
free(tmp);
}
이렇게 짰는데요
몇가지 버그가 있어요
● 이걸먹으면 죽는버그 어캐어캐 고치나요? ㅠㅠ
-
가을빛
아뇨 에러는 안뜨고 진행중인 함수가 꺼지네요
-
루나
이거 실행시면 잘되다가 갑자기 에러 뜨면서 꺼지지 않나요??
-
목소리
?? 더 이상 사용하지 않을려고 free시킨건데;;
-
예린
제가 잘못 생각한건지는 모르겠는데 사용하고 있는 메모리를 free하시면 ㅎㅎ;;