#include<stdio.h>
#include<stdlib.h> //清屏命令在这里。
#include <windows.h> //延时10毫秒-sleep,gotoxy函数
#include <iostream>
#include <conio.h> //getch()----不用按回车,就可以输入字符。
using namespace std;
//函数外定义全局变量
int high, width; //定义屏幕尺寸
int bird_x, bird_y; //小鸟的坐标。
int bar_y, bar_xTop, bar_xDown; //障碍物坐标
int score; //得分
void gotoxy(int x, int y) // 光标移动到intx,inty位置
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void HideCursor() //
{
CONSOLE_CURSOR_INFO cursor_info = { 1,0 }; //第二个值为0,表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup() //数据初始化
{
high = 18;
width = 25;
bird_x = 0;
bird_y = width / 3;
bar_y = width;
bar_xTop = high / 4;
bar_xDown = high / 2+2;
HideCursor(); //隐藏光标
}
void show() //显示画面
{
gotoxy(0, 0); //光标移动到原点位置,以下重置清屏
int i, j;
//system("cls"); //清除屏幕
//cout << position_x << " " << position_y << endl;
for (i = 0; i < high;i++) //2个for循环,将飞机,子弹,敌机都现在在屏幕上,通过不停的循环。巧妙的思路。
{
for (j = 0; j < width;j++)
{
if ((i==bird_x)&&(j==bird_y))
{
printf("@"); //输出小鸟@
}
else if ((j == bar_y-1) && ((i <= bar_xTop)||(i>=bar_xDown ))) //当满意一定条件时候,输出障碍物
{
printf("*"); //输出障碍物
}
else
{
printf(" "); //输出空格
}
}
printf("\n"); //这个位置不能乱动!否则错位。
}
printf("得分:%d\n :", score);
}
void UpdateWithoutInput() //与用户输入无关的更新,例如子弹自动往上飞,敌机自动往下落
{
if (bird_y==bar_y)
{
if ((bird_x>bar_xTop) && (bird_x<bar_xDown)) //在缺口的范围之内,加分,否则,游戏结束
{
score++;
}
else
{
exit(0); //程序结束运行。
}
}
bird_x++; //让小鸟位置持续下落
if (bar_y>0)
{
bar_y--; //让障碍物从右往左走。
}
else
{
bar_y = width; //当障碍我到最左边时候,重置一下。
//设置随机缺口这里,有点问题,还需要修改!
int randposition = rand() / (high - 5); //生成一个随机数。让障碍我缺口随机变化。
bar_xTop = randposition; //缺口上方,数值月销,缺口越大
bar_xDown = randposition + high/4+high/6 ; //缺口下方,数值越大,缺口越大
}
Sleep(150);
}
void UpdateWithInput() //与用户输入有关的更新。
{
char input;
if (kbhit()) //当按键时候执行。
{
input = getch();
if (input==' ') //左移挡板
{
bird_x -= 2;
}
}
}
int main()
{
startup(); //数据初始化
while (1)
{
show(); //显示画面
UpdateWithoutInput(); //与用户输入无关的更新
UpdateWithInput(); //与用户输入有关的更新。
}
return 0;
}

image.png
网友评论