跟着童晶老师做游戏(c语言入门)//小鸟飞飞

#include<bits/stdc++.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include <time.h>
#include <dos.h>
#define Speed 4
#define Bar 8            //障碍数 

using namespace std;

int brid_x,brid_y;
int high,width;
int speed;
int num;
int iskill;
 
void gotoxy(int x, int y)   //光标移动到x,y位置 
{
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
    SetConsoleCursorPosition(hOut, pos);          //两个参数分别是指定哪个窗体,具体位置
}

void HideCursor() // 用于隐藏光标
{
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};  // 第二个值为0表示隐藏光标
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);//函数和结构体都在windows.h中定义。
}

struct bars
{
    int y;
    int top_x;
    int width_x;
}bar[Bar];

void startup()
{
    srand(time(NULL));
    HideCursor();
    high=28;
    width=100;
    brid_x=high/2;
    brid_y=10;
    speed=0;
    num=0;
    iskill=0;
    for(int i=0;i<Bar;i++)
    {
        bar[i].y=i*(width/Bar)+20;
        bar[i].top_x=rand()%high*3/5;
        bar[i].width_x=rand()%5+4;
    }
}

void show()

    gotoxy(0,0);
    printf("目前分数:%d\n",num);
    for(int i=1;i<high;i++)
            {
                for(int j=0;j<width;j++)
                {
                    if(i==brid_x&&
                    j==brid_y) printf("@");
                    for(int z=0;z<Bar;z++)
                    {
                        if(bar[z].y==j&&
                        (bar[z].top_x>=i
                        ||bar[z].width_x
                        +bar[z].top_x<=i))
                        {
                            cout<<"*";
                            j++;
                        }
                    }
                    if(i!=brid_x||
                    j!=brid_y) printf(" ");
                }
                printf("\n");
            }
    for(int j=0;j<width+1;j++)
        cout<<"-";
    cout<<endl;
    if(iskill==1)
    {
        cout<<"呜呜,失败了\n";
        system("pause");
        exit(0);
    } 
    
//    printf("*");
}

void updateWithoutInput()
{
    if(speed<Speed-num/5)    speed++;
    if(speed>=Speed-num/5)
    {
        brid_x++;
        for(int i=0;i<Bar;i++)
        {
            bar[i].y--;    
        }
        speed=0;    
    }
    if(brid_x<=1) brid_x=1;
    if(brid_x>=high) 
    {
        brid_x=high-1;
        iskill=1;
    }
    for(int i=0;i<Bar;i++)
        {
            if(bar[i].y==brid_y&&
            (bar[i].top_x>=brid_x
            ||bar[i].width_x
            +bar[i].top_x<=brid_x)) 
            {
                iskill=1;
                num--;
            }    
        }
    for(int i=0;i<Bar;i++)
        {
            if(bar[i].y<brid_y)
            {
                num++;
                bar[i].top_x=rand()%high*3/5;
                bar[i].width_x=rand()%5+4;
                bar[i].y=width;
            }    
        }
}

void updateWithInput()

     char input;
     if(kbhit())
     {
     input=getch();
    if(input==' ')      
    {
        brid_x-=2;
    }
    if(input==27) system("pause");
    }
     
}

int main()
{
    startup();   //数据初始化 
    while(1)     //游戏循环执行 
    {
        show();        //游戏画面 
        updateWithoutInput();    //与输入无关更新 
        updateWithInput();        //与输入有关更新 
    }
    return 0;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值