从0开始制作小游戏(二)

 

上期我们说到我已经做好了游戏的基本界面,现在,我将在这空荡荡的世界里添加一棵树

效果图

 (有的人还可以看到罕见的水上树呢!)

贴代码咯:

#include <windows.h>
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <ctime>
using namespace std; 
void SetColor(unsigned short ForeColor,unsigned short BackGroundColor)
{
	HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hCon,(ForeColor%16)|(BackGroundColor%16*16));
}
void gotoxy(int x, int y) {
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void Sky()//淡蓝色 
{
	SetColor(9,9);//第一个9 前景色 第二个9 背景色
	cout<<" "; 
}
void water()
{
	SetColor(1,1);
	cout<<" ";
}
void grass()
{
	SetColor(11,10);
	cout<<" ";
}
void treeye()//树叶 英语没学好TAT
{
	SetColor(10,11);
	cout<<" ";
 } 
void treegan()//同理,这是树干
{
 	SetColor(0,0);
 	cout<<" ";
 } 
void PaintColor()
{
	for(int i=0;i<30;i++)
	{
		for(int j=0;j<110;j++) Sky();
	}
	for(int i=30;i<35;i++)
	{
		for(int j=0;j<110;j++) grass();
	}
}void Cloud(int line)
{
	//srand((unsigned)time(NULL));
	int n=rand()%70;
	gotoxy(n,line);
	for(int i=0;i<8;i++)
	{
		SetColor(15,15);
		cout<<" ";
	}cout<<endl;
}
void SummonLake() 
{
	//srand((unsigned)time(NULL));
	int n=rand()%70;
	gotoxy(n,29);
	for(int i=0;i<14;i++)
	{
		for(int j=0;j<2;j++)water();
	}cout<<endl;
}
void Tree()
{
	//srand((unsigned)time(NULL));
	int n=rand()%70;
	gotoxy(n,28);
	treegan();
	gotoxy(n,27);
	treegan();
	gotoxy(n,26);
	treegan();
	gotoxy(n-3,25);
	for(int i=0;i<6;i++) treeye();
	gotoxy(n-1,24);
	for(int i=0;i<4;i++) treeye();
}
int main()
{
	system("mode con cols=110 lines=35");
	srand((unsigned)time(NULL));
    SetConsoleTitle("My Life");
    PaintColor();Tree();
    Cloud(3);
    
    SummonLake();
    while(1) ;
    return 0;
}

为了不影响游戏体验,烦请各位关掉自己心爱的输入法,否则在草地最下面会出现一个蓝蓝的东西!

或许我们可以多生成几棵树:

 我们在生成树的时候,同时也用了一种新算法避开了湖面,这样就不会看见小特性了。

好啦,贴代码:

#include <windows.h>
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <ctime>
using namespace std; 
int waters,watere;//湖泊开始位置 湖泊结束位置 
void SetColor(unsigned short ForeColor,unsigned short BackGroundColor)
{
	HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hCon,(ForeColor%16)|(BackGroundColor%16*16));
}
void gotoxy(int x, int y) {
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void Sky()//淡蓝色 
{
	SetColor(9,9);//第一个9 前景色 第二个9 背景色
	cout<<" "; 
}
void water()
{
	SetColor(1,1);
	cout<<" ";
}
void grass()
{
	SetColor(11,10);
	cout<<" ";
}
void treeye()//树叶 英语没学好TAT
{
	SetColor(10,11);
	cout<<" ";
 } 
void treegan()//同理,这是树干
{
 	SetColor(0,0);
 	cout<<" ";
 } 
void PaintColor()
{
	for(int i=0;i<30;i++)
	{
		for(int j=0;j<110;j++) Sky();
	}
	for(int i=30;i<35;i++)
	{
		for(int j=0;j<110;j++) grass();
	}
}void Cloud(int line)
{
	//srand((unsigned)time(NULL));
	int n=rand()%70;
	gotoxy(n,line);
	for(int i=0;i<8;i++)
	{
		SetColor(15,15);
		cout<<" ";
	}cout<<endl;
}
void SummonLake() 
{
	//srand((unsigned)time(NULL));
	int n=rand()%70;
	waters=n;watere=n+14;
	gotoxy(n,29);for(int j=0;j<2;j++){
	for(int i=0;i<14;i++)water();
		gotoxy(n,30);
		
	}cout<<endl;
}
void SummonT(int n)
{
	gotoxy(n,28);
	treegan();
	gotoxy(n,27);
	treegan();
	gotoxy(n,26);
	treegan();
	gotoxy(n-3,25);
	for(int i=0;i<6;i++) treeye();
	gotoxy(n-1,24);
	for(int i=0;i<4;i++) treeye();
}
void Tree()
{
	//srand((unsigned)time(NULL));
	int n=rand()%70;
	while(n>=waters && n<=watere)
	{
		n=rand()%70;
	}
	gotoxy(n,28);
	treegan();
	gotoxy(n,27);
	treegan();
	gotoxy(n,26);
	treegan();
	gotoxy(n-3,25);
	for(int i=0;i<6;i++) treeye();
	gotoxy(n-1,24);
	for(int i=0;i<4;i++) treeye();
	for(int i=n;i<=104;i+=9)
	{
		if(i>=waters && i<=watere) continue;
		else SummonT(i);
	}
}
int main()
{
	system("mode con cols=110 lines=35");
	srand((unsigned)time(NULL));
    SetConsoleTitle("My Life");
    PaintColor();SummonLake();Tree();srand((unsigned)time(NULL));
    Cloud(3);
    
    
    while(1) ;
    return 0;
}

代码越来越乱了……

好了,第一期挖的坑全部填上了,下面我们来分析一下小人的移动规则:

小人的移动空间可以看成一个一维字符数组move[110],如果小人在字符数组内遇到了障碍字符'#',那么就代表有丘陵或者是到地图尽头了。

同时,当小人穿过树时,会显示树而非显示小人。

想期待可以通过A和D移动的小人吗?

欢饮关注下期~

《从0开始制作小游戏》快捷跳转
从0开始制作小游戏:2
从0开始制作小游戏:1

当然可以!用 C++ 制作小游戏代码是一项有趣的任务。这里我介绍一个简单的命令行扫雷游戏的代码实现思路: 1. 定义一个类 `MineSweeper`,包含以下属性和方法: 属性: - `board`:一个维数组,表示扫雷棋盘上每个格子的状态。 - `rows` 和 `cols`:分别表示扫雷棋盘的行数和列数。 - `num_mines`:表示扫雷棋盘上地雷的数量。 方法: - `init_board`:初始化扫雷棋盘,将所有格子都设置为未开启状态(默认为 0),并随机放置地雷。 - `print_board`:打印扫雷棋盘的当前状态。 - `open_cell`:开启一个格子,如果该格子上是地雷,则游戏结束;否则,如果该格子周围没有地雷,则递归开启周围的格子。 - `play_game`:开始游戏,不断接收用户输入,并调用相应的方法进行处理,直到游戏结束。 2. 在 `main` 函数中创建一个 `MineSweeper` 对象,并调用其 `play_game` 方法开始游戏。 下面是一个简单的示例代码(仅供参考): ``` #include <iostream> #include <vector> #include <ctime> #include <cstdlib> using namespace std; class MineSweeper { public: MineSweeper(int r, int c, int m) : rows(r), cols(c), num_mines(m), board(rows, vector<int>(cols, 0)) { init_board(); } void init_board() { // 随机放置地雷 srand(time(NULL)); int count = 0; while (count < num_mines) { int r = rand() % rows; int c = rand() % cols; if (board[r][c] != -1) { board[r][c] = -1; count++; } } } void print_board() { cout << " "; for (int i = 0; i < cols; i++) { cout << " " << i; } cout << endl; for (int i = 0; i < rows; i++) { cout << i << " "; for (int j = 0; j < cols; j++) { if (board[i][j] == -1) { cout << "* "; } else if (board[i][j] == 0) { cout << "- "; } else { cout << board[i][j] << " "; } } cout << endl; } } void open_cell(int r, int c) { if (r < 0 || r >= rows || c < 0 || c >= cols || board[r][c] != 0) { return; } int count = 0; for (int i = max(0, r-1); i <= min(rows-1, r+1); i++) { for (int j = max(0, c-1); j <= min(cols-1, c+1); j++) { if (board[i][j] == -1) { count++; } } } if (count > 0) { board[r][c] = count; } else { board[r][c] = -2; for (int i = max(0, r-1); i <= min(rows-1, r+1); i++) { for (int j = max(0, c-1); j <= min(cols-1, c+1); j++) { open_cell(i, j); } } } } bool play_game() { print_board(); while (true) { int r, c; cout << "请输入要开启的格子的行号和列号,用空格分隔(例如:0 1):"; cin >> r >> c; if (board[r][c] == -1) { cout << "你踩到地雷了,游戏结束!" << endl; return false; } else { open_cell(r, c); print_board(); bool win = true; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (board[i][j] == 0) { win = false; break; } } } if (win) { cout << "恭喜你扫完了所有地雷,游戏胜利!" << endl; return true; } } } } private: int rows, cols, num_mines; vector<vector<int>> board; }; int main() { int rows = 10, cols = 10, num_mines = 10; MineSweeper game(rows, cols, num_mines); game.play_game(); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值