C语言贪吃蛇小游戏

24 篇文章 2 订阅
4 篇文章 0 订阅
// snake.c -- 利用system()函数清除重绘技术完成蛇的移动
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#define WIDTH 40 
#define HEIGHT 20

// 功能函数
void init(void);	// 初始化
void printMap(void); // 打印地图
void help(void);	// 显示帮助信息
void moveTop(void);	// 蛇向上移动
void moveDown(void);	// 蛇向下移动
void moveLeft(void);	// 蛇向左移动
void moveRight(void);	// 蛇向右移动
void food(void);	// 生成食物
void gameOver(void);	// 游戏结束

// 全局变量
char map[HEIGHT][WIDTH];	// height表示高,width表示宽
int snakex[WIDTH];	// 表示蛇的x坐标
int snakey[HEIGHT];	// 表示蛇的y坐标
int snakeLen;	// 蛇的长度
int fd[1][2];	// 食物
int score;	// 得分

int main(void){
	char direction = 'd', ch;	// 初始化蛇向右移动
	long sleep;	// 让程序等待一段时间后再执行
	init();
	help();
	system("pause");
	srand((unsigned)time(NULL));
	while(1)
	{
		if(kbhit())
		{	
			ch = getch();
			if(ch == 27)
			{
				break;
			}
			else if(ch == ' ')
			{
				getch();
			}
			else if((ch == 'w' || ch == 'W') || (ch == 's' || ch == 'S')
					|| (ch == 'a' || ch == 'A') || (ch == 'd' || ch == 'D'))
			{
				ch = tolower(ch);
				if(!(ch + direction == 234 || ch + direction == 197))
				{
					direction = ch;
				}
			}
		}
		switch(direction)
		{
			case 'w':
				moveTop();
				break;
			case 's':
				moveDown();
				break;
			case 'a':
				moveLeft();
				break;
			case 'd':
				moveRight();
		}
		system("cls");
		printf("score: %d\n", score);
		printMap();
		sleep = clock();
		while(clock() - sleep < 200)
			continue;
	}
	return 0;
}

void init(void){
	int i, j;
	// 初始化地图,将地图用空格填充
	for(i = 0; i < HEIGHT; i++){
		for(j = 0; j < WIDTH; j++){
			if(i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1){
				map[i][j] = '*';
			}else{
				map[i][j] = ' ';
			}
		}
	}
	snakeLen = 5;	// 初始化蛇长度
	// 初始化蛇坐标
	for(i = 1; i <= snakeLen; i++)
	{
		snakex[i - 1] = snakeLen - i + 1;
	}
	for(i = 1; i <= snakeLen; i++)
	{
		snakey[i - 1] = 1;
	}
	// 生成食物
	food();
}

void printMap(void){
	int i, j;
	map[snakey[0]][snakex[0]] = '+';	// '+'代表蛇头
	for(i = 1; i < snakeLen; i++)
	{
		map[snakey[i]][snakex[i]] = 'o';	// 'o'代表蛇身
	}
	for(i = 0; i < HEIGHT; i++){
		for(j = 0; j < WIDTH; j++){
			putchar(map[i][j]);
		}
		putchar('\n');
	}
}

void help(void)
{
	puts("Enter (w,a,s,d) to move snake,\nspace to stop and esc to quit.");
}

void moveTop(void)
{
	int i;
	// 如果下一个位置是蛇身或者墙壁,游戏结束
	if(map[snakey[0] - 1][snakex[0]] == 'o' ||
	map[snakey[0] - 1][snakex[0]] == '*')
	{
		gameOver();
	}
	// 如果下一个位置是食物,蛇长度加1,否则删除蛇尾
	else if(map[snakey[0] - 1][snakex[0]] == '$')
	{
		snakeLen++;
		score++;
		food();
	}
	else
	{
		map[snakey[snakeLen - 1]][snakex[snakeLen - 1]] = ' ';
	}

	// 更新蛇坐标
	for(i = snakeLen - 2; i >= 0; i--)
	{
		snakex[i + 1] = snakex[i];
		snakey[i + 1] = snakey[i];
	}
	snakey[0]--;
}

void moveDown(void)
{
	int i;
		// 如果下一个位置是蛇身或者墙壁,游戏结束
	if(map[snakey[0] + 1][snakex[0]] == 'o' ||
	map[snakey[0] + 1][snakex[0]] == '*')
	{
		gameOver();
	}// 如果下一个位置是食物,蛇长度加1,否则删除蛇尾
	if(map[snakey[0] + 1][snakex[0]] == '$')
	{
		snakeLen++;
		score++;
		food();
	}
	else
	{
		map[snakey[snakeLen - 1]][snakex[snakeLen - 1]] = ' ';
	}

	// 更新蛇坐标
	for(i = snakeLen - 2; i >= 0; i--)
	{
		snakex[i + 1] = snakex[i];
		snakey[i + 1] = snakey[i];
	}
	snakey[0]++;
}

 void moveLeft(void)
{
 	int i;
		// 如果下一个位置是蛇身或者墙壁,游戏结束
	if(map[snakey[0]][snakex[0] - 1] == 'o' ||
	map[snakey[0]][snakex[0] - 1] == '*')
	{
		gameOver();
	}
	// 如果下一个位置是食物,蛇长度加1,否则删除蛇尾
	if(map[snakey[0]][snakex[0] - 1] == '$')
	{
		snakeLen++;
		score++;
		food();
	}
	else
	{
		map[snakey[snakeLen - 1]][snakex[snakeLen - 1]] = ' ';
	}

	// 更新蛇坐标
    for(i = snakeLen - 2; i >= 0; i--)
    {
    	snakex[i + 1] = snakex[i];
    	snakey[i + 1] = snakey[i];
    }
	snakex[0]--;
}

void moveRight(void)
{
	int i;
	// 如果下一个位置是蛇身或者墙壁,游戏结束
	if(map[snakey[0]][snakex[0] + 1] == 'o' ||
	map[snakey[0]][snakex[0] + 1] == '*')
	{
		gameOver();
	}
	// 如果下一个位置是食物,蛇长度加1,否则删除蛇尾
	else if(map[snakey[0]][snakex[0] + 1] == '$')
	{
		snakeLen++;
		score++;
		food();
	}
	else
	{
		map[snakey[snakeLen - 1]][snakex[snakeLen - 1]] = ' ';
	}
	// 更新蛇坐标
    for(i = snakeLen - 2; i >= 0; i--)	// 从倒数第二个下标开始依次往后赋值,避免从下标0依次往后赋值时数据被覆盖
    {
    	snakex[i + 1] = snakex[i];
    	snakey[i + 1] = snakey[i];
    }
	snakex[0]++;
}

void food(void)
{
	while(map[fd[0][0]][fd[0][1]] != ' ')
	{
		fd[0][0] = rand() % (HEIGHT - 2) + 1;
		fd[0][1] = rand() % (WIDTH - 2) + 1;
	}
	map[fd[0][0]][fd[0][1]] = '$';
}

void gameOver(void)
{
	puts("Game over!");
	system("pause");
	exit(0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程小老弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值