C++ VS2013 贪吃蛇小游戏

本文介绍了一个使用C++编写的控制台版贪吃蛇游戏,通过Windows API实现了字符界面的移动与交互。游戏包括蛇身的移动、食物的随机生成以及碰撞检测等功能。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#define N 21
void gotoxy(int x, int y)//位置函数
{
	COORD pos;
	pos.X = 2 * x;
	pos.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void color(int a)//颜色函数
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}
void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果)
{
	int i, j;//初始化围墙
	int wall[N + 2][N + 2] = { { 0 } };
	for (i = 1; i <= N; i++)
	{
		for (j = 1; j <= N; j++)
			wall[i][j] = 1;
	}
	color(11);
	for (i = 0; i<N + 2; i++)
	{
		for (j = 0; j<N + 2; j++)
		{
			if (wall[i][j])
				std::cout << "■";
			else std::cout << "□";
		}
		std::cout << std::endl;
	}
	gotoxy(N + 3, 1);//显示信息
	color(20);
	std::cout << "按 W S A D 移动方向" << std::endl;
	gotoxy(N + 3, 2);
	color(20);
	std::cout << "按任意键暂停" << std::endl;
	gotoxy(N + 3, 3);
	color(20);
	std::cout << "得分:" << std::endl;
	apple[0] = rand() % N + 1;//苹果
	apple[1] = rand() % N + 1;
	gotoxy(apple[0], apple[1]);
	color(12);
	std::cout << "●" << std::endl;
}
int main()
{
	int i, j;
	int** snake = NULL;
	int apple[2];
	int score = 0;
	int tail[2];
	int len = 3;
	char ch = 'p';
	srand((unsigned)time(NULL));
	init(apple);
	snake = (int**)realloc(snake, sizeof(int*)*len);
	for (i = 0; i<len; i++)
		snake[i] = (int*)malloc(sizeof(int)* 2);
	for (i = 0; i<len; i++)
	{
		snake[i][0] = N / 2;
		snake[i][1] = N / 2 + i;
		gotoxy(snake[i][0], snake[i][1]);
		color(14);
		std::cout << "★" << std::endl;
	}
	while (1)//进入消息循环
	{
		tail[0] = snake[len - 1][0];
		tail[1] = snake[len - 1][1];
		gotoxy(tail[0], tail[1]);
		color(11);
		std::cout << "■" << std::endl;
		for (i = len - 1; i>0; i--)
		{
			snake[i][0] = snake[i - 1][0];
			snake[i][1] = snake[i - 1][1];
			gotoxy(snake[i][0], snake[i][1]);
			color(14);
			std::cout << "★" << std::endl;
		}
		if (_kbhit())
		{
			gotoxy(0, N + 2);
			ch = _getche();
		}
		switch (ch)
		{
		case 'w':snake[0][1]--; break;
		case 's':snake[0][1]++; break;
		case 'a':snake[0][0]--; break;
		case 'd':snake[0][0]++; break;
		default: break;
		}
		gotoxy(snake[0][0], snake[0][1]);
		color(14);
		std::cout << "★" << std::endl;
		Sleep(abs(200 - 0.5*score));
		if (snake[0][0] == apple[0] && snake[0][1] == apple[1])//吃掉苹果后蛇分数加1,蛇长加1
		{
			score++;
			len++;
			snake = (int**)realloc(snake, sizeof(int*)*len);
			snake[len - 1] = (int*)malloc(sizeof(int)* 2);
			apple[0] = rand() % N + 1;
			apple[1] = rand() % N + 1;
			gotoxy(apple[0], apple[1]);
			color(12);
			std::cout << "●" << std::endl;
			gotoxy(N + 5, 3);
			color(20);
			std::cout << score << std::endl;
		}
		if (snake[0][1] == 0 || snake[0][1] == N || snake[0][0] == 0 || snake[0][0] == N)//撞到围墙后失败
		{
			gotoxy(N / 2, N / 2);
			color(30);
			std::cout << "失败!!!" << std::endl;
			for (i = 0; i<len; i++)
				free(snake[i]);
			Sleep(INFINITE);
			exit(0);
		}
	}
	return 0;
}


代码转载自:https://pan.quark.cn/s/8ce4326d996e 对于在 CentOS 7 系统中修改网卡配置文件后无法使设置生效的情况,经过实践验证,可以通过使用 nmcli 命令来进行调整。完成修改之后,需要重新启动虚拟机以使更改生效,这样操作流程即告完成。如果设置仍然无法生效,则表明虚拟机在启动过程中所获取的 IP 地址配置并非针对 eth0,此时可以对其它网卡的配置文件进行修改或将其移除。在 CentOS 7 系统中,网络配置的管理机制与早期版本存在差异,主要体现为采用了 Network Manager 服务来负责网络接口的管理。在某些情形下,尽管修改了 `/etc/sysconfig/network-scripts` 目录下的 `ifcfg-eth0` 文件,但网络配置却未能即时生效。此类问题的发生通常源于 CentOS 7 采用了不同于以往的配置读取方法。接下来将具体阐述如何借助 nmcli 命令来处理这一挑战。 以 root 用户身份登录系统并打开终端界面。nmcli 是 Network Manager 提供的命令行界面工具,它支持在命令行环境下执行网络连接的建立、编辑、查询及管理任务。针对修改 eth0 网卡配置的需求,可以遵循以下步骤进行操作: 1. 导航至 `/etc/sysconfig/network-scripts` 目录: ``` cd /etc/sysconfig/network-scripts ``` 2. 检查该目录内是否存在 `ifcfg-eth0.bak` 文件,该备份文件可能是先前调整配置时遗留下来的,若存在可能造成冲突。若发现该文件,可以选择将其删除: ``` [root@localhost netw...
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值