C++小游戏

#include<bits/stdc++.h>
#include<windows.h>
#include <stdlib.h>
#include<conio.h>
using namespace std;
int high,width;
int bird_x,bird_y;
int bar_y,bar_xTop,bar_xDown;
int score=0;
void startup()
{
    high=15;
    width=25;
    bird_x=0;
    bird_y=width/3;
    bar_y=width;
    bar_xTop=high/4;
    bar_xDown=high/2+2;
}
void gotoxy(int x,int y)
{
    HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X=x;
    pos.Y=y;
    SetConsoleCursorPosition(handle,pos);
}
void show()
{
    gotoxy(0,0);
    for (int i=0;i<=high+1;i++)
    {
        for (int j=0;j<=width;j++)
        {
            if ((i==bird_x)&&(j==bird_y))
                printf("@");
            else if ((j==bar_y)&&((i<=bar_xTop)||(i>=bar_xDown)))
                printf("*");
            else
                printf(" ");
        }
        cout<<endl;
    }
    cout<<"得分:"<<score;
}
void updateWithoutInput()
{
    if (bar_y==bird_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+3;
    }
    Sleep(230);
}
void updateWithInput()
{
    char input;
    if (kbhit())
    {
        input=getch();
        if (input==' ')
        {
            bird_x-=2;
        }
    }
}
int main()
{
    startup();
    while(1)
    {
        show();
        updateWithoutInput();
        updateWithInput();
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值