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

时间:2021-10-12 22:41:42

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

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

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#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);
}

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

原文链接:https://blog.csdn.net/junk2012/article/details/41675929