c++ 贪吃蛇小游戏

#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;
}
渣渣花了好久,但是写出来了心情还是很好的。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C++贪吃游戏的代码示例: ```cpp #include <iostream> #include <conio.h> #include <windows.h> using namespace std; bool gameOver; const int width = 20; const int height = 20; int x, y, fruitX, fruitY, score; int tailX[100], tailY[100]; int nTail; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; eDirection dir; void Setup() { gameOver = false; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } void Draw() { system("cls"); for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) cout << "#"; if (i == y && j == x) cout << "O"; else if (i == fruitY && j == fruitX) cout << "F"; else { bool printTail = false; for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { cout << "o"; printTail = true; } } if (!printTail) cout << " "; } if (j == width - 1) cout << "#"; } cout << endl; } for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; cout << "Score:" << score << endl; } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = true; break; } } } void Logic() { int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } if (x >= width) x = 0; else if (x < 0) x = width - 1; if (y >= height) y = 0; else if (y < 0) y = height - 1; for (int i = 0; i < nTail; i++) { if (tailX[i] == x && tailY[i] == y) gameOver = true; } if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % width; fruitY = rand() % height; nTail++; } } int main() { Setup(); while (!gameOver) { Draw(); Input(); Logic(); Sleep(10); // 控制游戏速度 } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值