-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtanchishe.txt
243 lines (211 loc) · 5.82 KB
/
tanchishe.txt
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
#-*- coding:utf-8 -*-
#include <windows.h>
#include "strsafe.h"
#include <math.h>
#include <ctime>
#define NUM 1000
#define TWOPI (2*3.14159)
#define LINEHEIGHT 15
#define MAXPTS 100
#define PLAYING_TIMERID 1
//贪吃蛇
struct Snake { POINT pts[MAXPTS];int size, dx, dy, R, G, B; };
Snake snake;
//游戏区域
int xside = 100, yside = 90;
int xdivisions = 20, ydivisions = 18;
int xlength = 2 * xside, ylength = 2 * yside;
int block = xlength / xdivisions; //设置参数时确保能整除且xblock=yblock且block为偶数
int r = block / 2;//蛇节宽度
int snakespeed = 500;//500ms
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MyWindows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("这个程序需要在 windows NT 才能运行!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(
szAppName,
TEXT("SCU工作室"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
//初始化贪吃蛇
snake.size = 2;
snake.dx = 1, snake.dy = 0;
snake.pts[0].x = -xside + 3 * block - block / 2;
snake.pts[0].y = -yside + 5 * block - block / 2;
snake.pts[1].x = -xside + 3 * block - block / 2;
snake.pts[1].y = -yside + 6 * block - block / 2;
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
int i;
RECT rect;
static int cxClient, cyClient;
int iMapMode;
static HGDIOBJ hPen, hOldPen;
static HBRUSH hBrush, hOldBrush;
switch (message)
{
case WM_CREATE:
SetTimer(hwnd, PLAYING_TIMERID, snakespeed, NULL);
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case (int)'w':case (int)'W':
snake.dx = 0, snake.dy = 1; //MessageBox(hwnd, TEXT("ojbk!"), TEXT(""), MB_OK);
return 0;
case (int)'a':case (int)'A':
snake.dx = -1, snake.dy = 0;
return 0;
case (int)'s':case (int)'S':
snake.dx = 0, snake.dy = -1;
return 0;
case (int)'d':case (int)'D':
snake.dx = 1, snake.dy = 0;
return 0;
}
return 0;
case WM_TIMER:
//食物的处理
struct Food{int x,y;};
Food food;
//更改蛇的位置
int lastx=snake.pts[snake.size].x;
int lasty=snake.pts[snake.size].y;
for (i = snake.size - 1; i > 0; i--)
{
snake.pts[i].x = snake.pts[i - 1].x;
snake.pts[i].y = snake.pts[i - 1].y;
}
snake.pts[0].x += snake.dx * block;
snake.pts[0].y += snake.dy * block;
//如果蛇头在食物处或,更改蛇的size,重新生成食物
if(snake.pts[0].x==food.x && snake.pts[0].y==food.y)
{
snake.size++;
snake.pts[snake.size].x=lastx;
snake.pts[snake.size].y=lasty;
}
srand((unsigned)time(NULL));
int tempb=1;
while(tempb)
{
food.x=(rand()%xdivisions-xdivisions/2)*block;
food.y=(rand()%ydivisions-ydivisions/2)*block;
for(i=1;i<=snake.size;i++)
{
if(food.x==snake.pts[i].x && food.y==snake.pts[i].y)
{
tempb=1;
break;
}
if(i==snake.size) tempb=0;
}
}
//死亡判断(碰到边界/蛇身/障碍物)
int flagx=snake.pts[0].x;
int flagy=snake.pts[0].y;
bool invalid=false;
if(abs(flagx)>=xside || abs(flagy)>=yside) invalid=true;//发送重画窗口的消息
for(i=1;i<=snake.size;i++)
{
if(snake.pts[i].x==flagx+block*(snake.dx) && snake.pts[i].y==flagy+block*(snake.dy)) invalid=true;
}
if(invalid==true)
{
invalid=false;
InvalidateRect(hwnd, NULL, TRUE);//发送重画窗口的消息
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
iMapMode = GetMapMode(hdc);
SetMapMode(hdc, MM_ISOTROPIC);
SetWindowExtEx(hdc, 100, 100, NULL);//窗口范围设为100,100
SetViewportExtEx(hdc, cxClient / 2, -cyClient / 2, NULL);
SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL);
//全局背景
//游戏背景
//游戏边界
Rectangle(hdc, -xside, yside, xside, -yside);
//游戏方格
hPen = CreatePen(PS_SOLID, 0, RGB(0, 0, 0));//默认条件下填充非实线的是白色
hOldPen = SelectObject(hdc, hPen);
SetBkMode(hdc, TRANSPARENT);
for (i = 1; i < xdivisions; i++)
{
MoveToEx(hdc, -xside + block * i, yside, NULL);
LineTo(hdc, -xside + block * i, -yside);
}
for (i = 1; i < ydivisions; i++)
{
MoveToEx(hdc, xside, -yside + block * i, NULL);
LineTo(hdc, -xside, -yside + block * i);
}
SelectObject(hdc, hOldPen);
//食物
//贪吃蛇
//一部分初始化暂时放在switch外面
snake.R = 255, snake.G = 0, snake.B = 0;
//hPen = CreatePen(PS_SOLID, 1, RGB(snake.R, snake.G, snake.B));
//SelectObject(hdc, hPen);//若取消注释则这两行把边框也变为蛇的颜色
hBrush = CreateSolidBrush(RGB(snake.R, snake.G, snake.B));
SelectObject(hdc, hBrush);
for (i = 0; i < snake.size; i++)
{
LONG& x = snake.pts[i].x;
LONG& y = snake.pts[i].y;
Rectangle(hdc, x - r, y + r, x + r, y - r);
}
EndPaint(hwnd, &ps);
return 0;
case WM_CLOSE:
if (MessageBox(hwnd, TEXT("确认要关闭窗口吗?"), TEXT("请确认"), MB_YESNO) == IDYES)
DestroyWindow(hwnd);
else return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}