C++飞机大战 easyX

在这里插入图片描述

#include<graphics.h>
#include<conio.h>
#include <mmsystem.h>
#include<Windows.h>
#include<math.h>
#include<stdio.h>

#pragma comment(lib,"Winmm.lib")

class preGraph {
    
    
public:
	preGraph(int w, int h)
	{
    
    
		initgraph(w, h);
		BeginBatchDraw();
	}
	~preGraph()
	{
    
    
		EndBatchDraw();
		closegraph();
	}
};
IMAGE img_bk;//背景
IMAGE img_plane_mask;
IMAGE img_plane;//飞机
IMAGE img_bullet;//子弹
IMAGE img_bullet_mask;
IMAGE img_enemyplane;//敌机
IMAGE img_enemyplane_mask;
int width;//窗口大小
int height;
int score;
int planeX, planeY;//飞机的坐标
int planeWidth, planeHeight;//飞机宽高
int bulletX, bulletY;//子弹坐标
int enemy_x, enemy_y;//敌机位置
int enemyWidth, enemyHeight;//敌机宽高
int speed;
bool isExplode;
bool bulletExit;

void start()
{
    
    
	
	mciSendString("open .\\飞机大战图片音乐素材\\game_music.mp3 alias bkmusic", NULL, 0, NULL);
	mciSendString("play bkmusic repeat", NULL, 0, NULL);

	loadimage(&img_bk, ".\\飞机大战图片音乐素材\\background.jpg");
	width = img_bk.getwidth();
	height = img_bk.getheight();
	
	loadimage(&img_plane_mask, ".\\飞机大战图片音乐素材\\planeNormal_1.jpg");
	loadimage(&img_plane, ".\\飞机大战图片音乐素材\\planeNormal_2.jpg");
	planeHeight = img_plane.getheight();
	planeWidth = img_plane.getwidth();

	loadimage(&img_bullet_mask, ".\\飞机大战图片音乐素材\\bullet1.jpg");
	loadimage(&img_bullet, ".\\飞机大战图片音乐素材\\bullet2.jpg");

	loadimage(&img_enemyplane, ".\\飞机大战图片音乐素材\\enemyPlane2.jpg");
	loadimage(&img_enemyplane_mask, ".\\飞机大战图片音乐素材\\enemyPlane1.jpg");
	enemyHeight = img_enemyplane.getheight();
	enemyWidth = img_enemyplane.getwidth();

	planeX = width * 0.5 + 10;
	planeY = height * 0.5 + 10;

	enemy_x = width * 0.5;
	enemy_y = 0;

	bulletY = planeY;
	bulletX = planeX;

	isExplode = false;
	bulletExit = false;

	score = -1;
}
void show()
{
    
    
	putimage(0, 0, &img_bk, SRCCOPY);
	putimage(planeX, planeY, &img_plane_mask, NOTSRCERASE);
	putimage(planeX, planeY, &img_plane, SRCINVERT);
	
	putimage(enemy_x, enemy_y, &img_enemyplane_mask, NOTSRCERASE);
	putimage(enemy_x, enemy_y, &img_enemyplane, SRCINVERT);

	putimage(bulletX, bulletY, &img_bullet_mask, NOTSRCERASE);
	putimage(bulletX, bulletY, &img_bullet, SRCINVERT);
	
	settextcolor(BLUE);
	TCHAR s[] = _T("当前积分:");
	outtextxy(10, height - 200, s);
	TCHAR s_12[5];
	_stprintf_s(s_12, _T("%d"), score);
	outtextxy(90, height - 200, s_12);
	FlushBatchDraw();
	Sleep(10);
}
void updateWithoutInput()
{
    
    
	bulletY -= 3;//子弹向上飞
	if (bulletY <= 0) {
    
    
		bulletExit = false;
	}
	if (enemy_y < height ) {
    
    //敌机向下飞
		enemy_y += 2;
	}
	else {
    
    
		enemy_y = 0;//上下为一个周期,与flappy里的柱子左右一个周期同理
	}
	if (bulletX >= enemy_x && bulletX <= enemy_x + enemyWidth && 
		bulletY >= enemy_y && bulletY <= enemy_y + enemyHeight) {
    
    //判定子弹和敌机是否碰撞
		enemy_x = rand() % width;
		enemy_y = -40;
		bulletY = -85;
		score++;

		bulletExit = false;
	}
	if (abs(planeX - enemy_x) < planeWidth/2 + enemyWidth/2 - 10 && 
		abs(planeY - enemy_y) < planeHeight/2 + enemyHeight/2 - 10) {
    
    //判定飞机和敌机是否发生碰撞
		isExplode = true;
	}


}
void updateWithInput()
{
    
    
	char input;
	if (_kbhit())
	{
    
    
		input = _getch();//键盘控制
		switch (input)
		{
    
    
		//需要注释掉鼠标控制才可使用键盘控制飞机移动
		/*case 'a':planeX -= speed; break;
		case 's':planeY += speed; break;
		case 'd':planeX += speed; break;
		case 'w':planeY -= speed; break;*/
		case ' ':
			if (bulletExit == false) {
    
    
				bulletExit = true;
				bulletX = planeX + img_plane.getwidth() / 2 - 17;
				bulletY = planeY - img_bullet.getheight() + 5;
			}
		}
	}
	ExMessage m;//新版写法
	while (peekmessage(&m, EM_MOUSE, PM_NOREMOVE))
	{
    
    
		m = getmessage();
		if (m.message == WM_MOUSEMOVE)//飞机跟随鼠标
		{
    
    
			planeX = m.x - (planeWidth / 2 - 5);
			planeY = m.y - (planeHeight / 2 + 30);
		}
		if (m.message == WM_LBUTTONDOWN && bulletExit == false)//左键发射子弹
		{
    
    
			bulletExit = true;
			bulletX = planeX + img_plane.getwidth() / 2 - 17;
			bulletY = planeY - img_bullet.getheight() + 5;
		}
	}


}

void lose() {
    
    
	EndBatchDraw();
	cleardevice();
	char str[] = "Game over!";
	outtextxy(width / 2 - 10, height / 2, str);
	_getch();
	closegraph();
}

int main()
{
    
    
	start();
	preGraph initgp(width, height);
	while (1)
	{
    
    
		show();
		updateWithoutInput();
		updateWithInput();
		if (isExplode == true) {
    
    
			lose();
		}
	}
	_getch();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_45311187/article/details/120932930