vc++6.0贪吃蛇代码
时间: 2023-07-31 07:05:07 浏览: 74
以下是一个简单的贪吃蛇游戏的VC++6.0代码示例:
```c++
#include <windows.h>
#include <time.h>
#define ROW 30
#define COL 30
#define WIDTH 20
#define HEIGHT 20
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
struct Point
{
int x;
int y;
};
struct Snake
{
Point body[ROW * COL];
int length;
int direction;
};
struct Food
{
Point position;
bool exist;
};
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
void DrawGrid(HDC hdc);
void DrawSnake(HDC hdc, Snake snake);
void DrawFood(HDC hdc, Food food);
void MoveSnake(Snake& snake);
bool CheckCollision(Snake snake, int x, int y);
void GenerateFood(Food& food, Snake snake);
Snake snake;
Food food;
int score;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
srand((unsigned)time(NULL));
snake.body[0].x = ROW / 2;
snake.body[0].y = COL / 2;
snake.length = 1;
snake.direction = UP;
food.exist = false;
score = 0;
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "SnakeClass";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, "SnakeClass", "Snake", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WIDTH * ROW + 16, HEIGHT * COL + 39, NULL, NULL, hInstance, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch (message)
{
case WM_CREATE:
SetTimer(hwnd, 1, 100, NULL);
break;
case WM_TIMER:
MoveSnake(snake);
if (CheckCollision(snake, food.position.x, food.position.y))
{
snake.length++;
score++;
food.exist = false;
}
InvalidateRect(hwnd, NULL, TRUE);
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_UP:
if (snake.direction != DOWN)
{
snake.direction = UP;
}
break;
case VK_DOWN:
if (snake.direction != UP)
{
snake.direction = DOWN;
}
break;
case VK_LEFT:
if (snake.direction != RIGHT)
{
snake.direction = LEFT;
}
break;
case VK_RIGHT:
if (snake.direction != LEFT)
{
snake.direction = RIGHT;
}
break;
}
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
DrawGrid(hdc);
DrawSnake(hdc, snake);
if (!food.exist)
{
GenerateFood(food, snake);
}
DrawFood(hdc, food);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
KillTimer(hwnd, 1);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
void DrawGrid(HDC hdc)
{
for (int i = 0; i <= ROW; i++)
{
MoveToEx(hdc, i * WIDTH + 8, 0, NULL);
LineTo(hdc, i * WIDTH + 8, HEIGHT * COL);
}
for (int i = 0; i <= COL; i++)
{
MoveToEx(hdc, 8, i * HEIGHT, NULL);
LineTo(hdc, WIDTH * ROW + 8, i * HEIGHT);
}
}
void DrawSnake(HDC hdc, Snake snake)
{
for (int i = 0; i < snake.length; i++)
{
Rectangle(hdc, snake.body[i].x * WIDTH + 9, snake.body[i].y * HEIGHT, (snake.body[i].x + 1) * WIDTH + 8, (snake.body[i].y + 1) * HEIGHT);
}
}
void DrawFood(HDC hdc, Food food)
{
if (food.exist)
{
Ellipse(hdc, food.position.x * WIDTH + 9, food.position.y * HEIGHT, (food.position.x + 1) * WIDTH + 8, (food.position.y + 1) * HEIGHT);
}
}
void MoveSnake(Snake& snake)
{
for (int i = snake.length - 1; i > 0; i--)
{
snake.body[i].x = snake.body[i - 1].x;
snake.body[i].y = snake.body[i - 1].y;
}
switch (snake.direction)
{
case UP:
snake.body[0].y--;
break;
case DOWN:
snake.body[0].y++;
break;
case LEFT:
snake.body[0].x--;
break;
case RIGHT:
snake.body[0].x++;
break;
}
if (snake.body[0].x < 0 || snake.body[0].x >= ROW || snake.body[0].y < 0 || snake.body[0].y >= COL)
{
MessageBox(NULL, "Game Over!", "Game Over!", MB_ICONEXCLAMATION | MB_OK);
PostQuitMessage(0);
}
for (int i = 1; i < snake.length; i++)
{
if (snake.body[0].x == snake.body[i].x && snake.body[0].y == snake.body[i].y)
{
MessageBox(NULL, "Game Over!", "Game Over!", MB_ICONEXCLAMATION | MB_OK);
PostQuitMessage(0);
}
}
}
bool CheckCollision(Snake snake, int x, int y)
{
if (snake.body[0].x == x && snake.body[0].y == y)
{
return true;
}
else
{
return false;
}
}
void GenerateFood(Food& food, Snake snake)
{
do
{
food.position.x = rand() % ROW;
food.position.y = rand() % COL;
} while (CheckCollision(snake, food.position.x, food.position.y));
food.exist = true;
}
```
这个示例使用 Win32 API 和 GDI 绘制贪吃蛇游戏,并且包含了基本的移动、碰撞检测和食物生成等功能。还有许多可以改进和完善的地方,但是作为一个简单的示例代码已经足够了。
阅读全文