下载链接:http://pan.baidu.com/share/link?shareid=106949&uk=638387423 #include<windows.h>
#include "resource.h" #define ID_TIMER 1
#define MAXFOOD 40
typedef struct tagSnake
{
POINT pt;
struct tagSnake *next;
}Snake, *pSnake;
struct tagFood
{
int X;
int Y;
};
enum tagDirection
{
TOP,DOWN,LEFT,RIGHT
};
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK InstructionsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
void InitSnake();
void CreateFood();
void InitMenu(HWND);
void DestorySnake();
pSnake pSnakeHead, pSnakeTail;
tagFood food;
int speed, FoodNum;
tagDirection Direction;
HINSTANCE hInst; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Snake");
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(hInstance, MAKEINTRESOURCE(1));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName; if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(
szAppName,
szAppName,
WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
306,
444,
NULL,
NULL,
hInstance,
NULL); ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd); hInst = hInstance; 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;
HPEN hNewPen, hOldPen;
PAINTSTRUCT ps;
HMENU hMenu;
pSnake temp;
int i, tempSpeed; switch(message)
{
case WM_CREATE:
InitSnake();
CreateFood();
InitMenu(hwnd);
break; case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hNewPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 100));
hOldPen = (HPEN)SelectObject(hdc, hNewPen);
for (i = 0; i <= 300; i += 10)
{
MoveToEx(hdc, i, 0, NULL);
LineTo(hdc, i, 400);
}
for (i = 0; i <= 400; i += 10)
{
MoveToEx(hdc, 0, i, NULL);
LineTo(hdc, 300, i);
}
for (temp = pSnakeTail; temp != NULL; temp = temp->next)
{
Rectangle(hdc, temp->pt.x * 10, temp->pt.y * 10, (temp->pt.x + 1) * 10, (temp->pt.y + 1) * 10);
#include "resource.h" #define ID_TIMER 1
#define MAXFOOD 40
typedef struct tagSnake
{
POINT pt;
struct tagSnake *next;
}Snake, *pSnake;
struct tagFood
{
int X;
int Y;
};
enum tagDirection
{
TOP,DOWN,LEFT,RIGHT
};
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK InstructionsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK AboutDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
void InitSnake();
void CreateFood();
void InitMenu(HWND);
void DestorySnake();
pSnake pSnakeHead, pSnakeTail;
tagFood food;
int speed, FoodNum;
tagDirection Direction;
HINSTANCE hInst; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Snake");
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(hInstance, MAKEINTRESOURCE(1));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName; if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(
szAppName,
szAppName,
WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
306,
444,
NULL,
NULL,
hInstance,
NULL); ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd); hInst = hInstance; 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;
HPEN hNewPen, hOldPen;
PAINTSTRUCT ps;
HMENU hMenu;
pSnake temp;
int i, tempSpeed; switch(message)
{
case WM_CREATE:
InitSnake();
CreateFood();
InitMenu(hwnd);
break; case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hNewPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 100));
hOldPen = (HPEN)SelectObject(hdc, hNewPen);
for (i = 0; i <= 300; i += 10)
{
MoveToEx(hdc, i, 0, NULL);
LineTo(hdc, i, 400);
}
for (i = 0; i <= 400; i += 10)
{
MoveToEx(hdc, 0, i, NULL);
LineTo(hdc, 300, i);
}
for (temp = pSnakeTail; temp != NULL; temp = temp->next)
{
Rectangle(hdc, temp->pt.x * 10, temp->pt.y * 10, (temp->pt.x + 1) * 10, (temp->pt.y + 1) * 10);