#include<graphics.h>//easyx图形库 #include<iostream> #include<conio.h> using namespace std; IMAGE bk;//指向背景图 IMAGE img_player[2];//指向飞机 IMAGE img_bullet[2];//指向子弹 IMAGE img_enemy[2][2];//指向敌机 int thecount = 0; int thatcount = 0; enum theWindows//存储窗口的属性 { WIDTH = 640,//宽度 HEIGHT = 700,//高度 BULLET_NUM = 15,//子弹数量 ENEMY_NUM=10,//敌机数量 BIG,//大飞机 SMALL//小飞机 }; class Plane { public: int x;//横坐标 int y;//纵坐标 bool live;//判断飞机是否存活 int width; int height; int hp; int type; }player,bull[BULLET_NUM],enemy[ENEMY_NUM]; void loadImage()//加载图片 { //这里因为unicode字符集会报错,所以我把字符集改成了多字节字符集 //加载背景 loadimage(&bk, "C:/Users/pz001/Desktop/陈俊杰/飞机大战/images/background.jpg"); //加载玩家 loadimage(&img_player[1], "C:/Users/pz001/Desktop/陈俊杰/飞机大战/images/role.png");//黑底彩图 loadimage(&img_player[0], "C:/Users/pz001/Desktop/陈俊杰/飞机大战/images/role_1.png");//白底黑图 //加载子弹 loadimage(&img_bullet[1], "C:/Users/pz001/Desktop/陈俊杰/飞机大战/images/bullet.png");//黑底彩图 loadimage(&img_bullet[0], "C:/Users/pz001/Desktop/陈俊杰/飞机大战/images/bullet_1.png");//白底黑图 //加载敌机 loadimage(&img_enemy[0][1], "C:/Users/pz001/Desktop/陈俊杰/飞机大战/images/enemy.png");//黑底彩图 loadimage(&img_enemy[0][0], "C:/Users/pz001/Desktop/陈俊杰/飞机大战/images/enemy_1.png");//白底黑图 loadimage(&img_enemy[1][1], "C:/Users/pz001/Desktop/陈俊杰/飞机大战/images/enemy2.png");//黑底彩图 loadimage(&img_enemy[1][0], "C:/Users/pz001/Desktop/陈俊杰/飞机大战/images/enemy2_1.png");//白底黑图 } bool Timer(int ms, int id)//计时器 { static DWORD time1[10]; if (clock() - time1[id] > ms) { time1[id] = clock(); return true; } return false; } void button(int x,int y,int w,int h) { setfillcolor(BROWN); fillroundrect(x,y,x+w,y+h,10,10); outtextxy(x, y, "button"); } void enemyType(int i)//敌机类型以及数据初始化 { static unsigned times; times = time(NULL); srand(times);//把种子随机 if (rand() % 10 > 5) { enemy[i].type = BIG; enemy[i].hp = 3; enemy[i].width = 200; enemy[i].height = 200; } else { enemy[i].type = SMALL; enemy[i].hp = 1; enemy[i].width = 100; enemy[i].height = 73; } } void CreateEnemy()//敌机激活 { for (int i = 0; i < ENEMY_NUM; i++) { if (!enemy[i].live) { enemyType(i); enemy[i].x = rand() % (WIDTH-60); enemy[i].y = -enemy[i].height; enemy[i].live = true; break; } } } void EnemyMove(int speed) //敌机移动 { for (int i = 0; i < ENEMY_NUM; i++) { if (enemy[i].live) { enemy[i].y += speed; if (enemy[i].y > HEIGHT) { enemy[i].live = false; player.hp--; thecount++; } } } } void CreateBullet()//子弹激活 { for (int i = 0; i < BULLET_NUM; i++) { if (!bull[i].live) { bull[i].x = player.x+45; bull[i].y = player.y; bull[i].live = true; break; } } } void BulletMove(int speed)//子弹运动 { for (int i = 0; i < BULLET_NUM; i++) { if (bull[i].live) { bull[i].y -= speed; if (bull[i].y < -5)//子弹超出地图边界则自毁 { bull[i].live = false; } } } } void GameInit()//游戏初始化 { loadImage();//加载图片 setbkmode(TRANSPARENT);//去除字体背景色 settextcolor(BLACK);//统一字体颜色黑色 /* * 初始化飞机 */ player.x = WIDTH / 2; player.y = HEIGHT - 76; player.hp = 5; player.live = true; /* * 初始化子弹 */ for (int i = 0; i < BULLET_NUM; i++) { bull[i].x = 0; bull[i].y = 0; bull[i].live = false; } /* * 初始化敌机 */ for (int i = 0; i < ENEMY_NUM; i++) { enemy[i].live = false; } } void GameDraw()//绘制游戏 { loadImage(); putimage(0, 0, &bk);//布置背景 char playerHP[10],playerLose[10],playerShoot[10]; sprintf_s(playerHP,"%d",player.hp); sprintf_s(playerLose, "%d", thecount); sprintf_s(playerShoot, "%d", thatcount); outtextxy(0, 0, "生命值:"); outtextxy(60, 0, playerHP); outtextxy(0, 20, "遗落数:"); outtextxy(60, 20,playerLose); outtextxy(0, 40, "击落数:"); outtextxy(60, 40, playerShoot); //布置飞机 putimage(player.x, player.y, &img_player[0],SRCAND); putimage(player.x, player.y, &img_player[1],SRCPAINT); //布置子弹 for (int i = 0; i < BULLET_NUM; i++) { if (bull[i].live) { putimage(bull[i].x, bull[i].y, &img_bullet[0], SRCAND); putimage(bull[i].x, bull[i].y, &img_bullet[1], SRCPAINT); } } //布置敌机 for (int i = 0; i < ENEMY_NUM; i++) { if (enemy[i].live) { if (enemy[i].type == SMALL) { putimage(enemy[i].x, enemy[i].y, &img_enemy[0][0], SRCAND); putimage(enemy[i].x, enemy[i].y, &img_enemy[0][1], SRCPAINT); } else { putimage(enemy[i].x, enemy[i].y, &img_enemy[1][0], SRCAND); putimage(enemy[i].x, enemy[i].y, &img_enemy[1][1], SRCPAINT); } } } } //角色移动,获取键盘消息,上下左右 void PlayerControl(int speed) { #if 0 if (_kbhit())//用于解决阻塞问题 { char key = _getch(); //1._getch() 阻塞函数,和scanf一样,如果没有输入就会卡主程序,一直等待输入,并且这个函数不是C语言标准函数,需要包含头文件<conio.h> switch (key) { case 'w': case 'W': player.y -= speed; break; case 's': case 'S': player.y += speed; break; case 'a': case 'A': player.x -= speed; break; case 'd': case 'D': player.x += speed; break; } } #elif 1 //使用windows函数获取键盘输入GetAsyncKeyState,非常丝滑,不是阻塞函数 if (GetAsyncKeyState(VK_UP)) { if (player.y > 0)//如果超出上边界,则不能向上移动 { player.y -= speed; } } if (GetAsyncKeyState(VK_DOWN)) { if (player.y < HEIGHT -75)//如果超出下边界,则不能向下移动 { player.y += speed; } } if (GetAsyncKeyState(VK_LEFT))//如果超出左边界,则不能向左移动 { if (player.x > -50) { player.x -= speed; } } if (GetAsyncKeyState(VK_RIGHT))//如果超出右边界,则不能向右移动 { if (player.x < WIDTH -50) { player.x += speed; } } #endif if (Timer(500, 0) && GetAsyncKeyState(VK_SPACE))//若按下空格则发射子弹 { CreateBullet(); } } void playGame()//游戏机制 { for (int i = 0; i < ENEMY_NUM; i++) { if (!enemy[i].live) { continue; } for (int j = 0; j < BULLET_NUM; j++) { if (!bull[j].live) { continue; } if (bull[j].x>enemy[i].x&&bull[j].x<enemy[i].x+enemy[i].width&& bull[j].y>enemy[i].y && bull[j].y < enemy[i].y + enemy[i].height) { bull[j].live = false; enemy[i].hp--; } } if (player.x > enemy[i].x && player.x<enemy[i].x + enemy[i].width && player.y>enemy[i].y && player.y < enemy[i].y + enemy[i].height)//碰撞 { enemy[i].live = false; player.hp--; } if (enemy[i].hp <= 0) { enemy[i].live = false; thatcount++; } if (player.hp <= 0) { player.live = false; } } } int main() { //创建一个窗口,之前都是控制台的窗口,现在要有一个可视化图形窗口这里我使用的是easyx图形库 initgraph(WIDTH, HEIGHT,SHOWCONSOLE); GameInit(); //双缓冲绘图 BeginBatchDraw(); while (true) { GameDraw(); FlushBatchDraw(); PlayerControl(5); BulletMove(3); if (Timer(1500, 1)) { CreateEnemy(); } EnemyMove(2); playGame(); if (!player.live) { break; } } EndBatchDraw(); cout << "击落数:"<<thatcount<<endl; cout << "遗漏数:" << thecount << endl; char c; cin >> c; return 0; }
注:图形库用的easyx,编译器用的VS2022,素材都是自己扣的
其中“黑底彩图”类似这种
白底黑图是类似这种
因为需要用到透明拼贴我都是用PS扣的素材
源码中
loadimage(&img_enemy[0][1], "C:/Users/pz001/Desktop/陈俊杰/飞机大战/images/enemy.png");//黑底彩图标签:enemy,课程设计,img,int,大战,player,live,bull,大全 From: https://www.cnblogs.com/nashacjj/p/17504596.html
字符串中的是路径,用于寻找图片,大家需要自己重新编译