#include <iostream>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <string>
#include <ctime>
using namespace std;
bool gameOver;//判断游戏开始结束
///分数
int score;
//障碍物随机数//
int m=1,m1=1,m2=1;
/两个相邻障碍物随机距离数///
int r;
//声明函数
void Setup();
void Draw();
void Input();
void Logic();
地图范围///
const int width = 40;
const int height = 10;
///地图坐标//
/*
x
|
|
| *
| o *
|——————————>y
*/
//主角坐标///
int x = 8;//x是高度
int y = 20;//y是横向长度
int n = 38;//地图横向坐标
int up = 0;//跳跃初始值
int s;
/主函数
int main(void)
{
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
Sleep(20);
}
return 0;
}
///初始化程序///
void Setup()
{
gameOver = false;
score = 0;
}
//游戏逻辑/
void Logic()
{
//障碍物移动///
n--;
if (n+r == 0)///当障碍物到最左边时
{
/障碍物回归左边/
n = 38;
/生成随机数/
srand((unsigned)time(NULL));
s = rand();
srand(s);
m = rand() % 2;
m1 = rand() % 2;
m2 = rand() % 2;
r = (rand() %14) + 6;
}
加分判断/
if (n == 20||n+r==20)
score = score + 1;
///碰撞死亡/
if (x == 8 && y == n && m==1)
gameOver = true;
if (x == 7 && y == n&& m1==1)
gameOver = true;
if (x == 6 && y == n && m2 == 1)
gameOver = true;
if (x == 8 && y == n+r && m == 1)
gameOver = true;
if (x == 7 && y == n+r && m1 == 1)
gameOver = true;
if (x == 6 && y == n+r && m2 == 1)
gameOver = true;
///主角跳跃实现代码
if (up == 1 && x > 4)
{
x--;
if (x == 4)
up = 2;
}
if (up == 2)
{
x++;
if (x == 8)
up = 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 == x && j == y)
cout << "O";
/障碍生成//
///第一个障碍///
//else if (i == 8 && j == n && m==1)
else if (i == 8 && j == n )
cout << "#";
else if (i == 7 && j == n&& m1==1)
cout << "#";
else if (i == 6 && j == n&& m2==1)
cout << "#";
第二个障碍
//else if (i == 8 && j == n+r && m == 1)
else if (i == 8 && j == n+r)
cout << "#";
else if (i == 7 && j == n+r && m1 == 1)
cout << "#";
else if (i == 6 && j == n+r && m2 == 1)
cout << "#";
///绘制地面///
else if (i == 8 && j < width)
cout << "_";
绘制边框///
else if (j == width)
cout << "*";
///绘制背景空白处///
else
cout << " ";
}
cout << endl;
}
for (int i = 0; i < width + 2; i++)//画底部边界
cout << "*";
cout << endl;
//cout << up << endl;
//调试参数
cout << "score: " << score << endl;
cout << "m2: " << m2 << endl;
cout << "m1: " << m1 << endl;
//cout << "m : " << m << endl;
cout << " m: 1"<< endl;
cout << "r: " << r << endl;
}
/输入///
void Input()
{
if (_kbhit())
{
switch (_getch())
{
case 72://小键盘↑键,控制跳跃
up = 1;
break;
case 'x'://x键退出游戏
gameOver = true;
break;
}
}
}
/随机数不够随机,关卡重复了
2021-11-16跳高高小游戏进阶版,新增随机障碍物,详细注解
最新推荐文章于 2024-08-09 10:25:03 发布