利用vs 2019和easyx图形库完成射击小游戏
需要的工具:
- Win 10
- VS 2019
- EasyX
main.cpp
#include<stdio.h>
#include<math.h>
#include<graphics.h>
#define WIDTH 960
#define HEIGHT 640
#define BULL_NUM 20
struct Battery
{
int x;
int y;
int endx;
int endy;
int len;
int speed;
double radian;
}bat;
struct Bullet
{
int x;
int y;
int vx;
int vy;
int r;
bool falg;
DWORD color;
}shot[BULL_NUM];
struct Balloon
{
int x;
int y;
bool flag;
DWORD color;
}ball[BULL_NUM];
void GameInit()
{
initgraph(WIDTH, HEIGHT, SHOWCONSOLE);
srand(GetTickCount());
bat.x = WIDTH / 2;
bat.y = HEIGHT - 10;
bat.endx = bat.x;
bat.endy = bat.y - 70;
bat.len = 70;
bat.radian = 0;
bat.speed = 5;;
for (int i = 0; i < BULL_NUM; i++)
{
shot[i].falg = false;
shot[i].r = 5;
}
for (int i = 0; i < BULL_NUM; i++)
{
ball[i].x = rand() % (WIDTH - 30);
ball[i].y = rand() % (HEIGHT - 60);
ball[i].flag = true;
ball[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
}
}
void GameDraw()
{
BeginBatchDraw();
setbkcolor(RGB(114, 191, 207));
cleardevice();
for (int i = 0; i < BULL_NUM; i++)
{
if (ball[i].flag)
{
setfillcolor(ball[i].color);
solidellipse(ball[i].x, ball[i].y, ball[i].x + 30, ball[i].y + 50);
setlinestyle(PS_SOLID, 1);
setlinecolor(WHITE);
arc(ball[i].x + 5, ball[i].y + 5, ball[i].x + 25, ball[i].y + 45, 0, 1.4);
arc(ball[i].x, ball[i].y + 50, ball[i].x + 20, ball[i].y + 80, 0, 1.4);
}
}
setlinestyle(PS_SOLID, 2);
circle(bat.x, bat.y, 60);
setfillcolor(BLACK);
solidcircle(bat.x, bat.y, 5);
setlinestyle(PS_SOLID, 5);
setlinecolor(BLACK);
line(bat.x, bat.y, bat.endx, bat.endy);
for (int i = 0; i < BULL_NUM; i++)
{
if (shot[i].falg)
{
setfillcolor(shot[i].color);
solidcircle(shot[i].x, shot[i].y, shot[i].r);
}
}
EndBatchDraw();
}
void CreatBullet()
{
for (int i = 0; i < BULL_NUM; i++)
{
if (!shot[i].falg)
{
shot[i].falg = true;
shot[i].x = bat.endx;
shot[i].y = bat.endy;
shot[i].vx = bat.speed * cos(bat.radian);
shot[i].vy = bat.speed * sin(bat.radian);
shot[i].color = RGB(rand() % 256, rand() % 256, rand() % 256);
break;
}
}
}
void BulletMove()
{
for (int i = 0; i < BULL_NUM; i++)
{
if (shot[i].falg)
{
shot[i].x += shot[i].vx;
shot[i].y -= shot[i].vy;
if (shot[i].x<0 || shot[i].x>WIDTH || shot[i].y<0 || shot[i].y>HEIGHT)
{
shot[i].falg = false;
}
}
}
}
void MouseEvent()
{
if (MouseHit())
{
MOUSEMSG msg = GetMouseMsg();
bat.radian = atan2((double)bat.y - msg.y, msg.x - bat.x);
bat.endx = bat.x + cos(bat.radian) * bat.len;
bat.endy = bat.y - sin(bat.radian) * bat.len;
if (msg.uMsg == WM_LBUTTONDOWN)
{
CreatBullet();
}
}
}