#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;
}
C++射击用空格键进行发射用上下键控制发射方向
最新推荐文章于 2021-08-02 02:14:26 发布