贪吃蛇游戏

C语言实现经典游戏贪吃蛇
吃到果实蛇的长度会变大,如果碰到墙壁或者果实就会gameover
windows平台下实现

代码如下:
gmae.h:

#pragma once
#pragma once
#include 
#include 
#include 
#include 

void HideCursor();
void gotoxy(int x, int y);
void DataInit();
void Show();
void UpdateWithoutInput();
void UpdateWithInput();
void MoveSnakeByDirection();

#define HIGH 20
#define WIDTH 30

test.cpp:

#include "game.h"

int canvas[HIGH][WIDTH] = { 0 };
int score;
int MoveDirection;
int food_x, food_y;

int main()
{
	HideCursor();   //隐藏光标
	DataInit();     //数据初始化
	while (1)
	{
		Show();         //显示画面
		UpdateWithoutInput();     //与用户无关的数据更新
		UpdateWithInput();        //与用户有关的数据更新
	}
	return 0;
}

game.cpp:

#include "game.h"

extern int canvas[HIGH][WIDTH];
extern int score;
extern int MoveDirection;
extern int food_x, food_y;

void DataInit()
{
	int i = 0, j = 0;

	for (i = 0; i < HIGH; i++)//初始化边框
	{
		for (j = 0; j < WIDTH; j++)
		{
			canvas[i][j] = 0;
			canvas[0][j] = -1;
			canvas[HIGH - 1][j] = -1;
			canvas[i][0] = -1;
			canvas[i][WIDTH - 1] = -1;
		}
	}

	canvas[HIGH / 2][WIDTH / 2] = 1;//初始化蛇头

	for (i = 1; i <= 4; i++)//初始化蛇身
	{
		canvas[HIGH / 2][WIDTH / 2 - i] = i + 1;
	}

	MoveDirection = 4;

	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] == -1)
			{
				printf("#");
			}
			else 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] == -2)
			{
				printf("F");
			}
		}
		printf("\n");
	}
	Sleep(100);
}

void UpdateWithoutInput()
{
	MoveSnakeByDirection();
}

void UpdateWithInput()
{
	char input;

	if (_kbhit())
	{
		input = _getch();
		if (input == 'w')
		{
			MoveDirection = 1;
			MoveSnakeByDirection();
		}
		if (input == 's')
		{
			MoveDirection = 2;
			MoveSnakeByDirection();
		}
		if (input == 'a')
		{
			MoveDirection = 3;
			MoveSnakeByDirection();
		}
		if (input == 'd')
		{
			MoveDirection = 4;
			MoveSnakeByDirection();
		}
	}
}

void MoveSnakeByDirection()
{
	int i = 0, j = 0;

	for (i = 1; i < HIGH - 1; i++)
	{
		for (j = 0; j < WIDTH - 1; j++)
		{
			if (canvas[i][j] >= 1)
			{
				canvas[i][j]++;
			}
		}
	}

	int OldTail_i, OldTail_j, OldHead_i, OldHead_j;
	int max = 0;

	for (i = 1; i < HIGH - 1; i++)
	{
		for (j = 0; j < WIDTH - 1; j++)
		{
			if (max < canvas[i][j])
			{
				max = canvas[i][j];
				OldTail_i = i;
				OldTail_j = j;
			}

			if (canvas[i][j] == 2)
			{
				OldHead_i = i;
				OldHead_j = j;
			}
		}
	}

	int newHead_i = 0,newHead_j = 0;
	if (MoveDirection == 1)
	{
		newHead_i = OldHead_i - 1;
		newHead_j = OldHead_j;
	}
	if (MoveDirection == 2)
	{
		newHead_i = OldHead_i + 1;
		newHead_j = OldHead_j;
	}
	if (MoveDirection == 3)
	{
		newHead_i = OldHead_i;
		newHead_j = OldHead_j - 1;
	}
	if (MoveDirection == 4)
	{
		newHead_i = OldHead_i;
		newHead_j = OldHead_j + 1;
	}

	if (canvas[newHead_i][newHead_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[OldTail_i][OldTail_j] = 0;
	}

	if (canvas[newHead_i][newHead_j] > 0 || canvas[newHead_i][newHead_j] == -1)
	{
		printf("WASTED!!!\n");
		Sleep(2000);
		system("pause");
		exit(0);
	}
	else
	{
		canvas[newHead_i][newHead_j] = 1;
	}

}

fix.cpp:
主要用来修复闪屏和隐藏光标,可以用cls来代替

#include "game.h"
void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}

你可能感兴趣的:(c,游戏,c语言,c++)