上期我们说到我已经做好了游戏的基本界面,现在,我将在这空荡荡的世界里添加一棵树
效果图
?(有的人还可以看到罕见的水上树呢!)
贴代码咯:
#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移动的小人吗?
欢饮关注下期~
|