欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++实现打地鼠游戏设计

程序员文章站 2022-03-15 08:15:32
本文实例为大家分享了c++实现打地鼠游戏的具体代码,供大家参考,具体内容如下代码:#include class cmywnd :public cframewnd{pr...

本文实例为大家分享了c++实现打地鼠游戏的具体代码,供大家参考,具体内容如下

C++实现打地鼠游戏设计

代码:

#include <afxwin.h>
 
class cmywnd :public cframewnd
{
private:
 cdc *m_pmdc;
 cbitmap *m_pbitmap[5];
 crect myrect[6];
 cstring picpath[5];
 int hit;
 bool m_state[6];
 int counter;
 int num;
 int hammer_x;
 int hammer_y;
public:
 cmywnd()
 {
 
 create(null,"third app");
 
 cclientdc dc(this);
 picpath[0]="../image/background.bmp";
 picpath[1]="../image/mouse1.bmp";
 picpath[2]="../image/mouse2.bmp";
 picpath[3]="../image/hammer1.bmp";
 picpath[4]="../image/hammer2.bmp";
 //
 myrect[0].setrect(30,10,130,110);
 myrect[1].setrect(190,10,290,110);
 myrect[2].setrect(340,10,440,110);
 myrect[3].setrect(30,140,130,240);
 myrect[4].setrect(190,140,290,240);
 myrect[5].setrect(340,140,440,240);
 //
 hit=0;
 for(int i=0;i<6;i++)
  m_state[i]=false;
 
 counter=0;
 hammer_x=hammer_y=0;
 num=0;
 //不显示鼠标
 //showcursor(false);
 
 m_pmdc=new cdc;
 for(int i=0;i<5;i++)
 {
  m_pbitmap[i]=new cbitmap;
  m_pbitmap[i]->m_hobject=(hbitmap)::loadimage(null,picpath[i],
       image_bitmap, 0,0,lr_loadfromfile);
 }
 m_pmdc->createcompatibledc(&dc);
 movewindow(200,20,480,320);
 this->settimer(1,1000,null);
 
 }
 void mypait(int flag);
 
 ~cmywnd()
 {
 for(int i=0;i<5;i++)
 delete m_pbitmap[i];
 delete m_pmdc;
 
 }
 declare_message_map()
 afx_msg void onlbuttonup(uint nflags, cpoint point);
// afx_msg void onpaint();
 afx_msg void ontimer(uint_ptr nidevent);
 afx_msg void onmousemove(uint nflags, cpoint point);
// afx_msg void onpaint();
};
 
class cmyapp:public cwinapp
{
public:
 bool initinstance();
};
 
bool cmyapp::initinstance()
{
 cmywnd *pf=new cmywnd;
 pf->showwindow(m_ncmdshow);
 this->m_pmainwnd=pf;
 return true;
}
cmyapp firstapp;begin_message_map(cmywnd, cframewnd)
 on_wm_lbuttonup()
// on_wm_paint()
 on_wm_timer()
 on_wm_mousemove()
// on_wm_paint()
 end_message_map()
 
void cmywnd::onlbuttonup(uint nflags, cpoint point)
{
 // todo: 在此添加消息处理程序代码和/或调用默认值
 counter++;
 m_state[num]=false;
 num=rand()%6;
 m_state[num]=true;
 for(int i=0;i<6;i++)
 {
 if(myrect[i].ptinrect(point)&&m_state[i])
 {
  hit++;
 }
 else
  hit=0;
 }
 cframewnd::onlbuttonup(nflags, point);
}
 
 
void cmywnd::ontimer(uint_ptr nidevent)
{
 // todo: 在此添加消息处理程序代码和/或调用默认值
 cclientdc dc(this); // device context for painting
 m_pmdc->selectobject(m_pbitmap[0]);
 dc.bitblt(0,0,480,320,m_pmdc,0,0,srccopy);
 m_pmdc->selectobject(m_pbitmap[3]);
 dc.bitblt(hammer_x,hammer_y,148,148,m_pmdc,0,0,srcand);
 m_pmdc->selectobject(m_pbitmap[4]);
 dc.bitblt(hammer_x,hammer_y,148,148,m_pmdc,0,0,srcpaint);
 for(int i=0;i<6;i++)
 {
 if(m_state[i])
 {
  m_pmdc->selectobject(m_pbitmap[1]);
  dc.bitblt(myrect[i].left,myrect[i].top,100,100,m_pmdc,0,0,srcand);
  m_pmdc->selectobject(m_pbitmap[2]);
  dc.bitblt(myrect[i].left,myrect[i].top,100,100,m_pmdc,0,0,srcpaint);
 }
 }
 if(hit>=3)
 {
 killtimer(1);
 messagebox("你赢了!");
 }
 if(counter>=10)
 {
 killtimer(1);
 messagebox("你输了!");
 }
 cframewnd::ontimer(nidevent);
}
 
 
 
void cmywnd::onmousemove(uint nflags, cpoint point)
{
 // todo: 在此添加消息处理程序代码和/或调用默认值
 hammer_x=point.x;
 hammer_y=point.y;
 cframewnd::onmousemove(nflags, point);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: C++ 打地鼠