flappy bird c语言实现

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
//辅助函数
void gotoxy(int x, int y) //将光标调整到(x,y)的位置
{
    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 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

//全局变量的定义
//游戏画面大小
int high;
int width;
//小鸟的坐标
int bird_x;
int bird_y;
//障碍物的相关坐标    
int bar1_y;
int bar1_xTop;
int bar1_xDown;
int score;

void startup() //数据初始化
{
    //游戏界面大小 
    high = 15;    
    width = 25;
    //小鸟的初始位置 
    bird_x = high / 2;
    bird_y = width / 4;
    //障碍物的初始位置
    bar1_y = width - 1;    
    bar1_xTop = high / 4;
    bar1_xDown = high / 2;
    //得分 
    score = 0;
}

void show()  //显示画面
{
    gotoxy(0, 0);    //将光标调整到(0,0)的位置
    int i, j;
    for (i = 0; i < high; i++)
    {
        for (j = 0; j < width; j++)
        {
            if ((i == bird_x) && (j == bird_y))    //输出小鸟
            {
                printf("@");
            }
            else if ((j == bar1_y) && ((i < bar1_xTop) || (i > bar1_xDown)))    //输出障碍物    
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }
    printf("得分:%d\n", score);
}

int updateWithoutInput()//与用户无关的更新
{
    bird_x++;    
    bar1_y--;
    if (bird_y == bar1_y)
    {
        if ((bird_x >= bar1_xTop) && (bird_x <= bar1_xDown))    
        {
            score++;
        }
        else
        {
            printf("游戏失败!!!");
            return -1;
        }
    }
    else
    {
        if (bird_x > high)
        {
            printf("游戏失败!!!");
            return -1;
        }
    }
    if (bar1_y <= 0)
    {
        bar1_y = width - 1;
        int upside = rand() % (int)(high * 0.6) + 1;
        bar1_xTop = upside;
        int opening = rand() % (int)(high * 0.2) + 2;
        while ((bar1_xDown = bar1_xTop + opening) > high - 2)
        {
            opening = rand() % (int)(high * 0.2) + 2;
        }
    }
    Sleep(100);
    return 0;
 }
 
void updateWithInput()//与用户输入有关的更新
{
    char input;
    if (_kbhit())
    {
        input = _getch();
        if (input == ' ' && bird_x > 0)
        {
            bird_x = bird_x - 2;
        }
    }
}
int main()
{
    srand((unsigned)time(NULL));
    HideCursor();
again:
    startup();    //初始化变量 
    while (1)
    {
        show(); //显示画面
        int ret = updateWithoutInput();//与用户无关的更新
        if (ret == -1)
        {
            system("CLS");
            printf("1.重新开始\n0.退出\n请选择:");
            int input = 0;
            scanf("%d", &input);
            if (input)
            {
                goto again;
            }
            else
                return 0;
        }
        updateWithInput();//与用户输入有关的更新
    }
    return 0;
}
 

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值