-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakeView.cpp
309 lines (258 loc) · 7.17 KB
/
SnakeView.cpp
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// SNAKEView.cpp : implementation of the CSNAKEView class
//
#include "stdafx.h"
#include "SNAKE.h"
#include "SNAKEDoc.h"
#include "SNAKEView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
struct Snake
{
int x,y;
int len;
int direct;
}Snake[50];
struct Food
{
int x;
int y;
int isfood;
}Food;
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// CSNAKEView
IMPLEMENT_DYNCREATE(CSNAKEView, CView)
BEGIN_MESSAGE_MAP(CSNAKEView, CView)
//{{AFX_MSG_MAP(CSNAKEView)
ON_WM_KEYDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_TIMER()
ON_COMMAND(ID_GAME_START, OnGameStart)
ON_COMMAND(ID_GAME_PAUSE, OnGamePause)
ON_COMMAND(ID_GAME_CONTINUE, OnGameContinue)
ON_COMMAND(ID_GAME_EXIT, OnGameExit)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSNAKEView construction/destruction
CSNAKEView::CSNAKEView()
{
// TODO: add construction code here
}
CSNAKEView::~CSNAKEView()
{
}
BOOL CSNAKEView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CSNAKEView drawing
void CSNAKEView::OnDraw(CDC* pDC)
{
CSNAKEDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CBrush backBrush(RGB(0,0,0));
CBrush* pOldBrush = pDC->SelectObject(&backBrush);
CRect rect;
pDC->GetClipBox(&rect);
pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(),PATCOPY);
pDC->SelectObject(pOldBrush);
pDC->Rectangle(19,19,501,501);
Oninit();
}
/////////////////////////////////////////////////////////////////////////////
// CSNAKEView printing
BOOL CSNAKEView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CSNAKEView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CSNAKEView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CSNAKEView diagnostics
#ifdef _DEBUG
void CSNAKEView::AssertValid() const
{
CView::AssertValid();
}
void CSNAKEView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CSNAKEDoc* CSNAKEView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSNAKEDoc)));
return (CSNAKEDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSNAKEView message handlers
void CSNAKEView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
switch(nChar)
{
case VK_UP:if(Snake[0].direct!=2)Snake[0].direct=1; break;
case VK_DOWN:if(Snake[0].direct!=1)Snake[0].direct=2; break;
case VK_LEFT:if(Snake[0].direct!=4)Snake[0].direct=3; break;
case VK_RIGHT:if(Snake[0].direct!=3)Snake[0].direct=4; break;
}
CView::OnKeyDown(nChar, nRepCnt, nFlags); //??????????????????
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CSNAKEView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CString str;
str.Format("%d,%d",point.x,point.y);
AfxMessageBox(str);
CView::OnRButtonDown(nFlags, point); //??????????????????
CView::OnRButtonDown(nFlags, point);
CView::OnRButtonDown(nFlags, point);
}
void CSNAKEView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CDC *pDC=GetDC();
CString soure;
if(Snake[0].len==2)SetTimer(1,370,NULL);
if(Snake[0].len==3)SetTimer(1,270,NULL);
if(Snake[0].len==6)SetTimer(1,200,NULL);
if(Snake[0].len==9)SetTimer(1,100,NULL);
soure.Format("得分:%d!",(Snake[0].len-3)*10);
//撞界判断
if(Snake[0].x*20<=37||Snake[0].y*20<=37||Snake[0].x*20>=462||Snake[0].y*20>=462)
{
KillTimer(1);
AfxMessageBox(soure);
// s=0;
}
//蛇身相撞判断
if(Snake[0].len>3)
for(int sn=Snake[0].len-1;sn>0;sn--)
{
if(Snake[0].x*20==Snake[sn].x*20&&Snake[0].y*20==Snake[sn].y*20)
{
KillTimer(1);
AfxMessageBox(soure);
// s=0;
}
}
///////////////////////////////////////////////////////////////////////////
pDC->SelectStockObject(WHITE_PEN);
pDC->Rectangle(Snake[Snake[0].len-1].x*20,Snake[Snake[0].len-1].y*20,(Snake[Snake[0].len-1].x+1)*20,(Snake[Snake[0].len-1].y+1)*20);
for(int i=Snake[0].len-1;i>0;i--)
{
Snake[i].x=Snake[i-1].x;
Snake[i].y=Snake[i-1].y;
}
//行走方向判断
if(Snake[0].direct==1)Snake[0].y--;
if(Snake[0].direct==2)Snake[0].y++;
if(Snake[0].direct==3)Snake[0].x--;
if(Snake[0].direct==4)Snake[0].x++;
pDC->SelectStockObject(BLACK_PEN);
CBrush DrawBrush=(RGB(100,100,100));
CBrush *Drawbrush=pDC->SelectObject(&DrawBrush);
pDC->Rectangle(Snake[0].x*20,Snake[0].y*20,(Snake[0].x+1)*20,(Snake[0].y+1)*20);
pDC->SelectObject(DrawBrush);
//判断吃豆的条件,撞到就吃
if(Snake[0].x*20==Food.x*20&&Snake[0].y*20==Food.y*20)
{
Snake[0].len++;
Food.isfood=1;
Snake[Snake[0].len-1].x=Snake[Snake[0].len-2].x;
Snake[Snake[0].len-1].y=Snake[Snake[0].len-2].y;
}
//如果食物被吃了 就生成
if(Food.isfood==1)
{
srand((unsigned)time(NULL));
do
{
for(int isfo=Snake[0].len-1;isfo>=0;isfo--)
if(Snake[0].x*20==Snake[isfo].x*20&&Snake[0].y*20==Snake[isfo].y*20)
{
Food.x=rand()%25;
Food.y=rand()%25;
}
}
while(Food.x*20<70||Food.y*20<70||Food.x*20>430||Food.y*20>430);
pDC->Rectangle(Food.x*20,Food.y*20,(Food.x+1)*20,(Food.y+1)*20);
Food.isfood=0;
}
CView::OnTimer(nIDEvent);
}
void CSNAKEView::OnInitialUpdate()
{
CView::OnInitialUpdate();
Snake[0].x=10;
Snake[0].y=10;
Snake[1].x=11;
Snake[1].y=10;
Snake[2].x=12;
Snake[2].y=10;
Snake[0].direct=3;
Snake[0].len=3;
Food.isfood=1;
// TODO: Add your specialized code here and/or call the base class
}
void CSNAKEView::OnGameStart()
{
// TODO: Add your command handler code here
AfxMessageBox("1秒后开始游戏!");
SetTimer(1,1000,NULL);
}
void CSNAKEView::OnGamePause()
{
// TODO: Add your command handler code here
KillTimer(1);
AfxMessageBox("暂停游戏...");
}
void CSNAKEView::OnGameContinue()
{
// TODO: Add your command handler code here
SetTimer(1,10,NULL);
}
void CSNAKEView::OnGameExit()
{
// TODO: Add your command handler code here
AfxMessageBox("退出游戏...");
exit (0);
}
//DEL CSNAKEView::Oninit()
//DEL {
//DEL
//DEL }
//DEL int CSNAKEView::Oninit()
//DEL {
//DEL
//DEL }
void CSNAKEView::Oninit()
{
CDC *pDC=GetDC();
CBrush DrawBrush=(RGB(100,100,100));
CBrush *Drawbrush=pDC->SelectObject(&DrawBrush);
for(int i=0;i<=Snake[0].len-1;i++)
pDC->Rectangle(Snake[i].x*20,Snake[i].y*20,(Snake[i].x+1)*20,(Snake[i].y+1)*20);
pDC->SelectObject(DrawBrush);
}