C++贪吃蛇小游戏(源码+思路)

实训作业
代码自己写的,思路老师给的。

开发环境:VS2015 Win32控制台应用程序

思路如下:
1、结构体:行列
SData
{
int nRow;
int nCol;
}
2、定义贪吃蛇:数组
SData arrSnake[15] ={};
//蛇头。蛇头坐标初始化。
arrSnake[0] = SData(1,1);
当前蛇的长度:int nLength = 1;
3、蛇的移动。自己动
定义一个蛇的移动方向 nDir = E_DIR_NONE

//按键之后更改方向
if(down)
{
nDir = E_DIR_DOWN;
}
//蛇头移动之前要备份坐标。
//蛇头根据方向动
if(nDir == E_DIR_DOWN)
{
蛇头坐标更改。
}

4、食物,(循环外面创建)0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
比如地图15,20 1~13 行随机 0~12 + 1 rand():0~65536 执行一次就会取得当中的一个随机值。 rand()%13 + 1 1~13 0~12
l列:118随机。118 1+ 0~17 rand()%18 +1
食物的行:
SData sFood = SData( rand()%13 + 1,rand()%18 +1)//随机。

5、吃食物:只需要判断蛇头的坐标是否跟食物的坐标相等就可以。
相等:
身体变长:nLength++;
食物再随机坐标:sFood = SData( rand()%13 + 1,rand()%18 +1)

6.跟随(现有长度的范围内身体跟着蛇头动。)

a.备份当当前这一节坐标
b.后一节跟着前一节动。
for(int i = 1;i < nLength;i++)
{
//备份当前这一节坐标
arrSnake[i].nRowBk = arrSnake[i].nRow;
//当前这一节跟着前一节动
arrSnake[i].nRow = arrSnake[i-1].nRowBK;

}

随便按↑↓←→后即开始游戏,吃满15个食物即过关
源码如下:

// Snake.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include
#include
#define KEY_DOWN(vk_code) GetAsyncKeyState(vk_code) & 0x8000 ? 1 : 0  //用于判断按键输入
using namespace std;

//void JudgeHead();
//void MakeHeadMove();

  
//bool Key_Up = false, Key_Down = false, Key_Left = false, Key_Right = false;
bool bNewGame = false;  //判断游戏结束条件一
bool bNewGame2 = false;  //判断游戏结束条件二
bool bNewGame3 = false;  //判断游戏胜利状态

