forked from lianera/archives
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.cpp
285 lines (251 loc) · 4.86 KB
/
Snake.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
// Snake.cpp: implementation of the CSnake class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Snake.h"
#include "resource.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern HINSTANCE hInst;
CSnake::CSnake()
{
m_length=1;
m_head=new CNode;
m_tail=new CNode;
m_head->m_next=m_tail;
m_tail->m_former=m_head;
m_head->x=15;
m_head->y=15;
m_tail->x=m_head->x;
m_tail->y=m_head->y;
m_headTrend=LEFT;
m_issk=false;
}
CSnake::~CSnake()
{
ReleaseDC(hWnd,m_bmDC);
CNode *p=m_head;
DeleteNode(m_length);
delete m_head;
delete m_tail;
}
bool CSnake::AddNode(int num)
{
if(num<0) return false;
for(int i=0;i<num;i++)
{
CNode *newnode=new CNode;
//调整坐标
newnode->x=m_tail->x;
newnode->y=m_tail->y;
//在尾巴前加入
//next
newnode->m_next=m_tail;
m_tail->m_former->m_next=newnode;
//former
newnode->m_former=m_tail->m_former;
m_tail->m_former=newnode;
}
m_length+=num;
return false;
}
bool CSnake::DeleteNode(int num)
{
if(num<0 || num>m_length) return false;
for(int i=1;i<num;i++)
{
//尾巴前剪掉
CNode *wantdel;
wantdel=m_tail->m_former; //取得欲删除的
wantdel->m_former->m_next=m_tail; //连接前一个的“后"连接
m_tail->m_former=wantdel->m_former;//连接尾巴的前一个
delete wantdel;
}
m_length-=num;
return false;
}
int CSnake::GetLength()
{
return m_length;
}
bool CSnake::SetInitLength(int len)
{
if(len<0) return false;
return AddNode(len);
}
CSnake::CSnake(int len)
{
SetInitLength(len);
}
bool CSnake::IsEmpty()
{
return m_length==0;
}
bool CSnake::MoveSnack(int pace)
{
CNode Prim=*m_head; //Prim移动前节点位置
//移动头
if(!SwitchTrend(m_headTrend,m_head->x,m_head->y,pace))
{
return false;
}
//把尾巴前一个放到 头的后一个
CNode *mov=m_tail->m_former;
//拿掉
m_tail->m_former=mov->m_former;
mov->m_former->m_next=m_tail;
CNode Mov=*mov;
//放入
mov->m_next=m_head->m_next;
mov->m_former=m_head;
m_head->m_next=mov;
mov->m_next->m_former=mov;
//设置坐标
mov->x=Prim.x;
mov->y=Prim.y;
//尾巴前移
m_tail->x=Mov.x;
m_tail->y=Mov.y;
return true;
}
ETrend CSnake::GetTrend()
{
return m_headTrend;
}
ETrend CSnake::NotTrend()
{
if(m_headTrend==LEFT ) return RIGHT;
if(m_headTrend==RIGHT ) return LEFT;
if(m_headTrend==UP ) return DOWN;
if(m_headTrend==DOWN ) return UP;
return NOTREND;
}
bool CSnake::SwitchTrend(ETrend trend, int &x, int &y,int pace)
{
switch(trend)
{
case LEFT:
x-=pace;
break;
case RIGHT:
x+=pace;
break;
case UP:
y-=pace;
break;
case DOWN:
y+=pace;
break;
default :
return false;
break;
}
return true;
}
bool CSnake::GetSnack(int *x, int *y)
{
CNode *p=m_head;
for(int i=0;i<=m_length;i++)
{
x[i]=p->x;
y[i]=p->y;
p=p->m_next;
}
x[m_length+1]=m_tail->x;
y[m_length+1]=m_tail->y;
return true;
}
void CSnake::DrawSnack(HDC hDC,CPanel *pPanel)
{
//取得所有节点位置
int *x=new int[m_length+2];
int *y=new int[m_length+2];
GetSnack(x,y);
//画画
HBITMAP oldbmp;
//头
int hl=pPanel->GetGcx() * x[0];
int ht=pPanel->GetGcy() * y[0];
int hcx=pPanel->GetGcx();
int hcy=pPanel->GetGcy();
if(m_issk)
{
oldbmp=(HBITMAP)SelectObject(m_bmDC,m_shrunken);
m_issk=false;
}
else
{
oldbmp=(HBITMAP)SelectObject(m_bmDC,m_headbmp);
m_issk=true;
}
TransparentBlt(hDC,hl,ht,hcx,hcy,m_bmDC,0,0,47,47,RGB(255,0,0));
SelectObject(m_bmDC,m_nodebmp);
//节点
for(int i=1;i<=m_length+1;i++)
{
int nl=pPanel->GetGcx() * x[i];
int nt=pPanel->GetGcy() * y[i];
int ncx=pPanel->GetGcx();
int ncy=pPanel->GetGcy();
TransparentBlt(hDC,nl,nt,ncx,ncy,m_bmDC,0,0,47,47,RGB(255,0,0));
}
SelectObject(m_bmDC,oldbmp);
delete []x;
delete []y;
}
void CSnake::InitDraw(HDC hDC)
{
m_headbmp=LoadBitmap(hInst,MAKEINTRESOURCE(IDB_HEAD));
m_nodebmp=LoadBitmap(hInst,MAKEINTRESOURCE(IDB_NODE));
m_shrunken=LoadBitmap(hInst,MAKEINTRESOURCE(IDB_SHRUNKEN));
m_bmDC=CreateCompatibleDC(hDC);
}
bool CSnake::SetTrend(ETrend trend)
{
if(NotTrend()==trend) return false;
m_headTrend=trend;
return true;
}
//检查碰撞
bool CSnake::IsBump(CPanel *pPanel)
{
//四周边框
/* if(m_head->x<0 || m_head->x > pPanel->GetGx() ||
m_head->y<0 || m_head->y > pPanel->GetGy())
{
return true;
}
*/
if(m_head->x<0)m_head->x=pPanel->GetGx();
if(m_head->x>pPanel->GetGx())m_head->x=0;
if(m_head->y<0)m_head->y=pPanel->GetGy();
if(m_head->y>pPanel->GetGy())m_head->y=0;
//自己
//取得所有节点位置
int *x=new int[m_length+2];
int *y=new int[m_length+2];
GetSnack(x,y);
//用头和身体比较
for(int i=1;i<=m_length+1;i++)
{
if(x[0]==x[i] && y[0]==y[i])
{
delete []x;
delete []y;
return true;
}
}
delete []x;
delete []y;
return false;
}
bool CSnake::IsEatApple(CMill *pMill)
{
POINT ApplePos=pMill->GetPos();
if(m_head->x==ApplePos.x && m_head->y==ApplePos.y)
{
return true;
}
return false;
}