-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnakeView.cpp
384 lines (316 loc) · 7.6 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// SnakeView.cpp : CSnakeView 类的实现
//
#include "stdafx.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "Snake.h"
#endif
#include "SnakeDoc.h"
#include "SnakeView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CSnakeView
IMPLEMENT_DYNCREATE(CSnakeView, CView)
BEGIN_MESSAGE_MAP(CSnakeView, CView)
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CSnakeView::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_WM_TIMER()
ON_WM_CREATE()
ON_COMMAND(ID_32772, &CSnakeView::OnStart)
ON_WM_KEYDOWN()
ON_COMMAND(ID_32773, &CSnakeView::OnDifficultyEasy)
ON_COMMAND(ID_32774, &CSnakeView::OnDifficultyNormal)
ON_COMMAND(ID_32775, &CSnakeView::OnDifficultyHard)
ON_COMMAND(ID_32776, &CSnakeView::OnDifficultyHarder)
ON_COMMAND(ID_32777, &CSnakeView::OnPause)
END_MESSAGE_MAP()
// CSnakeView 构造/析构
CSnakeView::CSnakeView()
{
// TODO: 在此处添加构造代码
gameStart = 0;
snakeLength = 4;
difficulty = 10;
pauseOrContinue = 1; //游戏初始设为运行状态
}
CSnakeView::~CSnakeView()
{
}
BOOL CSnakeView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
return CView::PreCreateWindow(cs);
}
// CSnakeView 绘制
void CSnakeView::OnDraw(CDC* /*pDC*/)
{
CSnakeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
}
// CSnakeView 打印
void CSnakeView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
AFXPrintPreview(this);
#endif
}
BOOL CSnakeView::OnPreparePrinting(CPrintInfo* pInfo)
{
// 默认准备
return DoPreparePrinting(pInfo);
}
void CSnakeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加额外的打印前进行的初始化过程
}
void CSnakeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加打印后进行的清理过程
}
void CSnakeView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void CSnakeView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
// CSnakeView 诊断
#ifdef _DEBUG
void CSnakeView::AssertValid() const
{
CView::AssertValid();
}
void CSnakeView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CSnakeDoc* CSnakeView::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSnakeDoc)));
return (CSnakeDoc*)m_pDocument;
}
#endif //_DEBUG
// CSnakeView 消息处理程序
void CSnakeView::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
KillTimer(1); //停掉OnCreate中的定时器
CString score;
score.Format(_T("分数: %d"), snakeLength-4); //游戏分数为蛇的节点数-蛇出生时的节点数
AliveOrDie();
//创建设备描述表
CDC *pDC, hDC;
pDC = GetDC();
hDC.CreateCompatibleDC(NULL);
CBitmap hBitmap, *pOldBitmap ;
hBitmap.CreateCompatibleBitmap(pDC, 1000, 1000);
pOldBitmap = hDC.SelectObject(&hBitmap);
hDC.FillSolidRect(0, 0, 1000, 1000, RGB(255, 255, 255)); //设定白色区域范围
hDC.TextOut(550, 200, score); //在坐标(500,200)处输出游戏分数
hDC.Rectangle(CRect(90, 90, 520, 520)); //设定外矩形框
hDC.Rectangle(CRect(100, 100, 510, 510));
if(gameStart == 1) //游戏处于运行状态
{
b.x = a[0].x;
b.y = a[0].y;
//第0个节点的变化
if (move == right) //向右移动
{
a[0].x = a[0].x + 10;
}
else if (move == up) //向上移动
{
a[0].y = a[0].y - 10;
}
else if (move == left) //向左移动
{
a[0].x = a[0].x - 10;
}
else if (move == down) //向下移动
{
a[0].y = a[0].y + 10;
}
//除第0、1个节点之外其它所有节点的变化
for (int i = 1; i < snakeLength - 1; i++)
{
a[snakeLength - i].x = a[snakeLength - 1 - i ].x;
a[snakeLength - i].y = a[snakeLength - 1 - i ].y;
}
//第1个节点的变化
a[1].x = b.x;
a[1].y = b.y;
CBrush head(RGB(255, 0, 0)); //将蛇的头节点设置成红色
hDC.SelectObject(head);
rect = new CRect(a[0].x, a[0].y, a[0].x+10, a[0].y+10); //头节点的边框
hDC.Rectangle(rect);
for(int i = 1; i < snakeLength; i++)
{
CBrush body(RGB(255, 255, 0)); //将蛇的身体节点设置成黄色
hDC.SelectObject(body);
rect = new CRect(a[i].x, a[i].y, a[i].x+10, a[i].y+10); //身体各节点的边框
hDC.Rectangle(rect);
}
if(a[0].x == xfood && a[0].y == yfood) //蛇吃了食物
{
snakeLength++; //蛇的长度加1
foodOrNot = 1; //产生新食物
//蛇新增加的尾节点与原先的尾节点(现在的倒数第二个节点)的位置重合
//再移动一下才能看到蛇的节点加1
a[snakeLength-1].x = a[snakeLength-2].x;
a[snakeLength-1].y = a[snakeLength-2].y;
}
if(foodOrNot == 1)
{
int x1 = int(Random(10,50)); // 产生的随机值在11~51之间
int y1 = int(Random(10,50));
xfood = x1*10;
yfood = y1*10;
for(int i=0; i<snakeLength; i++)
{
if(xfood==a[i].x && yfood==a[i].y)
{
//如果食物的位置恰好在蛇的某个节点的位置,重新设置食物的位置
int x2 = int(Random(10,50));
int y2 = int(Random(10,50));
xfood = x2*10;
yfood = y2*10;
}
}
foodOrNot = 0;
}
CBrush food(RGB(0,255,0)); //食物的颜色设置为绿色
hDC.SelectObject(food);
hDC.Rectangle(xfood, yfood, xfood+10, yfood+10); //设置食物的边框
}
pDC->BitBlt(0, 0, 1000, 1000, &hDC, 0, 0, SRCCOPY);
hBitmap.DeleteObject();
hDC.DeleteDC();
OnDraw(pDC);
SetTimer(1, difficulty*30, NULL); //将游戏难度与移动时间相关
CView::OnTimer(nIDEvent);
}
int CSnakeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
SetTimer(1, 10, NULL);
return 0;
}
void CSnakeView::AliveOrDie()
{
for(int i=1; i<snakeLength; i++)
{
//蛇的头节点碰到身体某节点
if(a[0].x==a[i].x && a[0].y==a[i].y)
{
gameStart=0;
}
//蛇的头节点碰到边界
else if(a[0].x<100 || a[0].x>500 || a[0].y<100 || a[0].y>500)
{
gameStart=0;
}
}
}
double CSnakeView::Random(double start, double end) //产生随机数,且其值在start~end之间
{
return start+(end-start)*rand()/(RAND_MAX+1.0);
}
void CSnakeView::OnStart()
{
// TODO: 在此添加命令处理程序代码
gameStart = 1;
Initialize();
OnTimer(1);
}
void CSnakeView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
lastMove = move;
if(nChar==37 && lastMove!=right) //按下向左移动键且之前的移动方向不是向右
{
move = left;
}
else if(nChar==38 && lastMove!=down) //按下向上移动键且之前的移动方向不是向下
{
move = up;
}
else if(nChar==39 && lastMove!=left) //按下向右移动键且之前的移动方向不是向左
{
move = right;
}
else if(nChar==40 && lastMove!=up) //按下向下移动键且之前的移动方向不是向上
{
move = down;
}
OnTimer(1); //连续按可以加速
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CSnakeView::Initialize()
{
//游戏刚开始时的状态
move = right;
lastMove = right;
snakeLength = 4;
foodOrNot = 1;
//蛇的出生位置
a[0].x = 180;
a[0].y = 170;
a[1].x = 170;
a[1].y = 170;
a[2].x = 160;
a[2].y = 170;
a[3].x = 150;
a[3].y = 170;
}
void CSnakeView::OnDifficultyEasy()
{
// TODO: 在此添加命令处理程序代码
difficulty = 10;
}
void CSnakeView::OnDifficultyNormal()
{
// TODO: 在此添加命令处理程序代码
difficulty = 6;
}
void CSnakeView::OnDifficultyHard()
{
// TODO: 在此添加命令处理程序代码
difficulty = 3;
}
void CSnakeView::OnDifficultyHarder()
{
// TODO: 在此添加命令处理程序代码
difficulty = 2;
}
void CSnakeView::OnPause()
{
// TODO: 在此添加命令处理程序代码
if(pauseOrContinue == 1)
{
// 暂停游戏
pauseOrContinue = 0;
KillTimer(1);
}
else if(pauseOrContinue == 0)
{
// 继续游戏
pauseOrContinue = 1;
OnTimer(1);
}
}