//按键选择范围
enum {
	K_Up,
	K_Down,
	K_Left,
	K_Right,
	K_None
};
int KeyState = K_None;  //选择按键
int nBodyLength = 1;  //蛇身长度
int Map[15][20] = {
	{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
	{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
};

//定义蛇头
struct sDate {
	int s_x;
	int s_y;
	int s_xx;
	int s_yy;
	sDate() {
		s_x = 0;
		s_y = 0;
		s_xx = 0;
		s_yy = 0;
	}
	sDate(int a,int b) {
		s_x = a;
		s_y = b;
		s_xx = a;
		s_yy = b;
	}
	void BackUp() {
		s_xx = s_x;
		s_yy = s_y;
	}
	void Restore() {  //可不要
		s_x = s_xx;
		s_y = s_yy;
	}
};
sDate shead[15] = {};//初始化蛇头位置为(2,3)
sDate sfood = sDate(rand()%13+1,rand()%17+1);//随机食物

//打印地图
void Print() {
	//nBodyLength = sizeof(shead) / sizeof(sDate);
	while (1) {
		Sleep(50);  //设置刷新速度(蛇速度),数值越大刷新越慢。
		
		for (int i = 0; i < nBodyLength; i++) {  //按键判断
			shead[i].BackUp();
		}
		if (KEY_DOWN(VK_UP)) {
			KeyState = K_Up;
		}
		else if (KEY_DOWN(VK_DOWN)) {
			KeyState = K_Down;
		}
		else if (KEY_DOWN(VK_LEFT)) {
			KeyState = K_Left;
		}
		else if (KEY_DOWN(VK_RIGHT)) {
			KeyState = K_Right;
		}
		switch (KeyState) {
		case 0: --shead[0].s_x; break;
		case 1: ++shead[0].s_x; break;
		case 2: --shead[0].s_y; break;
		case 3: ++shead[0].s_y; break;
		}
		for (int m = 1; m < nBodyLength; m++) { //判断蛇头与蛇身相撞
			if (shead[0].s_x == shead[m].s_x && shead[0].s_y == shead[m].s_y) {
				bNewGame2 = true;
				break;
			}
		} 
		if (shead[0].s_x == sfood.s_x && shead[0].s_y == sfood.s_y) {  //长身体
			sfood = sDate(rand() % 13 + 1, rand() % 17 + 1);
			++nBodyLength;
		}
		//蛇身体跟随
		for (int i = 1; i < nBodyLength; i++) {
			shead[i].s_x = shead[i - 1].s_xx;
			shead[i].s_y = shead[i - 1].s_yy;
		}
		system("cls");
		for (int i = 0; i < 15; i++)   //打印地图
		{
			for (int j = 0; j < 20; j++) {
				bool bDrawBody = false;
				for (int m = 0; m < nBodyLength; m++) {
					if (i == shead[m].s_x && j == shead[m].s_y) {
						bDrawBody = true;
						break;
					}	
				}
				if (1 == Map[i][j]) {
					cout << "■";
				}
				else if (bDrawBody) {
					cout << "¤";
				}
				else if (i == sfood.s_x && j == sfood.s_y) {
					cout << "⊙";
				}
				else {
					cout << "  ";
				}
			}
			cout << endl;
		}
		
		//暂停初始化后的游戏界面
		if (bNewGame || bNewGame3) {
			system("pause");
			bNewGame = false;
			bNewGame3 = false;
		}

		//游戏胜利处理
		if (15 == nBodyLength) {
			bNewGame3 = true;
			cout << "~@胜利@~" << endl;
			shead[0].s_x = 7;
			shead[0].s_y = 7;
			nBodyLength = 1;
			sfood = sDate(rand() % 13 + 1, rand() % 17 + 1);
			system("pause");
		}
		
		//游戏失败处理
		if (1 == Map[shead[0].s_x][shead[0].s_y] || bNewGame2) {
			cout << "失败,游戏结束!" << endl;
			bNewGame = true;
			bNewGame2 = false;
			shead[0].s_x = 7;
			shead[0].s_y = 7;
			nBodyLength = 1;
			sfood = sDate(rand() % 13 + 1, rand() % 17 + 1);
			system("pause");
			break;
		}
	}
}
//
//// 判断蛇头移动方法二
//void JudgeHead() {
//	if (KEY_DOWN(VK_UP)) {
//		--shead[0].s_x;
//		Key_Up = true;
//		Key_Down = false;
//		Key_Left = false;
//		Key_Right = false;
//	}
//	if (KEY_DOWN(VK_DOWN)) {
//		++shead[0].s_x;
//		Key_Down = true;
//		Key_Up = false;
//		Key_Left = false;
//		Key_Right = false;
//	}
//	if (KEY_DOWN(VK_LEFT)) {
//		--shead[0].s_y;
//		Key_Left = true;
//		Key_Up = false;
//		Key_Down = false;
//		Key_Right = false;
//	}
//	if (KEY_DOWN(VK_RIGHT)) {
//		++shead[0].s_y;
//		Key_Right = true;
//		Key_Up = false;
//		Key_Down = false;
//		Key_Left = false;
//	}
//}
//void MakeHeadMove() {
//	if (Key_Up == true) {
//		--shead[0].s_x;	
//	}else
//	if (Key_Down == true) {
//		++shead[0].s_x;
//	}else
//	if (Key_Left == true) {
//		--shead[0].s_y;
//	}else
//	if (Key_Right == true) {
//		++shead[0].s_y;
//	}
//}

int main()
{
	shead[0] = sDate(7, 7);
	while (1) {
		Print();
	}
    return 0;
}


游戏画面:
开始界面
C++贪吃蛇小游戏(源码+思路)_第1张图片
撞墙失败界面
C++贪吃蛇小游戏(源码+思路)_第2张图片
胜利界面
C++贪吃蛇小游戏(源码+思路)_第3张图片

你可能感兴趣的:(C/C++小游戏)