C/C++教程

C语言在二维数组基础上实现贪吃蛇

本文主要是介绍C语言在二维数组基础上实现贪吃蛇,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

欢迎在此基础上创新


#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
#define High  20
#define Width 30
int food_x, food_y;												    //食物位置
int canvas[High][Width] = { 0 };								    //游戏区域的大小			
int movedirection;  									            //小蛇的移动方向,1向上,2向下,3向左,4向右
void menu()                                                         //游戏菜单
{
	printf("************************\n");
	printf("*****  1.开始游戏  *****\n");
	printf("*****  0.退出游戏  *****\n");
	printf("*****[wsad控制方向]*****\n");
}


void gotoxy(int x, int y)										    //将光标移动到(x,y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD coord;
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(handle,coord);
}


void startup()													    //数据初始化
{
	int i = 0, j = 0;                                               //将数组初始化,以循环进行游戏 
	for(i = 0; i < High; i++)
	{
		for (j = 0; j < Width; j++)
		{
			canvas[i][j] = 0;
		}

	}

	for(i = 0; i < High; i++)                                       //设置边框
	{
		canvas[i][0] = -1;
		canvas[i][Width - 1] = -1;
	}
	for(i = 0; i < Width; i++)                                      //设置边框
	{
		canvas[0][i] = -1;
		canvas[High - 1][i] = -1;
	}
	canvas[High / 2][Width / 2] = 1;                                 //设置蛇头
	for(i = 1; i < 5; i++)                                          //设置蛇身
	{
		canvas[High / 2][Width / 2 - i] = i + 1;
	}
	movedirection = 1;                                               //初始蛇向上走
	srand((unsigned)time(NULL));                                     //随机数
	food_x = rand() % (High - 5) + 2;                                //设置食物
	food_y = rand() % (Width - 5) + 2;
	canvas[food_x][food_y] = -2;
}

void show()													    	 //显示画面
{
	gotoxy(0, 0);											         //光标移动到原点,以下重画清屏
	int i = 0, j = 0;
	for(i = 0; i < High; i++)
	{	for(j = 0; j < Width; j++)
		{
			if(canvas[i][j] == 0)
				printf(" ");
			else if(canvas[i][j] == -1)
				printf("#");                                          //输出边框 
			else if(canvas[i][j] == 1)
				printf("@");                                          //输出蛇头  
			else if(canvas[i][j] > 1)
				printf("*");                                          //输出蛇身  
			else if(canvas[i][j] == -2)
				printf("$");                                          //输出食物
		}
	printf("\n");
	}
Sleep(150);                                                           //可以更改游戏速度
}

void game()
{
	void updatewithoutinput();
	void updatewithinput();
	while (1)
	{
		show();												         //显示画面
		updatewithoutinput();								         //与用户输入无关的更新
		updatewithinput();									         //与用户输入有关的更新
	}
}

void movesnake()												     //蛇的移动
{
	int i = 0, j = 0;
	for(i = 1; i < High - 1; i++)
		for(j = 1; j < Width - 1; j++)
			if(canvas[i][j] > 0)
			   canvas[i][j]++;


	int oldT_i, oldT_j, oldH_i, oldH_j;
	int max = 0;

	for(i = 1; i < High - 1; i++)
		for(j = 1; j < Width - 1; j++)
			if(canvas[i][j] > 0)
			{
				if(max < canvas[i][j])
				{
					max = canvas[i][j];
					oldT_i = i;
					oldT_j = j;
				}
				if(canvas[i][j] == 2)
				{
					oldH_i = i;
					oldH_j = j;
				}
			}


	int newH_i = 0, newH_j = 0;
	if(movedirection == 1)                                          //蛇向上移  
	{
		newH_i = oldH_i - 1;
		newH_j = oldH_j;
	}
	if(movedirection == 2)                                          //蛇向下移
	{
		newH_i = oldH_i + 1;
		newH_j = oldH_j;
	}
	if(movedirection == 3)                                          //蛇向左移
	{
		newH_i = oldH_i;
		newH_j = oldH_j - 1;
	}
	if(movedirection == 4)                                          //蛇向右移
	{
		newH_i = oldH_i;
		newH_j = oldH_j + 1;
	}
	if(canvas[newH_i][newH_j] == -2)                                //重置食物
	{
		canvas[food_x][food_y] = 0;
		food_x = rand() % (High - 5) + 2;
		food_y = rand() % (Width - 5) + 2;
		canvas[food_x][food_y] = -2;
	}
	else
		canvas[oldT_i][oldT_j] = 0;

	if(canvas[newH_i][newH_j] > 0 || canvas[newH_i][newH_j] == -1)  //判断游戏是否结束
	{
		printf("游戏结束!\n");
		Sleep(1000);

		int a = 0;
		do														     //选择是否继续游戏
		{
			menu();												     //游戏菜单
			printf("请选择");
			scanf("%d", &a);
			switch (a)
			{
			case 1:
				system("cls");									     //清空屏幕		
				startup();										     //数据初始化
				game();
				break;
			case 0:
				printf("退出游戏");
				break;
			default:
				printf("选择错误,请重新选择");
				break;
			}
		} while (a);
		exit(0);
	}
	else
		canvas[newH_i][newH_j] = 1;

}



void updatewithoutinput()									         //与用户输入无关的更新
{
	movesnake();
}

void updatewithinput()										         //与用户输入有关的更新
{
	char input;

	if(_kbhit())
	{
		input = _getch();
		if(input == 'a'&&movedirection!=4)                                           //蛇向左移 
		{
			movedirection = 3;
			movesnake();
		}
		else if(input == 'd'&&movedirection != 3)                                      //蛇向右移
		{
			movedirection = 4;
			movesnake();
		}
		else if(input == 'w'&&movedirection != 2)                                      //蛇向上移
		{
			movedirection = 1;
			movesnake();
		}
		else if(input == 's'&&movedirection != 1)                                      //蛇向下移
		{
			movedirection = 2;
			movesnake();
		}
	}
}

int main()
{
	int a = 0;
	do
	{
		menu();												         //游戏菜单
		printf("请选择");
		scanf("%d", &a);
		switch (a)
		{
		case 1:
			startup();										         //数据初始化
			game();
			break;
		case 0:
			printf("退出游戏");
			break;
		default:
			printf("选择错误,请重新选择");
			break;
		}
	} while (a);

	return 0;
}

这篇关于C语言在二维数组基础上实现贪吃蛇的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!