#include <iostream>
#include<conio.h>
#include<windows.h>
#include<ctime>
using namespace std;
class Bullet {
public:
int x, y;
bool active = false;
void show() {
setfillcolor(RGB(150, 180, 210));
fillrectangle(x - 5, y - 5, x + 5, y + 5);
}
void hide() {
setfillcolor(0);
fillrectangle(x - 5, y - 5, x + 5, y + 5);
}
void move() {
x += 3;
}
};
class Player {
public:
int x, y, hp = 100;
COLORREF color = RGB(150, 180, 210);
void show() {
setfillcolor(color);
fillrectangle(x - 25, y - 25, x + 25, y + 25);
}
void hide() {
setfillcolor(0);
fillrectangle(x - 25, y - 25, x + 25, y + 25);
}
void fire(Bullet &bullet) {
bullet.active = true;
bullet.x = x + 45;
bullet.y = y;
bullet.show();
}
};
int main() {
initgraph(640, 550);
Player player;
player.x = 30;
player.y = 30;
Bullet bullets[8];
while (true) {
if (GetAsyncKeyState('W') & 0x8000) {
player.hide();
player.y -= 3;
player.show();
}
if (GetAsyncKeyState('S') & 0x8000) {
player.hide();
player.y += 3;
player.show();
}
if (GetAsyncKeyState('K') & 0x8000) {
for (auto &bullet : bullets) {
if (!bullet.active) {
player.fire(bullet);
break;
}
}
}
for (auto &bullet : bullets) {
if (bullet.active) {
bullet.hide();
bullet.move();
bullet.show();
if (bullet.x >= 635) bullet.active = false;
}
}
Sleep(30);
}
closegraph();
return 0;
}