c++ 贪吃蛇小游戏

本文介绍了一个使用C++和Windows API实现的简单贪吃蛇游戏。游戏包含基本的游戏逻辑,如移动、得分和边界检测等,并通过键盘输入控制蛇的方向。此外,还展示了如何利用Windows API进行屏幕绘制和控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "iostream"
#include "stdlib.h"
#include "iomanip"
#include "windows.h"
#include "conio.h"   //有用于键盘输入字符而不用回车的函数
#include "ctime"     //调用time()
using namespace std;
HANDLE hConsole;   //句柄
int X = 10;
int INI = 35;
int &x = INI, &y = X;
int u = 0;
int v = 0;
int &U = u;
int &V = v;
int len=0;
int &length = len;
int sc = 0;
int &score = sc;
char a[2];
int flag_i;
void gotoxy(int x, int y)
{
	COORD coord;
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(hConsole, coord);
}
void print_screen()
{
	
		const int n = 5;
		const int m = 20;
		hConsole = GetStdHandle(STD_OUTPUT_HANDLE);   //句柄实例化
		SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);//设置API字体的颜色
		cout << endl;
		cout << endl;
		cout << "                -----------------欢迎来到贪吃蛇新世界----------------" << endl;
		cout << "                ----------------------开始你的表演--------------------" << endl;
		gotoxy(25, 28);
		cout << "--------w:上   s:下   a:左   d:右---------";
		for (int i = 0; i < 20; i++)
		{
			gotoxy(n + m, n + i);
			cout << "* ";
		}
		//cout << endl;
		for (int i = 0; i < 20; i++)
		{
			gotoxy(n + m + 2 * i, n);
			cout << "*";
		}
		for (int i = 0; i < 20; i++)
		{
			gotoxy(n + m + 2 * i, n + 20);
			cout << "* ";
		}
		for (int i = 0; i < 21; i++)
		{
			gotoxy(n + m + 2 * 20, n + i);
			cout << "*" << endl;
		}
		gotoxy(68,15);
		cout << "your are score:" << score;
		

	
}
/*void move()
{
	for (int i = 0; i < 20; i++)
	{
		gotoxy(25 + i, 5 + i);
		cout << "●";
		Sleep(500);
		system("cls");//清屏
		print_screen();//清屏之后继续将其他信息显示出来
	}
}*/

