基于C++的vs2012,win32游戏设计——僵尸

首先说一句、博主是一名大二萌新,一直希望写一篇属于自己的博客,今天终于实现了自己的愿望(笑),由于博主是第一次写博客,难免有不少的错误,如有不足还请各位大佬多多指教。

1.前言
首先,游戏之中的图片是我用Photoshop制作而成,地图是我下了一个植物大战僵尸的游戏解了它的安装包提取其中的资源,主角的图片其实是当时我的恶趣味(就是我的照片啦),植物和子弹也是我从万能的度娘上找到的,下面来看一下实际的效果图。

2.开始界面
基于C++的vs2012,win32游戏设计——僵尸_第1张图片

ps:当时看龙族有点入迷,于是贴上了这么中二的话,现在看看好羞耻。。。

3.游戏界面

ps:主角可以根据键盘自由移动,一共有三种植物(当然你可以接着加),每次移动主角都会换一下图,这样看起来比较像在走路(笑)

4.三大植物


ps:战车会对主角直接秒杀,主角吃掉炮台和向日葵会加分,炮台只会在最后一排生成并会发射子弹,子弹打主角十下主角也会死,向日葵随机在地图上出现但概率最低。。。
下面来看一下代码,为了节省篇幅,我只给出重要的代码。
5.背景类

#include "Back.h"

CBack::CBack(void)
{
 m_nBmpBack = 0;
 m_nBmpBegin = 0;
  x = 0;
  y = 0;
}
void CBack::BackShow(HDC hMemDC)//贴图函数‎()
{
    HDC hdc = ::CreateCompatibleDC(hMemDC); 
 ::SelectObject(hdc,m_nBmpBack);
 ::BitBlt(hMemDC,x,y-600,1200,600,hdc,0,0,SRCCOPY);
 ::SelectObject(hdc,m_nBmpBegin);
 ::BitBlt(hMemDC,x,y,1200,600,hdc,0,0,SRCCOPY);

 ::DeleteDC(hdc);
 }
 void CBack::BackInit(HINSTANCE hIns)//初始化图片(‎)
{
 m_nBmpBack = ::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_MAP));
 m_nBmpBegin = ::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_BEGIN));
}
void CBack::BackMove()//移动图片
{
  x = 0;
     y = 600;
}

6.主角类

#include "Player.h"
CPlayer::CPlayer(void)
{
  m_nBmpPlayer1 = 0;
  m_nBmpPlayer2 = 0;
  m_nBlood = 10;
  x = 860-1200;
  y = 160-600;
}
CPlayer::~CPlayer(void)
{
 ::DeleteObject(m_nBmpPlayer1);
 ::DeleteObject(m_nBmpPlayer2);
}
bool CPlayer::ISHitGunner(CGunner* pGunner)//是否被炮弹击中‎()
{
 if(pGunner->x+43 >= this->x+40 &&pGunner->x+43 <= this->x+100 && pGunner->y >= this->y+5 &&pGunner->y <= this->y+120)
  return true;
  if(pGunner->x >= this->x+40 &&pGunner->x <= this->x+100 && pGunner->y >= this->y+5 &&pGunner->y <= this->y+120)
  return true;
 if(pGunner->x+43 >= this->x+40 &&pGunner->x+43 <= this->x+100 && pGunner->y+40 >= this->y+5 &&pGunner->y+40 <= this->y+120)
  return true;
  if(pGunner->x >= this->x+40 &&pGunner->x <= this->x+100 && pGunner->y+40 >= this->y+5 &&pGunner->y+40 <= this->y+120)
  return true;
 else
 return false;
}
void CPlayer::PlayerInit(HINSTANCE hIns)//初始化函数‎()
{
 m_nBmpPlayer1 = ::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_ZOMBIE1));
 m_nBmpPlayer2 = ::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_ZOMBIE11));
}
void CPlayer::PlayerMove(int FX)//移动函数‎()
{
 if(FX == VK_LEFT)
 {
  if(x > 0)
  x-=34;
 }
 if(FX == VK_RIGHT)
 {
  if(x < 1200)
   x+=34;
   }
 if(FX == VK_UP)
 {
  if(y > 0)
   y-=40;
 }
 if(FX == VK_DOWN)
 {
  if(y < 450)
   y+=40;
 }
}
void CPlayer::PlayerShow(HDC hMemDC)//显示函数‎()
{
HDC hdc = ::CreateCompatibleDC(hMemDC);
::SelectObject(hdc,m_nBmpPlayer1);
::TransparentBlt(hMemDC,x,y,155,120,hdc,0,0,155,120,RGB(255,255,255));
::SelectObject(hdc,m_nBmpPlayer2);
 ::TransparentBlt(hMemDC,x,y+600,155,120,hdc,0,0,155,120,RGB(255,255,255));
 ::DeleteDC(hdc);
 }
 void CPlayer::DownBlood()//掉血‎()
{
 m_nBlood--;
}
void CPlayer::BackMove()//移动图片
{
 x = 860;
 y = 160;
}
void CPlayer::PlayerChange(int n)
{
 if(n%2 == 0)
 {
 x=x;
  y=y+600;
 }
 else
 {
  x=x;
  y=y-600;
 }
}
bool CPlayer::IsBoom()//死亡()
{
 if(m_nBlood == 0)
 {
  return true;
  }
 return false;
}

7.炮台类

#include "NanGua.h"
CNanGua::CNanGua(void)
{
 m_nPlant = 0;
 m_nBlood = 0;
 x = 255;
 y = rand()%(600-93);
}
CNanGua::~CNanGua(void)
{
}
void CNanGua::PlantInit(HINSTANCE hIns)//植物初始化函数‎()
{
 m_nPlant = ::LoadBitmap(hIns,MAKEINTRESOURCE(IDB_NANGUA));
}
void CNanGua::PlantShow(HDC hMemDC)//显示函数‎()
{
 HDC hdc = ::CreateCompatibleDC(hMemDC); 
 ::SelectObject(hdc,m_nPlant);
 ::TransparentBlt(hMemDC,x,y,155,93,hdc,0,0,155,93,RGB(0,0,0));
 ::DeleteDC(hdc);
}
bool CNanGua::IsHitPlayer(CPlayer& player)//是否接触玩家函数
{
return false;
}
void CNanGua::PlantMove()//植物移动函数()
{
}
void CNanGua::PlantSendGunner(HINSTANCE hIns, CGunnerBox& gunnerBox)//发射炮弹函数(‎)
{
 CGunner* pGunner = new CGunner;
 pGunner->GunnerInit(hIns,x,y);
 gunnerBox.m_lstGunner.push_back(pGunner);
}
bool CNanGua::PlantHitPlayer(CPlayer& player)//玩家吃掉植物
{
 if(player.x+60 >= this->x &&player.x+60 <= this->x+155 && player.y+5 >= this->y &&player.y+5 <= this->y+93)
  return true;
  if(player.x+80 >= this->x &&player.x+80 <= this->x+155 && player.y+50 >= this->y &&player.y+50 <= this->y+93)
  return true;
 if(player.x+80 >= this->x &&player.x+80 <= this->x+155 && player.y+115 >= this->y &&player.y+115 <= this->y+93)
  return true;
  else
 return false;
}

植物有着属于自己的植物盒子,子弹也有着子弹盒子,碍于篇幅就不贴了。。。(不知道为啥CSDN一会就崩,我是一点点保存一点点写的)当然这只是一个很简单的游戏,大家可以看看,也欢迎大家给出一些建议!

你可能感兴趣的:(游戏,win32,游戏制作)