C++射击用空格键进行发射用上下键控制发射方向

#include <iostream>
#include<cstdlib>
#include<windows.h>
#include<conio.h>
#include<ctime>
#include<vector>
using namespace std;
struct gun
{
    int x;
    int y;
    int w;
};
class Timer
{
private:
    clock_t t1,t2,t3;
    char c;
    vector<gun>g;
    int d;
public:
    Timer()
    {
        t1=clock();
        t2=clock();
        t3=clock();
        c=0;
        d=0;
    }
    void gotoxy(int x,int y)
    {
        HANDLE h;
        COORD c;
        c.X=x;
        c.Y=y;
        h=GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleCursorPosition(h,c);
    }
    void draw(int x,int y)
    {
        gotoxy(x,y);
        cout<<"HH";
    }
    void draw1(int x,int y)
    {
        gotoxy(x,y);
        cout<<"●";
    }
    void erase(int x,int y)
    {
        gotoxy(x,y);
        cout<<"  ";
    }
    void key(int& x,int& y,clock_t& t,int tt)
    {
        if(clock()-t>tt)
        {
            if(GetAsyncKeyState(VK_ESCAPE))
                exit(0);
            if(GetAsyncKeyState(VK_LEFT))
                c=1;//判断键盘按键的函数,比较灵敏,所以要用计时器来控制它接受键盘信息
            if(GetAsyncKeyState(VK_RIGHT))
                c=2;
            if(GetAsyncKeyState(VK_UP))
                d=1;
             if(GetAsyncKeyState(VK_DOWN))
                d=2;
            if(GetAsyncKeyState(VK_SPACE))
                {
                    gun temp;
                    temp.x=x;
                    temp.y=y-1;
                    temp.w=d;
                    g.push_back(temp);
                }
            t=clock();
        }
    }
    void move1(int& x,int& y,clock_t& t,int tt)
    {
        if(clock()-t>tt&&c!=0)
        {
            erase(x,y);
            switch(c)
            {
            case 1:
                --x;
                c=0;
                break;
            case 2:
                ++x;
                c=0;
                break;
            }
            draw(x,y);
            t=clock();
        }
    }
    void move2(clock_t& t,int tt)
    {
        if(clock()-t>tt)
        {
            for(int i=0; i<g.size(); ++i)
            {
                erase(g[i].x,g[i].y);
                if(g[i].w==1)
                {
                    g[i].y--;
                    if(--g[i].y<0)
                    {
                        g.erase(g.begin()+i);
                    }
                    else
                        draw1(g[i].x,g[i].y);
                }
                if(g[i].w==2)
                {
                    g[i].y++;
                    if(++g[i].y>30)
                    {
                        g.erase(g.begin()+i);
                    }
                    else
                        draw1(g[i].x,g[i].y);
                }
            }
            t=clock();
        }
    }
    void move()
    {
        int x=20;
        int y=20;
        draw(x,y);
        while(true)
        {
            key(x,y,t1,20);
            move1(x,y,t2,20);
            move2(t2,20);
        }
    }
};
int main()
{
    Timer t;
    t.move();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值