代码参考这个视频【C语言开发】教你用代码实现打气球游戏!C语言自制零基础射击小游戏,快来练练手吧!_哔哩哔哩_bilibili
#include<easyx.h>
#include<stdio.h>
#include<time.h>
#include<mmsystem.h>
#include<math.h>
#pragma comment(lib,"winmm.lib")
#define Bullet_Num 15 //子弹数量
#define Ballon_Num 10 //气球数量
#define B_speed 3//气球移动速度
IMAGE backgroud;
int score=0;
struct Ballon
{
int x;
int y;
int flag;
COLORREF color;
}ball[Ballon_Num];
struct Bullet //子弹
{
double x,y;
double vx,vy; //分量
COLORREF color;
bool flag; //子弹是否存在
}bull[Bullet_Num];
struct Battery //炮台
{
int x,y;
int endx,endy;
int r; //炮台半径
int len; //炮管的长度
double radian; //弧度
double speed; //子弹速度
}bat;
//初始化炮台
void Init_bat()
{
bat.x=getwidth()/2;
bat.y=getheight()-10;
bat.r=80;
bat.len=120;
bat.endx=bat.x;
bat.endy=bat.y-bat.len;
bat.speed=2;
}
//初始化气球
void Init_Ballon(int i)
{
ball[i].x=rand()%(getwidth()-30);
ball[i].y=rand()%(getheight()-50);
ball[i].flag=true;
ball[i].color=RGB(rand()%256,rand()%256,rand()%256);
}
void createBullet()
{
for(int i=0;i<Bullet_Num;i++)
{
if(!bull[i].flag)
{
bull[i].flag=true;
bull[i].x=bat.endx;
bull[i].y=bat.endy;
bull[i].color=RGB(rand()%256,rand()%256,rand()%256);
bull[i].vx=bat.speed*cos(bat.radian);
bull[i].vy=bat.speed*sin(bat.radian);
break;
}
}
}
void moveBullet()
{
for(int i=0;i<Bullet_Num;i++)
{
if(bull[i].flag)
{
bull[i].x+=bull[i].vx;
bull[i].y-=bull[i].vy;
if(bull[i].x<0 || bull[i].y<0 || bull[i].x>=getwidth() || bull[i].y>=getheight())
{
bull[i].flag=false;
}
}
}
}
void draw()
{
settextstyle(20,0,"楷体");
char core[10];
sprintf(core,"score:%d",score);
outtextxy(0,0,core);
//设置线条样式
setlinestyle(PS_SOLID,1);
//设置线条颜色
setlinecolor(WHITE);
for(int i=0;i<Ballon_Num;i++)
{
if(ball[i].flag)
{
//画椭圆
setfillcolor(ball[i].color);
solidellipse(ball[i].x,ball[i].y,ball[i].x+30,ball[i].y+60); //左上角和右下角的坐标
//画圆弧
arc(ball[i].x+10,ball[i].y+10,ball[i].x+30-10,ball[i].y+60-10,0,1.2);
arc(ball[i].x,ball[i].y+60,ball[i].x+20,ball[i].y+60+20,0,1.2);
}
}
//设置线条样式
setlinestyle(PS_SOLID,3);
//设置线条颜色
setlinecolor(BLACK);
//画炮台
circle(bat.x,bat.y,bat.r);
//画实心炮塔
setfillcolor(BLACK);
solidcircle(bat.x,bat.y,5);
//画炮管
line(bat.x,bat.y,bat.endx,bat.endy);
//绘制子弹
for(int j=0;j<Bullet_Num;j++)
{
if(bull[j].flag)
{
setfillcolor(bull[j].color);
solidcircle(bull[j].x,bull[j].y,5);
}
}
}
int music_boom()
{
mciSendString("open .\\music\\boom.mp3",0,0,0);
mciSendString("play .\\music\\boom.mp3",0,0,0);
return 0;
}
void mouseEvent()
{
static ExMessage msg;
//获取消息,如果获取了返回true,否则返回false
if(peekmessage(&msg,EX_MOUSE))
{
bat.radian=atan2(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;
switch(msg.message)
{
case WM_LBUTTONDOWN:
createBullet();
break;
case WM_RBUTTONDOWN:
break;
case WM_MOUSEWHEEL:
break;
default :
break;
}
}
}
//移动气球
void MoveBallon()
{
for(int i=0;i<Ballon_Num;i++)
{
if(ball[i].flag)
{
ball[i].y-=B_speed;
}
if(ball[i].y<0)
Init_Ballon(i);
}
}
//打气球
void playBallon()
{
for(int i=0;i<Ballon_Num;i++)
{
if(!ball[i].flag)
continue;
for(int j=0;j<Bullet_Num;j++)
{
if(!bull[j].flag)
continue;
//判断子弹是否打中气球
if(bull[j].x>=ball[i].x && bull[j].x<=ball[i].x+30 && bull[j].y>=ball[i].y && bull[j].y<=ball[i].y+60)
{
bull[j].flag=false;
ball[i].flag=false;
music_boom();
Init_Ballon(i);
score++;
}
}
}
}
int main()
{
char TIME[10];
int time=0;
initgraph(800,800,EX_SHOWCONSOLE);
mciSendString("open .\\music\\love.mp3",0,0,0);
mciSendString("play .\\music\\love.mp3",0,0,0);
Init_bat();
for(int i=0;i<Ballon_Num;i++)
Init_Ballon(i);
loadimage(&backgroud,"./images/back.jpg");
//双缓冲
BeginBatchDraw();
setbkmode(TRANSPARENT);
while(1)
{
int begin=clock();
int sec=time/1000;
putimage(0,0,&backgroud);
draw();
sprintf(TIME,"time:%d",60-sec);
outtextxy(720,0,TIME);
mouseEvent();
if(time%50==0)
MoveBallon();
moveBullet();
playBallon();
FlushBatchDraw();
cleardevice();
if(sec>60)
break;
int end=clock();
time+=end-begin;
}
EndBatchDraw();
mciSendString("pause .\\music\\love.mp3",0,0,0);
printf("你的总分是:%d",score);
getchar();
return 0;
}