c语言控制台的一个贪吃蛇小游戏

#include<stdio.h>
#include<Windows.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#define HEAD 1
#define TAIL 2
int score = 0;
bool lastwin = false;
unsigned long speed = 505;
typedef struct Snake
{
	int ch;
	COORD pos;
	Snake *next;
	Snake *last;
} *PriSna;
PriSna s;
PriSna tail = NULL;
int sx, sy;
void goto_x_y(int x, int y);
void Creatsnake(int len = 1);
void gamestart();
void Drawseed();
void goto_x_y(int x, int y)
{
	HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pt;
	pt.X = x;
	pt.Y = y;
	SetConsoleCursorPosition(hout, pt);
	CONSOLE_CURSOR_INFO set = { 1,0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &set); //隐藏光标
}
 
 
void Creatsnake(int len)  //蛇的身体
{
	if (s->next == NULL)
	{
		tail = s;
	}
	for (int i = 0; i < len; i++)
	{
		tail->next = new Snake[1];
		tail->next->last = tail;
		tail = tail->next;
		tail->ch = TAIL;
		tail->pos = tail->last->pos;
		tail->next = NULL;
	}
}
 
 
void DrawMap()
{
	for (int i = 0; i < 40; i++)
	{
		printf("□");
	}
	for (int i = 0; i < 22; i++)
	{
		goto_x_y(0, i + 1);
		printf("□");
		goto_x_y(78, i + 1);
		printf("□");
	}
	goto_x_y(0, 23);
	for (int i = 0; i < 40; i++)
	{
		printf("□");
	}
	goto_x_y(0, 24);
	printf("wasd控制反向,z暂停,自动变速; MADE BY TOUYI");
	goto_x_y(65, 24);
	printf("score:00");
	goto_x_y(0, 0);
}
 
 
bool judgegame()
{
	if (s->pos.X >= 79 || s->pos.X <= 2 || s->pos.Y>22 || s->pos.Y < 1)
		return true;
	else
	{
		PriSna t = s->next;
		while (t != NULL)
		{
			if (s->pos.X == t->pos.X&&s->pos.Y == t->pos.Y)
				return true;
			t = t->next;
		}
	}
	return false;
}
 
 
bool Drawsnake(char cha)
{
	if (cha == 't')
	{
		int two = 2;
		PriSna t = s;
		while (two--)
		{
			goto_x_y(t->pos.X - 2, t->pos.Y);
			if (t->ch == HEAD)
				printf("●");
			else
				printf("■");
			goto_x_y(0, 0);
			t = t->next;
		}
		return true;
	}
	if (cha == 'z' || cha == 'Z')
	{
		PriSna t = s;
		while (t != NULL)
		{
			goto_x_y(t->pos.X - 2, t->pos.Y);
			if (t->ch == HEAD)
				printf("●");
			else
				printf("■");
			goto_x_y(0, 0);
			t = t->next;
		}
	}
	else
	{
		PriSna t;
		if (cha == 'a' || cha == 'A')
		{
			goto_x_y(tail->pos.X, tail->pos.Y);
			t = tail;
			tail = tail->last;
			tail->next = NULL;
			printf("\b \b");
			goto_x_y(s->pos.X, s->pos.Y);
			(*t) = (*s);
			t->ch = TAIL;
			t->next->last = t;
			t->last = s;
			s->next = t;
			s->pos.X -= 2;
			printf("\b \b");
			Drawsnake('t');
		}
		else if (cha == 's' || cha == 'S')
		{
			goto_x_y(tail->pos.X, tail->pos.Y);
			t = tail;
			tail = tail->last;
			tail->next = NULL;
			printf("\b \b");
			goto_x_y(s->pos.X, s->pos.Y);
			(*t) = (*s);
			t->ch = TAIL;
			t->next->last = t;
			t->last = s;
			s->next = t;
			s->pos.Y++;
			printf("\b \b");
			Drawsnake('t');
		}
		else if (cha == 'd' || cha == 'D')
		{
			goto_x_y(tail->pos.X, tail->pos.Y);
			t = tail;
			tail = tail->last;
			tail->next = NULL;
			printf("\b \b");
			goto_x_y(s->pos.X, s->pos.Y);
			(*t) = (*s);
			t->ch = TAIL;
			t->next->last = t;
			t->last = s;
			s->next = t;
			s->pos.X += 2;
			printf("\b \b");
			Drawsnake('t');
		}
		else if (cha == 'w' || cha == 'W')
		{
			goto_x_y(tail->pos.X, tail->pos.Y);
			t = tail;
			tail = tail->last;
			tail->next = NULL;
			printf("\b \b");
			goto_x_y(s->pos.X, s->pos.Y);
			(*t) = (*s);
			t->ch = TAIL;
			t->next->last = t;
			t->last = s;
			s->next = t;
			s->pos.Y--;
			printf("\b \b");
			Drawsnake('t');
		}
	}
	if (s->pos.X == sx + 2 && s->pos.Y == sy)
	{
		Drawseed();
		Creatsnake();
		score += 10;
		goto_x_y(71, 24);
		speed -= 10;
		printf("%d", score);
		return true;
	}
	if (judgegame())
		return false;
	return true;
}
 
 
bool judge(int x, int y)
{
	PriSna t = s;
	while (t != NULL)
	{
		if (t->pos.X == x && t->pos.Y == y)
			return false;
		t = t->next;
	}
	return true;
}
 
 
void Drawseed()  //画食物
{
	srand((unsigned)time(NULL));
	while (1)
	{
		sx = ((rand() % 38) + 1) * 2;
		sy = (rand() % 22) + 1;
		if (judge(sx, sy))
		{
			break;
		}
	}
	goto_x_y(sx, sy);
	printf("▲");
	goto_x_y(0, 0);
}
 
 
void init()
{
	s = new Snake[1];
	PriSna p = s;
	s->pos.X = 16;
	s->pos.Y = 1;
	s->ch = HEAD;
	s->next = NULL;
	s->last = NULL;
	Creatsnake(3);
	p = p->next;
	for (int i = 1; i < 4; i++)
	{
		p->pos.X = (SHORT)(16 - i * 2);
		p->pos.Y = 1;
		p->ch = TAIL;
		p = p->next;
	}
	Drawsnake('z');
	Drawseed();
}
void gamestart()
{
	char ch = 'z', tch = 'd';
	//unsigned long speed = 1000;
	while (1)
	{
		if (_kbhit())
		{
			tch = ch;
			ch = _getch();
		}
		/*if (ch <= '5' && ch > '0')
		{
		speed = (unsigned long)((ch - '0') * 100);
		ch = tch;
		}
		else */
		if (ch == 'z' || ch == 'Z')  //暂停
		{
			_getch();
			ch = tch;
		}
		else if (ch == 'a' || ch == 'A' || ch == 'S' || ch == 's' || ch == 'd' || ch == 'D' || ch == 'W' || ch == 'w')
		{
			if (((ch == 'W' || ch == 'w') && (tch == 's' || tch == 'S')) || ((ch == 'd' || ch == 'D') && (tch == 'a' || tch == 'A')) || ((ch == 's' || ch == 'S') && (tch == 'w' || tch == 'W')) || ((ch == 'a' || ch == 'A') && (tch == 'd' || tch == 'D')))
			{
				ch = tch;
				continue;
			}
			tch = ch;
			if (Drawsnake(ch) == false && lastwin == false)
			{
				goto_x_y(35, 10);
				printf("game over!");
				break;
			}
			else if (lastwin == true)
				break;
		}
		else if (ch == 'q' || ch == 'Q')
			break;
		else
			ch = tch;
		Sleep(speed);
	}
	//此处结束
}
int main()
{
	init();
	DrawMap();
	gamestart();
	_getch();
	return 0;
}

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的贪吃蛇小游戏C语言代码,使用了Windows API库和控制台绘图,可以在Windows系统下运行: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #include <Windows.h> #define WIDTH 30 // 地图宽度 #define HEIGHT 20 // 地图高度 // 方向枚举 enum Direction { UP, DOWN, LEFT, RIGHT }; // 蛇节点结构体 struct SnakeNode { int x; // 节点横坐标 int y; // 节点纵坐标 struct SnakeNode* next; // 指向下一个节点的指针 }; // 全局变量 int score = 0; // 得分 int speed = 200; // 蛇移动速度(毫秒) enum Direction direction = RIGHT; // 初始方向为向右 struct SnakeNode* head; // 蛇头指针 struct SnakeNode* tail; // 蛇尾指针 int food_x, food_y; // 食物坐标 // 初始化地图 void init_map() { system("mode con cols=60 lines=25"); // 设置控制台窗口大小 system("title 贪吃蛇小游戏"); // 设置控制台窗口标题 srand((unsigned int)time(NULL)); // 随机数种子 food_x = rand() % WIDTH; // 随机生成食物坐标 food_y = rand() % HEIGHT; } // 初始化蛇 void init_snake() { head = (struct SnakeNode*)malloc(sizeof(struct SnakeNode)); // 分配蛇头内存空间 head->x = WIDTH / 2; // 初始蛇头坐标为地图中心 head->y = HEIGHT / 2; head->next = NULL; tail = head; // 初始时蛇尾与蛇头重合,只有一个节点 } // 生成食物 void generate_food() { while (1) { // 随机生成食物坐标,直到不与蛇身重合 food_x = rand() % WIDTH; food_y = rand() % HEIGHT; struct SnakeNode* p = head; while (p) { if (p->x == food_x && p->y == food_y) { break; } p = p->next; } if (!p) { // 食物坐标不与蛇身重合,退出循环 break; } } } // 在控制台绘制游戏界面 void draw() { system("cls"); // 清空控制台 printf("贪吃蛇小游戏\n得分:%d\n", score); // 显示得分 for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { if (x == food_x && y == food_y) { // 绘制食物 printf("●"); } else if (x == head->x && y == head->y) { // 绘制蛇头 printf("■"); } else { // 绘制空格 struct SnakeNode* p = head->next; int flag = 0; while (p) { if (p->x == x && p->y == y) { printf("■"); flag = 1; break; } p = p->next; } if (!flag) { printf(" "); } } } printf("\n"); } } // 蛇移动 void move() { // 计算蛇头新坐标 int new_x = head->x, new_y = head->y; switch (direction) { case UP: new_y--; break; case DOWN: new_y++; break; case LEFT: new_x--; break; case RIGHT: new_x++; break; } // 判断是否撞墙或撞到自己 if (new_x < 0 || new_x >= WIDTH || new_y < 0 || new_y >= HEIGHT) { printf("游戏结束!得分:%d\n", score); exit(0); } struct SnakeNode* p = head->next; while (p) { if (p->x == new_x && p->y == new_y) { printf("游戏结束!得分:%d\n", score); exit(0); } p = p->next; } // 判断是否吃到食物 if (new_x == food_x && new_y == food_y) { score += 10; speed -= 10; generate_food(); // 生成新的食物 } else { // 没有吃到食物,蛇尾移动 struct SnakeNode* tmp = tail; tail = tail->next; tail->next = NULL; tail->x = new_x; tail->y = new_y; free(tmp); } // 蛇头移动 head->x = new_x; head->y = new_y; } // 处理键盘输入 void handle_input() { if (GetAsyncKeyState(VK_UP) && direction != DOWN) { // 上 direction = UP; } else if (GetAsyncKeyState(VK_DOWN) && direction != UP) { // 下 direction = DOWN; } else if (GetAsyncKeyState(VK_LEFT) && direction != RIGHT) { // 左 direction = LEFT; } else if (GetAsyncKeyState(VK_RIGHT) && direction != LEFT) { // 右 direction = RIGHT; } } int main() { init_map(); // 初始化地图 init_snake(); // 初始化蛇 generate_food(); // 生成食物 while (1) { draw(); // 绘制游戏界面 move(); // 蛇移动 handle_input(); // 处理键盘输入 Sleep(speed); // 控制蛇移动速度 } return 0; } ``` 运行以上代码后,可以使用方向键控制蛇的移动,吃到食物得分加10,速度加快,撞墙或撞到自己游戏结束,得分将会显示在控制台窗口中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值