C++弹幕游戏自制分享
2022/2/6 9:42:28
本文主要是介绍C++弹幕游戏自制分享,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一个弹幕射击游戏,有两种模式,第一种模式是类似飞机大战,第二种模式是boss挑战,boss的弹幕后续可以追加多种类型,用的是easyx图形库
#include<iostream>
#include<conio.h>
#include"easyx.h"
#include<time.h>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<windows.h>
#include<Mmsystem.h>
#pragma comment(lib,"winmm.lib")
using namespace std;
enum first {
WIDTH = 1300,
HEIGHT = 800,
bulletnum = 2000,
BIG,
SMALL,
BOSS,
yixingtai,
erxingtai,
sanxingtai,
sixingtai,
wuxingtai,
liuxingtai,
qixingtai,
baxingtai,
jiuxingtai,
shixingtai
};
int enemynum;
struct Gamer//玩家
{
int attack;
double x;
double y;
bool live;//是否存活
int hp;
int type;
int height;
int width;
bool flag;
}player,bull[bulletnum], bull2[bulletnum], bull3[bulletnum],enemy[201],boss;
struct openwin{
int sco;
int chose;
bool ret;
int exen;
int spd;
}control;
ifstream ifs;
ofstream ofs;
IMAGE bk;
IMAGE img_role[2];
IMAGE img_bull[6];
IMAGE img_enemy[3][2];
IMAGE img_boss[2];
TCHAR s[1024];
void loadImg()//加载图片
{
loadimage(&bk, "./zhandoujiemian.png");
loadimage(&img_role[0], "./player1.png");
loadimage(&img_role[1], "./player2.png");
//加载子弹
loadimage(&img_bull[0], "./bullet1.png");
loadimage(&img_bull[1], "./bullet2.png");
loadimage(&img_bull[2], "./bullet3.png");
loadimage(&img_bull[3], "./bullet4.png");
loadimage(&img_bull[4], "./bullet5.png");
loadimage(&img_bull[5], "./bullet6.png");
loadimage(&img_enemy[0][0], "./enemy1.png");
loadimage(&img_enemy[0][1], "./enemy2.png");
loadimage(&img_enemy[1][0], "./enemy3.png");
loadimage(&img_enemy[1][1], "./enemy4.png");
loadimage(&img_enemy[2][0], "./enemy5.png");
loadimage(&img_enemy[2][1], "./enemy6.png");
loadimage(&img_boss[0], "./player1.png");
loadimage(&img_boss[1], "./bullet2.png");
}
bool Timer(int ms, int id) {//计时器
static DWORD t[20];
if (clock() - t[id] > ms) {
t[id] = clock();
return true;
}
return false;
}
//------------------------------------------------------------------------------------------------------------------------------------------------
class initanddraw {
public:
void enemyHp(int i) {
int randow1 = rand() % 10;
if (control.chose == 2) {
if (randow1 >= 0 && randow1 <= 2) {
enemy[i].type = BIG;
enemy[i].hp = 3;
enemy[i].width = 80;//碰撞体积-------------------
enemy[i].height = 80;
}//da
else if (randow1 >= 3 && randow1 <= 9) {
enemy[i].type = SMALL;
enemy[i].hp = 1;
enemy[i].width = 80;//碰撞体积-------------------
enemy[i].height = 80;
}
}
if (control.chose == 3) {
enemy[i].type = BOSS;
enemy[i].hp = 100;
enemy[i].width = 80;//碰撞体积-------------------
enemy[i].height = 80;
}
}
void gameinit() {
loadImg();
player.live = true;
player.hp = 3;
player.x = WIDTH / 4;
player.y = HEIGHT / 3;
player.width = 80;
player.height = 80;
for (int i = 0; i < bulletnum; i++) {
bull[i].x = 0;
bull[i].y = 0;
bull[i].live = false;
}
for (int j = 0; j < bulletnum; j++) {
bull2[j].x = 0;
bull2[j].y = 0;
bull2[j].live = false;
}
for (int k = 0; k < bulletnum; k++) {
bull3[k].x = 0;
bull3[k].y = 0;
bull3[k].live = false;
}
for (int i = 0; i < enemynum; i++) {
enemy[i].live = false;
enemyHp(i);
}
enemy[0].live = true;
enemy[0].x = 1350;
enemy[0].y = 200;
}
void xuetiaoboss() {
if (enemy[0].type == BOSS) {
setfillcolor(RED);
solidrectangle(300, 30, 300+enemy[0].hp*7, 40);
}
}
void xuetiaoplayer() {
setfillcolor(BLUE);
solidrectangle(100, 30, 100 + player.hp* 33, 40);
}
void gamedraw()//绘制
{
loadImg();
putimage(0, 0, &bk);
putimage(player.x, player.y, &img_role[1], NOTSRCERASE);//掩码图
putimage(player.x, player.y, &img_role[0], SRCINVERT);//源码图
putimage(player.x, player.y, &img_role[1], NOTSRCERASE);//掩码图
putimage(player.x, player.y, &img_role[0], SRCINVERT);//源码图
xuetiaoplayer();
for (int i = 0; i < bulletnum; i++) {
if (bull[i].live)
{
putimage(bull[i].x, bull[i].y, &img_bull[1], NOTSRCERASE);//掩码图
putimage(bull[i].x, bull[i].y, &img_bull[0], SRCINVERT);//源码图
putimage(bull[i].x, bull[i].y, &img_bull[1], NOTSRCERASE);//掩码图
putimage(bull[i].x, bull[i].y, &img_bull[0], SRCINVERT);//源码图
}
}
for (int j = 0; j < bulletnum; j++) {
if (bull2[j].live)
{
putimage(bull2[j].x, bull2[j].y, &img_bull[3], NOTSRCERASE);//掩码图
putimage(bull2[j].x, bull2[j].y, &img_bull[2], SRCINVERT);//源码图
putimage(bull2[j].x, bull2[j].y, &img_bull[3], NOTSRCERASE);//掩码图
putimage(bull2[j].x, bull2[j].y, &img_bull[2], SRCINVERT);//源码图
}
}
for (int i = 0; i < bulletnum; i++) {
if (bull3[i].live)
{
putimage(bull3[i].x, bull3[i].y, &img_bull[5], NOTSRCERASE);//掩码图
putimage(bull3[i].x, bull3[i].y, &img_bull[4], SRCINVERT);//源码图
putimage(bull3[i].x, bull3[i].y, &img_bull[5], NOTSRCERASE);//掩码图
putimage(bull3[i].x, bull3[i].y, &img_bull[4], SRCINVERT);//源码图
}
}
for (int i = 0; i < enemynum; i++) {
if (enemy[i].live) {
if (enemy[i].type == BIG) {
putimage(enemy[i].x, enemy[i].y, &img_enemy[1][1], NOTSRCERASE);
putimage(enemy[i].x, enemy[i].y, &img_enemy[1][0], SRCINVERT);
putimage(enemy[i].x, enemy[i].y, &img_enemy[1][1], NOTSRCERASE);
putimage(enemy[i].x, enemy[i].y, &img_enemy[1][0], SRCINVERT);
}
else if(enemy[i].type==SMALL){
putimage(enemy[i].x, enemy[i].y, &img_enemy[0][1], NOTSRCERASE);
putimage(enemy[i].x, enemy[i].y, &img_enemy[0][0], SRCINVERT);
putimage(enemy[i].x, enemy[i].y, &img_enemy[0][1], NOTSRCERASE);
putimage(enemy[i].x, enemy[i].y, &img_enemy[0][0], SRCINVERT);
}
else if (enemy[i].type == BOSS) {
xuetiaoboss();
putimage(enemy[i].x, enemy[i].y, &img_enemy[2][1], NOTSRCERASE);
putimage(enemy[i].x, enemy[i].y, &img_enemy[2][0], SRCINVERT);
putimage(enemy[i].x, enemy[i].y, &img_enemy[2][1], NOTSRCERASE);
putimage(enemy[i].x, enemy[i].y, &img_enemy[2][0], SRCINVERT);
}
}
}
}
}iad;
//------------------------------------------------------------------------------------------------------------------------------------------------
class zidanlei {
public:
void createbullet() {
for (int i = 0; i < bulletnum; i++) {
if (!bull[i].live) {
bull[i].x = player.x + 60;
bull[i].y = player.y + 50;
bull[i].live = true;
break;
}
}
}
void createbullet2() {
for (int j = 0; j < control.exen; j++) {
for (int i = 0; i < bulletnum; i++) {
if (!bull2[i].live) {
bull2[i].x = enemy[j].x+30;
bull2[i].y = enemy[j].y +50;
bull2[i].live = true;
break;
}
}
}
}
void createbullet3() {
if (boss.type == yixingtai || boss.type == erxingtai || boss.type == sanxingtai || boss.type == sixingtai || boss.type == liuxingtai) {
if (Timer(350, 4)) {
for (int i = 0; i < bulletnum; i++) {
if (!bull3[i].live)
{
bull3[i].x = enemy[0].x + 40;
bull3[i].y = enemy[0].y + 40;
bull3[i].live = true;
break;
}
}
}
}
if (boss.type == wuxingtai) {
if (Timer(50, 10)) {
for (int i = 0; i < 15; i++) {
if (!bull3[i].live)
{
bull3[i].x = enemy[0].x + 40;
bull3[i].y = enemy[0].y + 40;
bull3[i].live = true;
break;
}
}
}
}
if (boss.type == qixingtai) {
if (Timer(100, 11)) {
for (int i = 0; i < 24; i++) {
if (!bull3[i].live)
{
bull3[i].x = enemy[0].x + 40;
bull3[i].y = enemy[0].y + 40;
bull3[i].live = true;
break;
}
}
}
}
if (boss.type == baxingtai) {
if (Timer(100, 12)) {
for (int i = 0; i < 60; i++) {
if (!bull3[i].live)
{
if (i <= 20) {
bull3[i].x = enemy[0].x + 40;
bull3[i].y = enemy[0].y + 40;
}
if (i <= 40&&i> 20) {
bull3[i].x = enemy[0].x+40;
bull3[i].y = enemy[0].y + 80;
}
if (i <= 60 && i > 40) {
bull3[i].x = enemy[0].x + 40;
bull3[i].y = enemy[0].y;
}
bull3[i].live = true;
break;
}
}
}
}
}
void bulletmove() {
for (int i = 0; i < bulletnum; i++) {
if (bull[i].live) {
bull[i].x += 6;//子弹速度
if (bull[i].x > 1400)
{
bull[i].live = false;
}
}
}
}
void bulletmove2() {
for (int i = 0; i < bulletnum; i++) {
if (bull2[i].live) {
bull2[i].x -= 6;//子弹速度
if (bull2[i].x < -400)
{
bull2[i].live = false;
}
}
}
}
void bulletmove3() {
int zhuanhuan = 0;
if(Timer(8000, 7)) {
zhuanhuan = rand() % 8+1;
}
if (zhuanhuan == 1) {
boss.type = yixingtai;
}
if (zhuanhuan == 2) {
boss.type = erxingtai;
}
if (zhuanhuan == 3) {
boss.type = sanxingtai;
}
if (zhuanhuan == 4) {
boss.type = sixingtai;
}
if (zhuanhuan == 5) {
boss.type = wuxingtai;
}
if (zhuanhuan == 6) {
boss.type = liuxingtai;
}
if (zhuanhuan == 7) {
boss.type = qixingtai;
}
if (zhuanhuan == 8) {
boss.type = baxingtai;
}
if (zhuanhuan == 9) {
boss.type = jiuxingtai;
}
if (boss.type == yixingtai) {
for (int i = 0; i < bulletnum; i++) {
if (bull3[i].live) {
bull3[i].x -= 6;//子弹速度
if (bull3[i].x < -100 || bull3[i].y < -100 || bull3[i].y > 900 || bull3[i].x> 1400)
{
bull3[i].live = false;
}
}
}
}
else if (boss.type == erxingtai) {
for (int i = 0; i < bulletnum; i++) {
if (bull3[i].live) {
bull3[i].x -= 6;
bull3[i].y -= 3;
//子弹速度
if (bull3[i].x < -100 || bull3[i].y < -100 || bull3[i].y > 900 || bull3[i].x> 1400)
{
bull3[i].live = false;
}
}
}
}
else if (boss.type == sanxingtai) {
for (int i = 0; i < bulletnum; i++) {
if (bull3[i].live) {
bull3[i].x -= 6;
bull3[i].y += 3;
//子弹速度
if (bull3[i].x < -100 || bull3[i].y < -100 || bull3[i].y > 900 || bull3[i].x> 1400)
{
bull3[i].live = false;
}
}
}
}
else if (boss.type == sixingtai) {
for (int i = 0; i < 3; i++) {
if (bull3[i].live) {
bull3[i].x -= 15;
//子弹速度
if (bull3[i].x < -100 || bull3[i].y < -100 || bull3[i].y > 900 || bull3[i].x> 1400)
{
bull3[i].live = false;
}
}
}
}
else if (boss.type == wuxingtai) {
for (int i = 0; i < 15; i++) {
if (bull3[i].live) {
if (i>= 0&&i<5) {
bull3[i].x -= 6;
}
if (i >= 5 && i < 10) {
bull3[i].y -= 3;
bull3[i].x -= 5;
}
if (i >= 10 && i < 15) {
bull3[i].y += 3;
bull3[i].x -= 5;
}
if (bull3[i].x < -100 || bull3[i].y < -100 || bull3[i].y > 900 || bull3[i].x> 1400)
{
bull3[i].live = false;
}
}
}
}
else if (boss.type == liuxingtai) {
for (int i = 0; i < 10; i++) {
if (bull3[i].live) {
if (i < 5) {
bull3[i].x += 4;
}
if (i >= 5) {
bull3[i].x -= 6;
}
if (bull3[i].x < -100 || bull3[i].y < -100 || bull3[i].y > 900 || bull3[i].x> 1400)
{
bull3[i].live = false;
}
}
}
}
else if (boss.type == qixingtai) {
for (int i = 0; i < 24; i++) {
if (bull3[i].live) {
if (i >= 0 && i < 5) {
bull3[i].x -= 5;
}
if (i >= 5 && i < 10) {
bull3[i].y -= 3;
bull3[i].x -= 5;
}
if (i >= 10 && i < 15) {
bull3[i].y += 3;
bull3[i].x -= 5;
}if (i >= 15 && i <16) {
bull3[i].x += 5;
}
if (i >= 16 && i < 20) {
bull3[i].y -= 3;
bull3[i].x += 5;
}
if (i >= 20 && i < 24) {
bull3[i].y += 3;
bull3[i].x += 5;
}
if (bull3[i].x < -100 || bull3[i].y < -100 || bull3[i].y > 900 || bull3[i].x> 1400)
{
bull3[i].live = false;
}
}
}
}
else if (boss.type == baxingtai) {
for (int i = 0; i < 60; i++) {
if (bull3[i].live) {
if (i >= 0 && i < 20) {
bull3[i].x -= 6;
}
if (i >= 20 && i < 40) {
bull3[i].y -= 1;
bull3[i].x -= 6;
}
if (i >= 40 && i < 60) {
bull3[i].y += 1;
bull3[i].x -= 6;
}
if (bull3[i].x < -100 || bull3[i].y < -100 || bull3[i].y > 900 || bull3[i].x> 1400)
{
bull3[i].live = false;
}
}
}
}
}
}zidan;
//------------------------------------------------------------------------------------------------------------------------------------------------
void PlayerMove(int speed) {//操控
if (player.live == true) {
if (GetAsyncKeyState(VK_UP)) {
if (player.y > 0) {
player.y -= speed;
}
}
if (GetAsyncKeyState(VK_DOWN)) {
if (player.y < 700) {
player.y += speed;
}
}
if (GetAsyncKeyState(VK_LEFT)) {
if (player.x > 0) {
player.x -= speed;
}
}
if (GetAsyncKeyState(VK_RIGHT)) {
if (player.x < 1220) {
player.x += speed;
}
}
if (GetAsyncKeyState(VK_SPACE) && Timer(300, 1))
{
zidan.createbullet();
};
}
if(player.live==false) {
control.ret = false;
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------
class enemyfuct:public initanddraw, public zidanlei{
public:
void createenemy() {
for (int i = 0; i < enemynum; i++) {
if (!enemy[i].live && enemy[i].type!=BOSS) {
enemy[i].live = true;
enemy[i].x = 1350/*rand()%1450+1350*/;
enemy[i].y = rand() % (HEIGHT - 100);
enemyHp(i);
break;
}
else if (!enemy[i].live && enemy[i].type == BOSS) {
enemy[i].live = true;
enemy[i].x = 1350/*rand()%1450+1350*/;
enemy[i].y = rand() % (HEIGHT - 50);
enemyHp(i);
break;
}
}
}
void enemymove(int speed)
{
for (int i = 0; i < enemynum; i++)
{
if (enemy[i].live && enemy[i].type != BOSS) {
enemy[i].x -= speed;
if (Timer(1000, 3)) {
createbullet2();
}
if (enemy[i].x < -200)
{
player.hp -= 1;
enemy[i].live = false;
}
}
/// <summary>
/// ///
/// boss skill //
/// boss skill ///
/// boss skill
/// boss skill ///
///
/// </summary>
/// <param name="speed"></param>
if (enemy[i].live && enemy[i].type == BOSS) {
createbullet3();
if (enemy[i].x > 1100) {
enemy[i].x -= speed;
}
if (enemy[i].live && enemy[i].type == BOSS && enemy[i].x <= 1100 && enemy[i].y >= 600) {
boss.flag = true;
}
if (enemy[i].live && enemy[i].type == BOSS && enemy[i].x <= 1100 && enemy[i].y <= 100) {
boss.flag = false;
}
if (boss.flag == false) {
enemy[i].y += 2;
}
else {
enemy[i].y -= 2;
}
}
}
}
}enemyfun;
//------------------------------------------------------------------------------------------------------------------------------------------------
class diedpanding {
public:
int upsorce(openwin* p) {
p->sco += 1;
return p->sco;
}
int upsorce2(openwin* p) {
p->sco += 100;
return p->sco;
}
void diedenemy() {
for (int i = 0; i < enemynum; i++)
{
if (!enemy[i].live)
continue;
for (int k = 0; k < bulletnum; k++)
{
if (!bull[k].live)
continue;
if (bull[k].x > enemy[i].x && bull[k].x<enemy[i].x + enemy[i].width
&& bull[k].y>enemy[i].y && bull[k].y < enemy[i].y + enemy[i].height)
{
bull[k].live = false;
enemy[i].hp--;//掉血
}
}
if (enemy[i].hp <= 0)
{
control.exen -= 1;//存在的敌机数
enemy[i].live = false;
if (enemy[i].type == BOSS) {
upsorce2(&control);
settextcolor(YELLOW);
settextstyle(100, 0, "楷体");
setbkmode(TRANSPARENT);
outtextxy(300, 250, "游戏胜利");
_stprintf(s, _T("得分: %d"), control.sco);
outtextxy(300, 350, s);
control.ret = false;
}
if (enemy[i].type == SMALL) {
upsorce(&control);
}
if (enemy[i].type == BIG) {
upsorce(&control);
upsorce(&control);
upsorce(&control);
}
}
}
}
void diedgamer() {
for (int k = 0; k < bulletnum; k++)
{
if (enemy[0].type != BOSS) {
if (!bull2[k].live) {
continue;
}
if (bull2[k].x > player.x && bull2[k].x<player.x + player.width
&& bull2[k].y>player.y && bull2[k].y < player.y + player.height)
{
bull2[k].live = false;
player.hp--;//掉血
}
}
if (enemy[0].type == BOSS) {
if (!bull3[k].live) {
continue;
}
if (bull3[k].x > player.x && bull3[k].x<player.x + player.width
&& bull3[k].y>player.y && bull3[k].y < player.y + player.height)
{
bull3[k].live = false;
player.hp--;//掉血
}
}
if (player.hp <= 0)
{
settextcolor(RED);
settextstyle(100, 0, "楷体");
setbkmode(TRANSPARENT);
outtextxy(300, 250, "游戏结束");
_stprintf(s, _T("得分: %d"), control.sco);
outtextxy(300, 350, s);
Sleep(100);
player.live = false;
}
}
}
}died;
//------------------------------------------------------------------------------------------------------------------------------------------------
void dufenbiao() {
ifs.open("score.txt", ios::in);
char fen[1024];
while (ifs.getline(fen, sizeof(fen)))
{
settextcolor(YELLOW);
settextstyle(50, 0, "楷体");
setbkmode(TRANSPARENT);
outtextxy(0, 0, "分数表已打印在控制台");
cout << fen << endl;
}
Sleep(300);
ifs.close();
}
class shop {
public:
virtual void shopping()=0;
};
void shopping() {
settextcolor(YELLOW);
settextstyle(50, 0, "楷体");
setbkmode(TRANSPARENT);
outtextxy(0, 0, "还没开业");
Sleep(300);
}
int Openwindows() {
IMAGE bk2;
PlaySound("bgm1.wav", NULL, SND_FILENAME | SND_ASYNC |SND_LOOP);
loadimage(&bk2, "./Fengmian.png");
control.chose = 0;
control.sco = 0;
while (1) {
putimage(0, 0, &bk2);
char input;
if (kbhit())
{
input = getch();
if ((input == 'q')|| (input == 'Q')) {
enemynum = 200;
control.chose = 2;
PlaySound("bgm2.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
break;
}
if ((input == 'w') || (input == 'W')) {
enemynum = 1;
control.chose = 3;
PlaySound("bgm3.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
break;
}
if ((input == 'e') || (input == 'E')) {
dufenbiao();
}
if ((input == 'r') || (input == 'R')) {
shopping();
}
if ((input == 't') || (input == 'T'))
{
exit(10);
break;
}
}
}
}
int Fenshubiao(openwin *p) {
ofs.open("score.txt", ios::out|ios::app );
ofs <<"得分 " << control.sco <<endl;
ofs.close();
return p->sco;
}
//------------------------------------------------------------------------------------------------------------------------------------------------
int main()
{
initgraph(WIDTH, HEIGHT, EW_SHOWCONSOLE);
boss.flag = false;
srand((unsigned)time(NULL));
loop:while (1) {
control.ret = true;
Openwindows();
control.exen = 0;
control.spd = 1;
iad.gameinit();
BeginBatchDraw();
while (1) {
iad.gamedraw();
FlushBatchDraw();
PlayerMove(4);
zidan.bulletmove();
if (Timer(1800, 0))
{
enemyfun.createenemy();
control.exen += 1;//存在的敌机
}
if (Timer(20000, 5) && control.spd < 6) {
control.spd += 1;
}
enemyfun.enemymove(control.spd);
zidan.bulletmove2();
zidan.bulletmove3();
died.diedenemy();
died.diedgamer();
if (control.ret == false) {
break;
}
}
EndBatchDraw();
Sleep(1000);
cleardevice();
Fenshubiao(&control);
goto loop;
}
return 0;
}
这篇关于C++弹幕游戏自制分享的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享