飞机大战游戏源码

大家好,我是业余写来玩玩的,写不好的地方不要取笑哦!

自定义头文件WinMain.h源码

#include <Windows.h>
#include<list>
#include<time.h>
#include<ctime>
#include "resource.h"
#include "resource1.h"
#include <gdiplus.h>
#include <mmsystem.h>
#pragma comment(lib,"gdiplus.lib")
#pragma comment(lib,"Msimg32.lib")
#pragma comment(lib,"Winmm.lib")
using namespace Gdiplus;
using namespace std;


#define CLIENTWIDTH 400
#define CLIENTHEIGHT 600
#define TIMERID_1 10			 //定时器ID
#define TIMERID_2 20			 //定时器ID
#define GAMEFRAME 180         //游戏帧
#define VE_NPCONE 1          //一号敌机速度
#define VE_NPCTWO 1          //二号敌机速度
#define VE_NPCTRE 1          //三号敌机速度
#define MENU_ITEM_WIDTH 120	 //菜单宽度
#define MENU_ITEM_HEIGHT 40  //菜单高度
#define RATIO 0.1			 //碰撞矩形系数

enum GAMESCENE//游戏场景
{
   
	SCENE_START,
	SCENE_PLAY,
	SCENE_OVER
};

enum ENEMYTYPE//敌机类型
{
   
	NPCONE,
	NPCTWO,
	NPCTRE,
	SUPNPCONE
};

enum BULLETTYPE//子弹类型
{
   
	BULLETONE,
	BULLETTWO
};

enum MENUTYPE
{
   
	SETFOCUS,
	KILLFOCUS
};

enum AWARD
{
   
	AWARDONE,
	AWARDTWO
};

HBITMAP hBitmap_start, hBitmap_play, hBitmap_over;
HBITMAP hBitmap_npc1, hBitmap_npc2, hBitmap_npc3, hBitmap_player, hBitmap_bullet1, hBitmap_bullet2, hBitmap_supnpc1, hBitmap_award1, hBitmap_award2, hBitmap_bomb;
GAMESCENE curScene;//游戏场景
Point point;//记录玩家飞机轨迹
Rect PlayerCollisionRect;

int player_xPos, player_yPos;//玩家位置
int collision_maxY, collision_minY, collision_maxX, collision_minX;//玩家碰撞检测
int index;//玩家爆炸动画
bool death;//玩家死亡
bool isdeath;//玩家被撞击
int flag, flag_bullet;//指向生成敌机类型
bool upgrade;//子弹升级
int upgrade_num;//二级子弹数量
bool Bomb;//炸弹
int Bomb_num;//炸弹数量
clock_t tmp;



void GameInit();//初始化
void LoadImg();//加载位图
void GamePaint(HDC hdc);//游戏界面重绘
void ReleaseRes();//释放资源
void DelImage();//删除位图资源
void MouseClick(int x,int y,HWND hwnd);//鼠标点击事件
void DrawStartOver(HDC hdc, HBITMAP hBmp);//重绘开始结束场景
void DrawPlay(HDC hdc);//重绘玩游戏场景
void GameLogic();//游戏逻辑帧
void DrawMenu(HDC hdc, RectF rect, MENUTYPE type, PTCHAR text);//输出菜单
void CreateNpc(HBITMAP hbmp);//生成敌机
void CreateBullet(HBITMAP hbmp);//生成敌机
void IsHit();//子弹是否击中敌机
void IsFocus(int x, int y);//是否获得焦点
void CreateAward(HBITMAP hbmp);//生成奖励
bool isCollsionWithRect(Rect rect1, Rect rect2);//碰撞检测
void CreateBomb(HBITMAP hbmp);//生成炸弹
void BigBang();//飞机爆炸声
void LoadSound();//加载音频
void CloseSound();//关闭音频
void Update(HDC hdc, HDC hdcMem, HDC hdcMemTemp, BLENDFUNCTION dc_bf);//游戏画面帧

class EnemyPlane //敌机类
{
   
private:

public:

	double PosX;
	double PosY;
	int Blood;
	int collision_maxY, collision_minY, collision_maxX, collision_minX;
	int index;
	bool isdeath;
	bool death;
	Rect ColRect;
	HBITMAP Type;
	ENEMYTYPE enemyType;
	EnemyPlane(HBITMAP h)
	{
   
		Type = h;
		if (h == hBitmap_npc1)
		{
   
			enemyType = NPCONE;
			Blood = 2;
			index = 0;
			isdeath = death = false;
		}
		if (h == hBitmap_npc2)
		{
   
			enemyType = NPCTWO;
			Blood = 6;
			index = 0;
			isdeath = death = false;
		}
		if (h == hBitmap_npc3)
		{
   
			enemyType = NPCTRE;
			PosY = -245;
			Blood = 20;
			index = 0;
			isdeath = death = false;
		}
		if (h == hBitmap_supnpc1)
		{
   
			enemyType = SUPNPCONE;
			Blood = 3;
			index = 0;
			isdeath = death = false;
		}
	}