void Up(int &A,int &B)
{
	if (a[(flag_i + 1) % 2] == 'd')
		{
			A=A+length;
		}
		for (int i = 0;  ; i++)
		{
			if (!_kbhit())
			{
				gotoxy(U, V);
				cout << "$";
				gotoxy(A, B );
				cout << "@";
				for (int j = 0; j < length + 1; j++)
				{
					gotoxy(A, B +1+ j);
					cout << "。";
				}
				Sleep(500-5*i);
				system("cls");
				print_screen();
				if ((A == U) && (B == V))
				{
					srand((unsigned int)time(NULL));//
					U = 26+rand() % (64-26);
					V = 6+rand() % (24-6);
					gotoxy(U, V);
					cout << "$";
					length++;
					score = length * 10;
				}
				B--;
				if (B < 6||A<25||A>65||B>25)
				{
					gotoxy(35, 15);
					cout << "you are failed.....";
					A = A + 65;
					B = B + 60;
					break;
				}
				if (_kbhit())
				{
					B++;
					break;
				}
			}
			else
			{
				break;
			}
		
		}
		
		
}
void Down(int &A,int &B)
{
	if (a[(flag_i + 1) % 2] == 'd')
		{
			A=A+ length;
		}
	for (int i = 0; ; i++)
	{
		if (!_kbhit()) //判断键盘是否有输入
		{
			gotoxy(U, V);
			cout << "$";
			for (int j = 0; j < length + 1; j++)
			{
				gotoxy(A, B -1-j);
				cout << "。";
			}
			gotoxy(A, B );
			cout << "@";
			Sleep(500-5*i);
			system("cls");
			print_screen();
			if ((A == U) && (B == V))
			{
				srand((unsigned int)time(NULL));
				U = 26+rand() % (64-27);
				V = 6+rand() % (24-6);
				gotoxy(U, V);
				cout << "$";
				length++;
				score = length * 10;
			}
			B++;
			if (B < 6 || A<25 || A>65 || B >25)
			{
				gotoxy(35, 15);
				cout << "you are failed.....";
				A = A + 65;
				B = B + 60;
				break;
			}
			if (_kbhit())
			{
				B--;
				break;
			}
		}
		else
		{
			break;
		}
		
	}
	
}
void Left(int &A,int &B)
{
	 if (a[(flag_i + 1) % 2] == 'd')
		{
			A=A+length;
		}
	for (int i = 0; ; i++)
	{
		if (!_kbhit()) //判断键盘是否有输入
		{
			gotoxy(U, V);
			cout << "$";
			gotoxy(A, B);
			cout << "@";
			for (int j = 0; j < length + 1; j++)
			{
				//gotoxy(A + 1 + j, B);
				cout << "。";
			}
			Sleep(500 - 5 * i);
			system("cls");
			print_screen();
			if ((A == U) && (B == V))
			{
				srand((unsigned int)time(NULL));
				U = 26+rand() % (64-26);
				V = 6+rand() % (24-6);
				gotoxy(U, V);
				cout << "$";
				length++;
				score = length * 10;
				
			}
				A--;
				if (B < 6 || A < 25 || A>65 || B > 25)
				{
					gotoxy(35, 15);
					cout << "you are failed.....";
					A = A + 65;
					B = B + 60;
					break;
				}
				if (_kbhit())
				{
					A++;
					break;
				}
			

		
		}
		else
		{
			break;
		}
		
	}
	
}
void Right(int &A,int &B)
{
	if (a[(flag_i + 1) % 2] == 'w')
	{
		A = A - length;
	}
	else if (a[(flag_i + 1) % 2] == 's')
	{
		A=A-length;
	}
	else if (a[(flag_i + 1) % 2] == 'a')
	{
		A=A-length;
	}
	for (int i = 0; ; i++)
	{
		if (!_kbhit()) //判断键盘是否有输入
		{
			gotoxy(U, V);
			cout << "$";
			gotoxy(A - length-2, B);
			for (int j=0; j < length + 1; j++)
			{
				cout << "。";
			}
			cout << "@";
			Sleep(500-5*i);
			system("cls");
			print_screen();
			if ((A ==U)&&(B == V))
			{
				srand((unsigned int)time(NULL));
				U = 26+rand() % (64-26);
				V = 6+rand() % (24-6);
				gotoxy(U, V);
				cout << "$";
				length++;
				score = length * 10;
			}
				A++;
				if (B < 6 || A < 25 || A>62 || B > 25)
				{
					gotoxy(35, 15);
					cout << "you are failed.....";
					A = A + 65;
					B = B + 60;
					break;
				}
				if (_kbhit())
				{
					A--;
					break;
				}
		
		}
		else
		{
			break;
		}
		
	}
}
void select (char a)
{
	switch (a)
	{
	case 'w':	  	  Up(x,y);
		break;
	case 's':         Down(x,y);
		break;
	case 'a':         Left(x,y);
		break;
	case 'd':         Right(x,y);
		break;
	default: a='w';
		break;
	}
}


int main()
{
	//snake s;
	
	print_screen();
	srand((unsigned int)time(NULL));
	U = 26+rand() % (64-26);
	V = 6+rand() % (24-6);
 	Left(x,y);
	//cout << "x:" << x << "  y:" << y << endl;
	int flag = 0;
	char temp;
	for (int j = 0;;)
	{
		flag_i = flag % 2;
		temp=_getch();
		a[flag_i] = temp;
		flag++;
		select(a[flag_i]);
	
	}
	system("pause");
	return 0;
}
渣渣花了好久,但是写出来了心情还是很好的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值