基于ege的飞机大战
上来先放结果图(为了上传,所以帧数极低,游戏过程帧数设置为75帧)变身、大招、射击、积分、充值演示
因为是课设,所以要做的炫酷点(笑),飞机会左右摆动,消耗能量可以变身强化,空格键可以发射螺旋丸,打不过的话还可以充钱( ̄_, ̄ ),最后会显示历史最高分
下面贴贴代码,记得先把素材放到根目录呀
cpp文件
#include "flyflyfly.h"
#include <graphics.h>
#include <iostream>
#include <stdlib.h>
#include "ege/fps.h"
#include <fstream>
using namespace std;
int main()
{
initgraph(600, 800, INIT_RENDERMANUAL);
char name[30] = { "哈,飞机大战" };
setcaption(name);//改窗口的名字
fps fp;//显示帧率
keyi:login();//登录函数和返回的位置
help();//帮助界面
建敌人
zmy ball[300];
做玩家
mds player;
player.dx = player.dy = 4;//玩家移动速度
player.x = player.y = 300;//玩家初始坐标
yct boom[1900];//1900发子弹
int i = 0, ii = 0;
初始化敌人
initialization_enemy(ball);
各种游戏数据/
int n = 1;//敌人数量
int num = 0;//入侵次数
int dang = 0;//撞机次数
int speed = 7;//发射子弹速度
int fffzd = 66;//能量
int toy = 3;//大招数量
double zdspeed = 7;//预设子弹速度
int color;//子弹颜色
//各种时间参数/
double time = 1;
double t = fclock();
double tt = fclock();
double ttt = fclock();
double ti = fclock();
int zd = 0, uzd = 0, die = 0;
for (; is_run(); delay_fps(75))
{
cleardevice();//清屏
/敌人数量增加
enemy_num(&t, &n, &time, fclock());
/刷新敌人
refresh_enemy(&color, &ti,&num, &die, &n, ball, &time);
射击检测
shooting_examine(&ii, &uzd, ball, boom, &die, &zd, &n);
刷新子弹
refresh_bullet(&i, &uzd, &zd, boom, &player, &speed);
计数
counting(&num, &die, &zd, &dang, &fffzd, &toy);
玩家
Player(&fffzd, &n, &player, ball, &die, &dang);
控制
control(&player, &tt, &speed, &zdspeed, &zd, &ttt, &fffzd, &n, &die, ball, &toy, &dang);
结束提示
if ((dang) == 10)
{
for (i = 0; i < 200; i++, delay_fps(100))
{
setfont(-17, 0, "宋体");
char death[50] = "生命值已全部消耗,飞船即将坠毁";
outtextxy(140, 80, death);
char* hi = new char[3];
itoa((int)((200 - i) / 100+1), hi, 10);
outtextxy(210, 120, hi);
char Return[23] = "S后跳转充值界面";
outtextxy(220, 120, Return);
}
buy(&zdspeed, &fffzd, &toy, &dang);
}
if ((dang) == 10)
{
PIMAGE end = newimage();
getimage(end, "end.jpg");
for (i=0; i<500; i++,delay_fps(100))
{
cleardevice();
putimage(0, 0, end);
setbkmode(TRANSPARENT);
setfont(-23, 0, "宋体");
setcolor(BLACK);
char death[33] = "游戏结束,本次任务您的得分为:";
outtextxy(105, 410, death);
char* hinum = new char[3];
itoa((1800 - zd + 100 * die - 200 * num), hinum, 10);
outtextxy(445, 410, hinum);
int highest = 0;
int high= (1800 - zd + 100 * die - 200 * num);
fstream afile;
afile.open("high.txt",ios::in);//打开文件
afile>>highest;//读取最高分
afile.close();//关
if (high>highest)
{
highest = high;
fstream bfile;
bfile.open("high.txt", ios::out);
bfile << high;//写入最高分
bfile.close();//关
}
char hhh[33] = "历史最高得分为:";
outtextxy(170, 470, hhh);
char* hh = new char[33];
itoa((highest), hh, 10);
outtextxy(360, 470, hh);
char* hi = new char[3];
itoa((int)((500-i)/100+1), hi, 10);
outtextxy(206, 530, hi);
char Return[23] = "S后返回主菜单";
outtextxy(220, 530, Return);
}
goto keyi;//返回开始
}
}
closegraph();//关闭窗口
return 0;
}
flyflyfly.h
#ifndef FLYFLYFLY_H
#define FLYFLYFLY_H
#include <graphics.h>
#include <math.h>
#include <iostream>
#include <stdlib.h>
#include "ege/fps.h"
using namespace std;
struct zmy {//敌人
int dx, dy;
int x, y;
};
struct yct {//子弹
int x, y;
color_t color;
};
struct mds {//玩家
int dx, dy;
int x, y;
//color_t color;
};
//数量
void enemy_num(double* t, int* n, double* time, double a);
//刷新敌人
void refresh_enemy(int *color,double* ti, int* num, int* die, int* n, zmy* ball, double* time);
//初始化敌人
void initialization_enemy(zmy* ball);
//强化
void fire(mds* player, int* speed, double* ttt, int* zd, int* fff);
//登录
void login();
//帮助
void help();
//充值
void buy(double* zdspeed, int* fffzd, int* toy, int* dang);
//大招
void bug(int* n, zmy* ball, int* die);
//命中检测
void shooting_examine(int* ii, int* uzd, zmy* ball, yct* boom, int* die, int* zd, int* n);
//刷新子弹
void refresh_bullet(int* i, int* uzd, int* zd, yct* boom, mds* player, int* speed);
//计数提示
void counting(int* num, int* die, int* zd, int* dang, int* fffzd, int* toy);
//玩家刷新
void Player(int* fffzd, int* n, mds* player, zmy* ball, int* die, int* dang);
//控制
void control(mds* player, double* tt, int* speed, double* zdspeed, int* zd, double* ttt, int* fffzd, int* n, int* die, zmy* ball, int* toy, int* dang);
void enemy_num(double* t, int* n, double* time, double a)
{
if ((a - *t) > 5)//5s加一个
{
(*t) = a;
if ((*n) < 300)
++(*n);
}
}
void refresh_enemy(int *color,double* ti, int* num, int* die, int* n, zmy* ball, double* time)
{
int i;
PIMAGE fly = newimage();
getimage(fly, "fly3.png");//图片
for (i = 0; i < *n; ++i)
{
ball[i].x += ball[i].dx;
ball[i].y += ball[i].dy;//敌人移动
if (ball[i].x < (0) || ball[i].x >(545))
ball[i].dx = -ball[i].dx;//碰墙x速度反向
if (ball[i].y > 800)
{
ball[i].y -= 1800;//向后移动1800像素,循环使用
if (ball[i].dx == 0)
ball[i].dx += 1;
(*num)++;//入侵数加一
if ((*n) % 2 == 0 && (*n) < 180)
{
(*time) -= 0.01;//减少间隔
}
}
putimage_withalpha(NULL, fly, (ball[i].x), ball[i].y);//透明
}
if (fclock() - (*ti) > 3)//3s换一次
{
*color = random(0xff0000) + 0xffff;
(*ti) = fclock();
setfillcolor(*color);//子弹颜色随机
}
else
setfillcolor(*color);
delimage(fly);//释放
}
void initialization_enemy(zmy* ball)
{
int p;
int fly = 0;
for (int i = 0; i < 300; (i)++)
{
if (i % 60 == 0)
(fly)+=1;
randomize();
ball[i].x = random(520);
ball[i].y = 0;
randomize();
(p) = random(2);
randomize();
ball[i].dx = random(4);
if ((p) == 0)ball[i].dx = -ball[i].dx;
ball[i].dy = random(3) + fly;
randomize();
}
}
void fire(mds* player, int* speed, double* ttt, int* zd, int* fff)
{
if (fclock() - (*ttt) > 0.05)
{
if (keystate('S') && (*fff) > 0 && (*zd < 1800))// S按下了
{
(*ttt) = fclock();
(*zd)++;
(*fff) -= 1;
(*speed) = 12;
player->dx = player->dy = 7;
}
else if (fclock() - (*ttt) > 10 && (*fff) < 66)
{
(*ttt) = fclock();
(*fff) = 66;
}
}
if (!keystate('S'))
player->dx = player->dy = 4;
}
void login()
{
int x, y;
cleardevice();
PIMAGE log = newimage();
getimage(log, "log.jpg");
PIMAGE logA = newimage();
getimage(logA, "logA.jpg");
PIMAGE logB = newimage();
getimage(logB, "logB.jpg");
for (; ; delay_fps(120))
{
mousepos(&x, &y);
if (x < 560 && x>450 && y < 480 && y>430)
{
putimage(0, 0, logA);
if (keystate(VK_LBUTTON))
{
delimage(logA);
break;
}
}
else if (x < 560 && x>450 && y < 560 && y>510)
{
putimage(0, 0, logB);
if (keystate(VK_LBUTTON))
{
exit(1);
}
}
else
{
putimage(0, 0, log);
}
}
}
void help()
{
double tt = fclock();
int x, y;
cleardevice();
while (kbhit())
{
getch();
}
PIMAGE l10 = newimage();
getimage(l10, "help.png");
for (; ; delay_fps(120))
{
putimage(0, 0, l10);
mousepos(&x, &y);
if (fclock() - tt > 0.6)
{
if (keystate(VK_LBUTTON))
if (x < 600 && x>0 && y < 550 && y>350)
break;
else
continue;
else if (kbhit())
break;
else
continue;
}
}
delimage(l10);
}
void buy(double* zdspeed, int* fffzd, int* toy, int *dang)
{
int x, y;
cleardevice();
PIMAGE l10 = newimage();
getimage(l10, "cq.jpg");
for (; ; delay_fps(120))
{
putimage(0, 0, l10);
mousepos(&x, &y);
if (x < 270 && x>70 && y < 780 && y>735)
{
if (keystate(VK_LBUTTON))
{
(*dang) = 0;
(*zdspeed) = 15;
(*fffzd) = 99999;
(*toy) += 10;
return;
}
}
if (x < 510 && x>335 && y < 780 && y>735)
{
if (keystate(VK_LBUTTON))
{
(*zdspeed) = 2;
(*fffzd) = 0;
return;
}
}
}
delimage(l10);
}
void bug(int* n, zmy* ball, int* die)
{
double x = 500, y = 600;
int i = 0;
PIMAGE log1 = newimage();
getimage(log1, "bug0.png");
PIMAGE log2 = newimage();
getimage(log2, "bug1.png");
PIMAGE log3 = newimage();
getimage(log3, "bug2.png");
PIMAGE log4 = newimage();
getimage(log4, "bug3.png");
PIMAGE log5 = newimage();
getimage(log5, "bug4.png");
PIMAGE log6 = newimage();
getimage(log6, "bug5.png");
PIMAGE log7 = newimage();
getimage(log7, "bug6.png");
PIMAGE log8 = newimage();
getimage(log8, "bug7.png");
PIMAGE log9 = newimage();
getimage(log9, "bug8.png");
PIMAGE log0 = newimage();
getimage(log0, "bug9.png");
PIMAGE lo1 = newimage();
getimage(lo1, "boom0.png");
PIMAGE lo2 = newimage();
getimage(lo2, "boom1.png");
PIMAGE lo3 = newimage();
getimage(lo3, "boom2.png");
PIMAGE lo4 = newimage();
getimage(lo4, "boom3.png");
PIMAGE lo5 = newimage();
getimage(lo5, "boom4.png");
PIMAGE lo6 = newimage();
getimage(lo6, "boom5.png");
PIMAGE lo7 = newimage();
getimage(lo7, "boom6.png");
PIMAGE lo8 = newimage();
getimage(lo8, "boom7.png");
PIMAGE lo9 = newimage();
getimage(lo9, "boom8.png");
for (; i < 14; i++)
{
delay_fps(40);
putimage_withalpha(NULL, log1, x, y -= 4);
delay_fps(40);
putimage_withalpha(NULL, log2, x -= 4.5, y);
delay_fps(40);
putimage_withalpha(NULL, log3, x, y -= 4);
delay_fps(40);
putimage_withalpha(NULL, log4, x -= 4.5, y);
delay_fps(40);
putimage_withalpha(NULL, log5, x, y -= 4);
delay_fps(40);
putimage_withalpha(NULL, log6, x -= 4.5, y);
delay_fps(40);
putimage_withalpha(NULL, log7, x, y -= 4);
delay_fps(40);
putimage_withalpha(NULL, log8, x -= 4.5, y);
delay_fps(40);
putimage_withalpha(NULL, log9, x, y -= 4);
delay_fps(40);
putimage_withalpha(NULL, log0, x -= 4.5, y);
}
delimage(log0);
delimage(log1);
delimage(log2);
delimage(log3);
delimage(log4);
delimage(log5);
delimage(log6);
delimage(log7);
delimage(log8);
delimage(log9);
cleardevice();
putimage(25, 190, lo1);
delimage(lo1);
delay_fps(10);
putimage(25, 190, lo2);
delimage(lo2);
delay_fps(10);
putimage(25, 190, lo3);
delimage(lo3);
delay_fps(10);
putimage(25, 190, lo4);
delimage(lo4);
delay_fps(10);
putimage(25, 190, lo5);
delimage(lo5);
delay_fps(10);
putimage(25, 190, lo6);
delimage(lo6);
delay_fps(10);
putimage(25, 190, lo7);
delimage(lo7);
delay_fps(9);
putimage(25, 190, lo8);
delimage(lo8);
delay_fps(9);
putimage(25, 190, lo9);
delimage(lo9);
for (int i = 0; i < *n; i++)
if (
ball[i].y < 800
&& ball[i].y >(-100)
)
{/飞机返回重用
ball[i].y -= 1800;
ball[i].dy = random(4)+3;
ball[i].dx = random(4);
randomize();
int p = random(2);
if (p == 0)ball[i].dx = -ball[i].dx;
(*die)++;
}
}
void shooting_examine(int* ii, int* uzd, zmy* ball, yct* boom, int* die, int* zd, int* n)
{
int i ,p;
for (*ii = *uzd; (*ii) < *zd; ++(*ii))
{
for ((i) = 0; (i) < (*n); (i)++)
if (
(ball[i].x) < boom[*ii].x
&& ball[i].y < boom[*ii].y
&& ball[i].y > boom[*ii].y - 50
&& (ball[i].x + 60) > boom[*ii].x
)
{/飞机重用
ball[i].y -= 1800;
ball[i].dy = random(3) + 2;
ball[i].dx = random(4);
randomize();
(p) = random(2);
if (p == 0)ball[i].dx = -ball[i].dx;
boom[*ii].x -= 900;//子弹移除出场
(*die)++;
}
}
}
void refresh_bullet(int* i, int* uzd, int* zd, yct* boom, mds* player, int* speed)
{
for ((*i) = (*uzd); (*i) < (*zd); ++(*i))//还存在的子弹,发射子弹数zd-到达上部子弹数uzd
{
if ((*i) == (*zd)-1)//初始化发射的最后一个子弹
{
boom[*i].x = (player->x + 35);//子弹从飞机中间射出
boom[*i].y = player->y+30;
break;
}
boom[*i].y -= *speed;//子弹向上移动
fillellipsef(boom[*i].x, boom[*i].y, 3, 8);//绘出子弹
if (boom[*i].y < 50)
(*uzd)++;//用过的子弹(到达上面的)
}
}
void counting(int* num, int* die, int* zd, int* dang, int* fffzd, int* toy)
{
setbkmode(TRANSPARENT);
char drnumts[16] = "已入侵敌人数量:";
outtextxy(5, 15, drnumts);
char* drnum = new char[6];
itoa((*num), drnum, 10);
outtextxy(130, 15, drnum);
char died[16] = "已消灭敌人数量:";
outtextxy(5, 40, died);
char death[5];
itoa((*die), death, 10);
outtextxy(130, 40, death);
char zdnumts[16] = "目前剩余子弹量:";
outtextxy(170, 15, zdnumts);
char* zdnum = new char[4];
itoa(1800 - (*zd), zdnum, 10);
outtextxy(292, 15, zdnum);
char bloodts[16] = "飞机剩余生命值:";
outtextxy(170, 40, bloodts);
char* blood = new char[4];
itoa((100 - (10 * (*dang))), blood, 10);
outtextxy(300, 40, blood);
char bloo2dts[16] = "强化能量数值:";
outtextxy(340, 15, bloo2dts);
char* bloo2d = new char[11];
itoa((*fffzd), bloo2d, 10);
outtextxy(470, 15, bloo2d);
char toyts[16] = "终极技能数量:";
outtextxy(340, 40, toyts);
char* toynum = new char[11];
itoa((*toy), toynum, 10);
outtextxy(477, 40, toynum);
}
玩家
void Player(int* fffzd, int* n, mds* player, zmy* ball, int* die, int* dang)
{
PIMAGE fly = newimage();
if (keystate('S') && (*fffzd) > 0)//检测按下S,按下则载入变身图
getimage(fly, "fly2.png");
else if ((int)(fclock() * 10) % 2 == 0)//秒数乘以10判断奇偶,实现0.1s换一次飞机图
getimage(fly, "fly11.png");
else
getimage(fly, "fly12.png");
putimage_withalpha(NULL, fly, player->x, player->y);//飞机透明部分透明化
delimage(fly);//释放
for (int i = 0; i < *n; ++i)//撞机检测
{
if ((ball[i].x) < (player->x + 60) && (ball[i].y - 40) < (player->y) && (ball[i].y + 40) > (player->y) && (ball[i].x) > (player->x - 45))
{
ball[i].y -= 1800;//飞机返回重用
(*die) += 1;//击落数加一
(*dang) += 1;//撞击数加一
}
}
}
控制
void control(mds* player, double* tt, int* speed, double* zdspeed, int* zd, double* ttt, int* fffzd, int* n, int* die, zmy* ball, int* toy, int *dang)
{
if (keystate(VK_UP))// 上键按下了
{
if (player->y < 70) //碰墙检测
player->y += (player->dy+1 );//碰墙后退
player->y -= (player->dy-1);//飞机前进
while (kbhit())//检测缓冲区
{
getch();//清空键盘缓冲区
}
}
if (keystate(VK_DOWN))// 下键按下了
{
if (player->y >= 720)//碰墙检测
player->y -= (player->dy + 2);//碰墙前进
player->y += (player->dy+1);//飞机后退
while (kbhit())
{
getch();
}
}
if (keystate(VK_RIGHT)) // 右键按下了
{
if (player->x > (560))//碰墙检测
player->x -= (player->dx + 1);
player->x += player->dx;
while (kbhit())
{
getch();
}
}
if (keystate(VK_LEFT))// 左键按下了
{
if (player->x < (-30))//碰墙检测
player->x += (player->dx + 1);
player->x -= player->dx;
while (kbhit())
{
getch();
}
}
if (keystate('A') && !keystate('S')&&(*zd<1800))// A按下,S
{
if (fclock() - *tt > 0.15)//开火间隔0.15s
{
(*tt) = fclock();
*speed = *zdspeed;//子弹飞行速度复位
(*zd)++;//子弹数加一
}
while (kbhit())
{
getch();
}
}
fire(player, speed, ttt, zd, fffzd);//s强化子弹
if (keystate(VK_SPACE) && (*toy) != 0)// 按下空格开大
{
(*toy)--;//大招减一
bug(n, ball, die);//大招函数
}
if (keystate('C'))// 按下C充值
{
buy(zdspeed, fffzd, toy , dang);//充值
}
if (keystate('H'))// 按下H帮助
help();//帮助
}
#endif
素材的话我上传了csdn,使用前请记得下载合适的ege和vc运行库
//download.youkuaiyun.com/download/weixin_44544354/12041609