-1 新建一个 snake.c 文件,写入以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#define U 1
#define D 2
#define L 3
#define R 4
#define width 100
#define height 30
typedef struct BLOCK
{
int x;
int y;
struct BLOCK *next;
}Block;
Block *snakeHead;
Block *p;
Block *food;
int speed = 200;
void drawBlock(int x,int y);
void eraseBlock(int x,int y);
void createWindow();
void createMap();
void createSnake();
void createFood();
void moveSnake();
void endGame();
int main()
{
createWindow();
createMap();
createSnake();
createFood();
moveSnake();
return 0;
}
void drawBlock(int x,int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(handle, coord);
printf("□");
}
void eraseBlock(int x,int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(handle, coord);
printf(" ");
}
void createWindow()
{
char s[30];
sprintf(s,"mode con cols=%d lines=%d",width,height);
system(s);
system("title 贪食蛇游戏");
system("color b");
}
void createMap()
{
int i;
for(i=0;i<width-2;i+=2)
{
drawBlock(i,0);
drawBlock(i,height-1);
}
for(i=0;i<height-1;i++)
{
drawBlock(0,i);
drawBlock(width-2,i);
}
}
void createSnake()
{
snakeHead = (Block *)malloc(sizeof(Block));
snakeHead->x = 44;
snakeHead->y = 15;
snakeHead->next = NULL;
drawBlock(snakeHead->x,snakeHead->y);
p = snakeHead;
int i;
for(i=1;i<5;i++)
{
Block *body = (Block *)malloc(sizeof(Block));
body->x = p->x + 2;
body->y = p->y;
body->next = NULL;
drawBlock(body->x,body->y);
p->next = body;
p = body;
}
}
void createFood()
{
int x;
int y;
while(1){
srand(time(0));
x = (rand() % (width/2-2)) * 2 + 2;
y = rand() % (height-2) + 1;
bool inSnake = false;
p=snakeHead;
while(p->next != NULL)
{
if(p->x == x && p->y == y)
{
inSnake = true;
break;
}
p = p->next;
}
if(inSnake == false){
break;
}
}
food = (Block *)malloc(sizeof(Block));
food->x = x;
food->y = y;
food->next = NULL;
drawBlock(x,y);
}
void moveSnake()
{
int state = L;
int x = snakeHead->x;
int y = snakeHead->y;
while(1){
Sleep(speed);
if(state == U){
y =y-1;
}else if(state == D){
y =y+1;
}else if(state == R){
x = x+2;
}else if(state == L){
x = x-2;
}
if(x==food->x && y==food->y){
food->next = snakeHead;
snakeHead = food;
createFood();
}else{
Block *newHead = (Block *)malloc(sizeof(Block));
newHead->x = x;
newHead->y = y;
newHead->next = snakeHead;
drawBlock(newHead->x,newHead->y);
p = snakeHead;
while(p->next->next != NULL){
p=p->next;
}
eraseBlock(p->next->x,p->next->y);
free(p->next);
p->next = NULL;
snakeHead=newHead;
}
bool gameOver = false;
if(snakeHead->x == 0 || snakeHead->x == width-2 ||snakeHead->y == 0||snakeHead->y ==height-1){
gameOver = true;
}
p=snakeHead->next;
while(p->next != NULL){
if(p->x == snakeHead->x && p->y == snakeHead->y){
gameOver = true;
break;
}
p=p->next;
}
if(gameOver){
endGame();
break;
}
if(GetAsyncKeyState(VK_UP) && state != D){
state = U;
}else if(GetAsyncKeyState(VK_DOWN) && state != U){
state = D;
}else if(GetAsyncKeyState(VK_RIGHT) && state != L){
state = R;
}else if(GetAsyncKeyState(VK_LEFT) && state != R){
state = L;
}
}
}
void endGame()
{
system("cls");
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
coord.X = 30;
coord.Y = 10;
SetConsoleCursorPosition(handle, coord);
printf("Game Over!\n");
}
-2 新建一个run.bat文件,写入以下代码并双击运行:
cd %~dp0
gcc snake.c -Wall -g -fexec-charset=gbk
start a