간단한 게임 만들다가 질문
큰나라
질문 제목 : 질문 내용 :
#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하시면 ㅎㅎ;;
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692144 | C언어와 리눅스에 대한 질문입니다. | 싴흐한세여니 | 2025-04-20 |
2692114 | 컨텍스트 스위칭하는데 걸리는 시간 측정.. | YourWay | 2025-04-19 |
2692086 | 간접참조 연산자, 증감연산자 질문이용! (2) | 블랙캣 | 2025-04-19 |
2692056 | 주석좀 달아주세요. 몇개적엇는데 몇개만달아주세요. (2) | DevilsTears | 2025-04-19 |
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |
2691697 | if 문에서 구조체 배열에 저장되있던 문자열 검사하는 법 ? (2) | 민트맛사탕 | 2025-04-16 |
2691678 | C언어 함수 질문이요~!!! | 연보라 | 2025-04-15 |
2691650 | 반복문 | 돋가이 | 2025-04-15 |
2691618 | 링크드리스트 개념 질문이예요 (3) | 맨마루 | 2025-04-15 |
2691592 | 동적할당 이용 배열선언 질문입니다.ㅠㅠ (3) | 허리달 | 2025-04-15 |
2691542 | /=의 용도를 알려주세요 ㅠㅠ! (2) | 아라 | 2025-04-14 |
2691510 | sizeof 연산자 질문입니다 (2) | 종달 | 2025-04-14 |
2691483 | 파일 오픈시 에러 질문드립니다. (2) | 호습다 | 2025-04-14 |
2691450 | [visual c++ 툴]기초 질문 (3) | 해긴 | 2025-04-13 |