vc++6.0贪吃蛇代码
时间: 2023-07-31 17:05:10 浏览: 168
以下是一个基于VC++6.0的贪吃蛇游戏代码,仅供参考:
```
#include <windows.h>
#include <time.h>
#define MAX_LENGTH 100
#define WIDTH 30
#define HEIGHT 20
#define SPACE 20
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
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(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("This program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("Snake"), WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME ^ WS_MAXIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, (WIDTH + 1) * SPACE, (HEIGHT + 1) * SPACE, NULL, NULL, hInstance, NULL);
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) {
static int cxClient, cyClient;
static int head, tail, length;
static int x[MAX_LENGTH], y[MAX_LENGTH];
static int candyX, candyY;
static int direction;
static BOOL isEating;
static int score;
static BOOL isGameOver;
static HDC hdc;
static HBRUSH hbrSnake, hbrCandy;
static HPEN hpenGrid;
static RECT rect;
switch (message) {
case WM_CREATE:
srand(unsigned(time(NULL)));
x[0] = rand() % WIDTH;
y[0] = rand() % HEIGHT;
candyX = rand() % WIDTH;
candyY = rand() % HEIGHT;
direction = VK_RIGHT;
isEating = FALSE;
score = 0;
isGameOver = FALSE;
hbrSnake = CreateSolidBrush(RGB(0, 255, 0));
hbrCandy = CreateSolidBrush(RGB(255, 0, 0));
hpenGrid = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
SetTimer(hwnd, 1, 200, NULL);
break;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
break;
case WM_KEYDOWN:
switch (wParam) {
case VK_UP:
case VK_DOWN:
case VK_LEFT:
case VK_RIGHT:
if (abs(direction - wParam) != 2) {
direction = wParam;
}
break;
case VK_ESCAPE:
SendMessage(hwnd, WM_DESTROY, 0, 0);
break;
}
break;
case WM_TIMER:
if (!isGameOver) {
if (isEating) {
head++;
length++;
score += 10;
x[head] = x[head - 1];
y[head] = y[head - 1];
isEating = FALSE;
}
else {
tail++;
head++;
x[head] = x[head - 1];
y[head] = y[head - 1];
}
switch (direction) {
case VK_UP:
y[head]--;
break;
case VK_DOWN:
y[head]++;
break;
case VK_LEFT:
x[head]--;
break;
case VK_RIGHT:
x[head]++;
break;
}
if (x[head] < 0 || x[head] > WIDTH - 1 || y[head] < 0 || y[head] > HEIGHT - 1) {
isGameOver = TRUE;
}
for (int i = tail; i < head - 1; i++) {
if (x[i] == x[head] && y[i] == y[head]) {
isGameOver = TRUE;
break;
}
}
if (x[head] == candyX && y[head] == candyY) {
isEating = TRUE;
candyX = rand() % WIDTH;
candyY = rand() % HEIGHT;
}
InvalidateRect(hwnd, NULL, TRUE);
}
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &rect);
SelectObject(hdc, hpenGrid);
for (int i = 1; i < WIDTH; i++) {
MoveToEx(hdc, i * SPACE, 0, NULL);
LineTo(hdc, i * SPACE, (HEIGHT + 1) * SPACE);
}
for (int i = 1; i < HEIGHT; i++) {
MoveToEx(hdc, 0, i * SPACE, NULL);
LineTo(hdc, (WIDTH + 1) * SPACE, i * SPACE);
}
SelectObject(hdc, hbrCandy);
Rectangle(hdc, candyX * SPACE, candyY * SPACE, (candyX + 1) * SPACE, (candyY + 1) * SPACE);
SelectObject(hdc, hbrSnake);
for (int i = tail; i <= head; i++) {
Rectangle(hdc, x[i] * SPACE, y[i] * SPACE, (x[i] + 1) * SPACE, (y[i] + 1) * SPACE);
}
if (isGameOver) {
SetTextColor(hdc, RGB(255, 0, 0));
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc, cxClient / 2 - 50, cyClient / 2 - 10, TEXT("Game Over!"), 10);
}
SetTextColor(hdc, RGB(0, 0, 255));
SetBkMode(hdc, TRANSPARENT);
TCHAR szScore[20];
wsprintf(szScore, TEXT("Score: %d"), score);
TextOut(hdc, 0, 0, szScore, lstrlen(szScore));
EndPaint(hwnd, &rect);
break;
case WM_DESTROY:
KillTimer(hwnd, 1);
DeleteObject(hbrSnake);
DeleteObject(hbrCandy);
DeleteObject(hpenGrid);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
```
阅读全文