c语言初学者试写贪吃蛇

翻出来之前用c语言写的贪吃蛇,语法都比较简单,希望可以交流一下并给一点建议。

源码:

#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>

void Welcome();             //欢迎界面
void Finish();              //结束界面
void Over();                //通关界面
void Start();               //游戏界面
void Display(int x, int y);  //打印蛇
void DisplayFood(int x, int y);//打印食物
int GameOver();             //失败判定
void Up();                  //往上走
void Down();                //往下走
void Left();                //往左走
void Right();               //往右走
void Produce();             //产生食物
void Eat();                 //吃球后变长
void HideCursor();          //隐藏光标

typedef struct Snakes
{
	int x;
	int y;
}snake;

char direction;             //蛇的移动方向
snake s[362];               //蛇
int foodx, foody;           //食物的坐标
int len;                    //蛇的长度
int difficult;              //定义难度

int main() {
	system("color F1");
	HideCursor();
	Welcome();

	return 0;
}

void Display(int x, int y)
{
	COORD t;
	HANDLE hOutput;
	t.X = x;
	t.Y = y;
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOutput, t);
	printf("■");
}

void DisplayFood(int x, int y)
{
	COORD t;
	HANDLE hOutput;
	t.X = x;
	t.Y = y;
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOutput, t);
	printf("○");
}

void Welcome()
{
	system("mode con cols=53 lines=20");
	difficult = 1000;
	for (int i = 5; i > 0; i--) {
		system("cls");
		printf("*****************************************************\n\n\n\n\n\n\n\n");
		printf("*                  游戏将于%ds后开始                 *\n", i);
		printf("*           可以按数字1—3获得你想要的难度          *\n");
		printf("*                    使用wasd控制                   *\n\n\n\n\n\n\n\n");
		printf("*****************************************************\n");
		Sleep(1000);
		while (_kbhit()) {
			int ch = _getch();
			if (ch == 49)
				difficult = 1000;
			else if (ch == 50)
				difficult = 750;
			else if (ch == 51)
				difficult = 500;
			else
			{
				system("cls");
				printf("输入有误,重新开始");
				Sleep(2000);
				Welcome();
			}
		}
	}
	Start();
}

void Start()
{
	system("mode con cols=44 lines=23");
	//初始化蛇
	s[0].x = 5;
	s[0].y = 3;
	direction = 'd';
	len = 1;
	Produce();

	while (GameOver() && len < 361)
	{
		system("cls");

		printf("您的成绩为%d", len);

		//生成边框
		for (int i = 1; i <= 39; i += 2)
			Display(i, 1);
		for (int i = 1; i <= 39; i += 2)
			Display(i, 21);
		for (int i = 1; i <= 21; i++)
			Display(1, i);
		for (int i = 1; i <= 21; i++)
			Display(39, i);

		Eat();

		//生成蛇
		for (int i = 0; i < len; i++) {
			Display(s[i].x, s[i].y);
		}

		//重新生成食物
		DisplayFood(foodx, foody);

		Sleep(difficult);

		//捕获键盘输入
		while (_kbhit())
		{
			int	ch;
			ch = _getch();

			if (ch == 119 && direction != 's')
				direction = 'w';
			if (ch == 97 && direction != 'd')
				direction = 'a';
			if (ch == 115 && direction != 'w')
				direction = 's';
			if (ch == 100 && direction != 'a')
				direction = 'd';
		}

		if (direction == 'w')
			Up();
		if (direction == 'a')
			Left();
		if (direction == 's')
			Down();
		if (direction == 'd')
			Right();
	}
	if (len < 361)
		Finish();
	else
		Over();
}

void Up()
{
	s[361] = s[len - 1];
	for (int i = len; i >1; i--) {
		s[i - 1] = s[i - 2];
	}
	s[0].y = s[0].y - 1;
}

void Down()
{
	s[361] = s[len - 1];
	for (int i = len; i > 1; i--) {
		s[i - 1] = s[i - 2];
	}
	s[0].y = s[0].y + 1;
}

void Left()
{
	s[361] = s[len - 1];
	for (int i = len; i > 1; i--) {
		s[i - 1] = s[i - 2];
	}
	s[0].x = s[0].x - 2;
}

void Right()
{
	s[361] = s[len - 1];
	for (int i = len; i > 1; i--) {
		s[i - 1] = s[i - 2];
	}
	s[0].x = s[0].x + 2;
}

int GameOver()
{
	if (len < 5) {
		if (s[0].x == 1 || s[0].x == 39 || s[0].y == 1 || s[0].y == 21)
			return 0;
		else
			return 1;
	}
	else {
		for (int i = 4; i <= len; i++) {
			if ((s[0].x == s[i].x) && (s[0].y == s[i].y) || s[0].x == 1 || s[0].x == 39 || s[0].y == 1 || s[0].y == 21)
				return 0;
			else
				return 1;
		}
	}
}

void Produce()
{
	srand(time(0));
	foodx = (rand() % 18) * 2 + 3;
	foody = rand() % 19 + 2;
}

void Eat()
{
	if (s[0].x == foodx && s[0].y == foody)
	{
		len++;
		Produce();
		s[len - 1] = s[361];
	}
}

void Finish()
{
	system("mode con cols=53 lines=21");
	for (int i = 5; i >= 1; i--) {
		system("cls");
		printf("*****************************************************\n\n\n\n\n\n\n\n");
		printf("*                      游戏结束                    *\n");
		printf("*                    您的成绩为%d                   *\n", len);
		printf("*                  游戏将于%ds后退出                *\n", i);
		printf("*                 或者按F键重新开始                *\n\n\n\n\n\n\n\n");
		printf("*****************************************************\n");
		Sleep(1000);
		while (_kbhit()) {
			int ch = _getch();
			if (ch == 102)
				Welcome();
		}
	}
}

void Over()
{
	system("cls");
	printf("*****************************************************\n\n\n\n\n\n\n\n");
	printf("*                       恭喜您                    *\n");
	printf("*                    您已达到最大长度                   *\n\n\n\n\n\n\n\n");
	printf("*****************************************************\n");
	Sleep(5000);
	system("cls");
	printf("*****************************************************\n\n\n\n\n\n\n\n");
	printf("*                   按任意键退出                   *\n\n\n\n\n\n\n\n");
	printf("*****************************************************\n");
	system("exit");
}

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor;
	cursor.bVisible = FALSE;
	cursor.dwSize = sizeof(cursor);
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorInfo(handle, &cursor);
}

程序截图:
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

有一点忘了,就是生成食物的时候没有比较食物是否在蛇身上,如果在就重新生成食物,加上这一点就可以了。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值