	void CollsionRect(HBITMAP Type, Rect &ColRect)//碰撞矩形
	{
   
		BITMAP bmp;
		GetObject(Type, sizeof(BITMAP), &bmp);
		ColRect.X = PosX + bmp.bmWidth/4*RATIO;
		ColRect.Y = PosY + bmp.bmHeight*RATIO;
		ColRect.Width = bmp.bmWidth/4 - bmp.bmWidth/4*RATIO*2;
		ColRect.Height = bmp.bmHeight - bmp.bmHeight*RATIO*2;
	}

	double Vel()//返回敌机速度
	{
   
		switch (enemyType)
		{
   
		case NPCONE:
			return 4;
			break;
		case NPCTWO:
			return 1;
			break;
		case NPCTRE:
			return 0.5;
			break;
		case SUPNPCONE:
			return 2;
			break;
		default:
			break;
		}
	}
};
list <EnemyPlane> npcList;//储存敌机实例链表
list <EnemyPlane>::iterator iter_npc;//迭代器访问链表

class Bullet //子弹类
{
   
private:

public:
	int PosX;
	int PosY;
	int velocity = 20;
	int tmp;//子弹碰撞点偏移距离
	Rect ColRect;
	HBITMAP Type;
	BULLETTYPE bulletType;
	Bullet(HBITMAP h)
	{
   
		Type = h;
		if (h == hBitmap_bullet1)
		{
   
			bulletType = BULLETONE;
		}
		if (h == hBitmap_bullet2)
		{
   
			bulletType = BULLETTWO;
		}
	}

	void CollsionRect(HBITMAP Type, Rect &ColRect)
	{
   
		BITMAP bmp;
		GetObject(Type, sizeof(BITMAP), &bmp);
		ColRect.X = PosX + bmp.bmWidth*RATIO;
		ColRect.Y = PosY + bmp.bmHeight*RATIO;
		ColRect.Width = bmp.bmWidth - bmp.bmWidth*RATIO*2;
		ColRect.Height = bmp.bmHeight - bmp.bmHeight*RATIO*2;
	}
};
list <Bullet> bulletList;//储存子弹实例链表
list <Bullet>::iterator iter_bullet;

class Menu//菜单类
{
   
public:
	RectF rect;
	MENUTYPE menuType = KILLFOCUS;
	PTCHAR menuText;
	Menu(int x, int y, PTCHAR text)
	{
   
		rect.X = x;
		rect.Y = y;
		rect.Width = MENU_ITEM_WIDTH;
		rect.Height = MENU_ITEM_HEIGHT;
		menuText = text;
	};
private:

};
list <Menu> menuList_Start;
list <Menu> menuList_End;
list <Menu>::iterator iter_menu;

class Award//奖励类
{
   
public:
	double PosX;
	double PosY;
	double velocity = 0.8;
	Rect ColRect;
	HBITMAP Type;
	AWARD awardType;
	Award(HBITMAP h)
	{
   
		Type = h;
		if (h == hBitmap_award1)
		{
   
			awardType = AWARDONE;
		}
		if (h == hBitmap_award2)
		{
   
			awardType = AWARDTWO;
		}
	}

	void CollsionRect(HBITMAP Type, Rect &ColRect)
	{
   
		BITMAP bmp;
		GetObject(Type, sizeof(BITMAP), &bmp);
		ColRect.X = PosX + bmp.bmWidth*RATIO;
		ColRect.Y = PosY + bmp.bmHeight*RATIO;
		ColRect.Width = bmp.bmWidth - bmp.bmWidth*RATIO*2;
		ColRect.Height = bmp.bmHeight - bmp.bmHeight*RATIO*2;
	}
private:

};
list <Award> awardList;//储存奖励实例链表
list <Award>::iterator iter_award;

class Bomb_//炸弹类
{
   
public:
	int PosX;
	int PosY;
	HBITMAP Type;

	Bomb_(HBITMAP h)
	{
   
		PosX = 0;
		PosY = CLIENTHEIGHT - 36;
		Type = h;
	}
private:

};
list <Bomb_> bombList;//储存炸弹实例链表
list <Bomb_>::iterator iter_bomb;

主程序源文件WinMain.cpp源码

#include"WinMain.h"


LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//第1个参数,是消息要传递的窗口,参数2是消息的id(也就是这是个什么样的消息,比如鼠标左键按下对应的消息是WM_LBUTTONDOWN),参数3和参数4是消息的具体内容

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
   
	static TCHAR szWndClassName[] = TEXT("hellowin");
	HWND hwnd;//窗口句柄
	MSG msg;//消息类型
	WNDCLASS wndclass;//窗口类

	//GDI+初始化
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR           gdiplusToken;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

	wndclass.style = CS_HREDRAW | CS_VREDRAW;//窗口的风格,这里两个参数的意思是水平和垂直方向改变时重绘窗口
	wndclass.lpfnWndProc = WndProc;//窗口过程
	wndclass.cbClsExtra = 0;//额外分配的内存
	wndclass.cbWndExtra = 0;//额外分配的内存
	wndclass.hInstance = hInstance;//实例句柄
	wndclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));//设置图标,使用自定义图标第一个参数为实例句柄,第二个参数调用MAKEINTRESOURCE(图标ID)
	wndclass.hCursor = LoadCursor(NULL
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值