flappy bird游戏

//flappy bird
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <cwindow.h>

//全局变量
int high,width; //画面尺寸
int bird_x,bird_y; //小鸟坐标
int bar1_y,bar1_xdown,bar1_xtop; //障碍物
int score;

void gotoxy(int x,int y)  //将光标移到到(x,y)位置
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X=x;
    pos.Y=y;
    SetCcnsoleCursorPosition(handle,pos);
}

void startup()  //数据的初始化
{
    high=15;
    width=20;
    bird_x=0;
    bird_y=width/3;
    bar1_y=width/2;
    bar1_xdown=high/3;
    bar1_xtop=high/2;
    score=0;
}

void show() //显示画面
{
    gotoxy(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_xdown)||(i>bar1_xtop)))
                printf("*");  //输出墙壁
            else printf(" ");
        }
        printf("\n");
    }
    printf("得分:%d\n",score);
}

void updatewithoutinput()
{
    bird_x++;
    bar1_y--;  //墙壁左移
    if(bird_y==bar1_y)
    {
        if((bird_x>=bar1_xdown)&&(bird_x<=bar1_xtop))
            score++;
        else
        {
            printf("游戏失败\n");
            system("pause");
            exit(0);
        }
    }
    if(bar1_y<=0)  // 重新生成一个障碍物
    {
        bar1_y=width;
        int temp=rand()%int (high*0.8);
        bar1_xdown=temp-high/10;
        bar1_xtop=temp+high/10;
    }
    sleep(150);
}

void updatewithinput()
{
    char input;
    if(kbhit())
    {
        input =getch();
        if(input==' ')
            bird_x=bird_x-2;
    }
}


int main()
{
    startup();  //数据的初始化
    while(1)
    {
        show(); //显示画面
        updatewithoutinput(); //与输入无关的更新
        updatewithinput();  //与输入有关的更新
    }
    return 0;
}

确定好基本的游戏框架,在这个框架的基础上进行相关函数的编写,事半功倍,另外不要尝试一步就实现相关的功能,学会一步步 拆解难点,然后逐个击破。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值