C 언어 탐식 뱀 놀이(명령 행)
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
#define SNAKE_LENGTH 100//
#define SCREEN_WIDETH 80
#define SCREEN_HEIGHT 30
//
struct coor{
int x;
int y;
};
//
enum CH {
right = VK_RIGHT,
left = VK_LEFT,
up = VK_UP,
down = VK_DOWN
};
//
struct snake{
int len;//
struct coor coord[SNAKE_LENGTH];//
enum CH CH;//
int SPEED;
int flag;// 1 0
}snake;
//
void gotoxy(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
//
void init_sence()
{
//
for (int i = 0; i < SCREEN_WIDETH; i += 2)
{
gotoxy(i,0);
printf("■");
gotoxy(i, SCREEN_HEIGHT);
printf("■");
}
//
for (int i = 0; i <=SCREEN_HEIGHT; i++)
{
gotoxy(0, i);
printf("■");
gotoxy(SCREEN_WIDETH,i);
printf("■");
}
//
gotoxy(SCREEN_WIDETH + 5, 2);
printf("\t\t ");
gotoxy(SCREEN_WIDETH + 5, 6);
printf("2018//12//1");
gotoxy(SCREEN_WIDETH + 5, 8);
printf(" : ");
gotoxy(SCREEN_WIDETH + 5, 10);
printf("F1: \tF2: ");
gotoxy(SCREEN_WIDETH + 5, 12);
printf("CTRL: \t : ");
gotoxy(SCREEN_WIDETH + 5, 14);
printf("ESC: ");
gotoxy(SCREEN_WIDETH + 5, 28);
printf(" :QQ:2862841130:::");
}
struct foodcoord {
int x;
int y;
int flag;//
}food;
//** c **
#include"snake.h"
//
void move_snake();
//
void draw_snake();
//
void creatfood();
//
void eatfood();
//
void SnakeState();
int main()
{
//
system("mode con cols=110 lines=31");
//
SetConsoleTitleA(" ");
//
begin:
snake.CH = VK_RIGHT;//
snake.len = 5; //
snake.SPEED = 300;//
snake.coord[1].x = SCREEN_WIDETH / 2;//
snake.coord[1].y = SCREEN_HEIGHT / 2;
snake.coord[2].x = SCREEN_WIDETH / 2-2;//
snake.coord[2].y = SCREEN_HEIGHT / 2;
snake.coord[3].x = SCREEN_WIDETH / 2-4;//
snake.coord[3].y = SCREEN_HEIGHT / 2;
//
food.flag = 1;//1 0
//
snake.flag = 1;//1 0
init_sence();//
while (1)
{
draw_snake();//
Sleep(snake.SPEED);//
move_snake();//
if(food.flag)
creatfood();//
eatfood();//
SnakeState();//
if (!snake.flag)break;
}
system("cls");
gotoxy(SCREEN_WIDETH/2, SCREEN_HEIGHT/2-4);
printf(" GAME OVER!!!");
gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+2);
printf(" :\t\t\t%d ",snake.len-1);
gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+4);
printf(" :\t\t\tCTRL ");
gotoxy(SCREEN_WIDETH / 2-6, SCREEN_HEIGHT / 2+6);
printf(" :\t\tESC");
while (1)
{
if (GetAsyncKeyState(VK_CONTROL))
{
system("cls");
goto begin;
}
else if (GetAsyncKeyState(VK_ESCAPE))
return 0;
}
}
//
void move_snake()
{
//
if (GetAsyncKeyState(up))
{
if(snake.CH!=down)snake.CH = up;
}
else if (GetAsyncKeyState(down))
{
if (snake.CH != up)snake.CH = down;
}
else if (GetAsyncKeyState(right))
{
if (snake.CH != left)snake.CH = right;
}
else if (GetAsyncKeyState(left))
{
if (snake.CH != right)snake.CH = left;
}
else if (GetAsyncKeyState(VK_F1))
{
if(snake.SPEED>=100)snake.SPEED -= 50;
}
else if (GetAsyncKeyState(VK_F2))
{
if (snake.SPEED <= 3000)snake.SPEED += 100;
}
//
switch (snake.CH)
{
case right:snake.coord[1].x += 2; break;
case left:snake.coord[1].x -= 2; break;
case up:snake.coord[1].y -= 1; break;
case down:snake.coord[1].y += 1; break;
}
}
//
void draw_snake()
{
//
gotoxy(snake.coord[1].x, snake.coord[1].y);
printf("□");
// , for
for (int i = 2; i <= snake.len; i++)
{
gotoxy(snake.coord[i].x, snake.coord[i].y);
printf("□");
}
//
gotoxy(snake.coord[snake.len].x, snake.coord[snake.len].y);
printf(" ");
//
for (int i = snake.len; i >1; i--)
{
snake.coord[i].x = snake.coord[i - 1].x;
snake.coord[i].y = snake.coord[i - 1].y;
}
gotoxy(0, 0);
printf("■");
gotoxy(85, 25);
printf(" :%d ", snake.len-1);
}
//
void creatfood()
{
//
srand((unsigned)time(NULL));
if(food.flag)
while (1)
{
food.x = rand() % 80;
food.y = rand() % 30;
if (food.x % 2 == 0 && food.x >= 2 && food.x <= 78 && food.y > 1 && food.y < 30)
{
int flag = 0;
//
for (int i = 1; i <= snake.len; i++)
{
if (snake.coord[i].x == food.x&&snake.coord[i].y == food.y)
{
flag = 1;
break;
}
}
if (flag)continue;
//
else
{
gotoxy(food.x, food.y);
printf("⊙");
food.flag = 0;
break;
}
}
}
food.flag = 0;
}
//
void eatfood()
{
//
if (food.x == snake.coord[1].x&&food.y == snake.coord[1].y)
{
snake.len+=1;
food.flag = 1;
}
}
//
void SnakeState()
{
if (snake.coord[1].x < 2 || snake.coord[1].x>78 || snake.coord[1].y < 1 || snake.coord[1].y>29)
snake.flag = 0;
for (int i = 2; i <= snake.len; i++)
{
if (snake.coord[1].x == snake.coord[i].x&&snake.coord[1].y == snake.coord[i].y)
snake.flag = 0;
}
}
더 많은 재 미 있 는 클래식 게임 을 통 해 주 제 를 실현 하고 여러분 에 게 공유 합 니 다.C++클래식 게임 모음
python 클래식 게임 모음
python 러시아 블록 게임 집합
JavaScript 클래식 게임 을 계속 합 니 다.
자바 클래식 게임 모음
javascript 고전 게임 모음
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C 언어 구현 천둥 제거 게임 상세 정보먼저 작은 메뉴를 표시하고 게임을 할지 여부를 선택하십시오.사용자가 종료를 선택하면 프로그램 실행이 끝나고, 사용자가 게임을 선택하면 지뢰 제거 위치 좌표를 입력하라는 메시지가 표시됩니다.사용자가 입력한 좌표가 바둑...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.