📄 main.c
字号:
/*
Name:贪吃蛇
Copyright:
Author: 随心
Date: 08-02-08 22:43
Description: dev-cpp 4.2.2.9 下编译通过
更改记录:2008年2月9日 晚,第二关增加动态食物,增加支持最大化
*/
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#define MAXLEN 35
#define HARD 1 //初始难度
#define true 1
#define false 0
/****定义结构体****/
typedef struct point
{
int x;
int y;
}point;
/****函数声明****/
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
int GameOver(HWND *);
int IsPause();
void SetRandomPt(HWND*); //设置“食物”的位置
void DrawBknd(HWND *);
void DrawSnake(HWND *); //画蛇身
void DrawRandomPt(HWND *);
void Move(HWND *);
void MoveUp();
void MoveDown();
void MoveLeft();
void MoveRight();
void SetNowWay(int);
int GetNowWay();
void Start(HWND *pWnd);
void MoveRandomPt(HWND *);
/****变量声明****/
char szClassName[ ] = "WindowsApp";
HWND hwnd; /* This is the handle for our window */
int isstop;
int snake_len; //蛇身长度
point pt[MAXLEN]; //蛇节点数
point randPt;
int nowway; //当前方向
int hard;
int speed;
int mark;
int totaltime;
/****主函数****/
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"贪吃蛇(C&SDK版) V1.1 编程讨论群:21035626 2008年2月9日 晚 by 随心",
WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_OVERLAPPEDWINDOW, //窗口风格
260, /* Windows decides the position */
100, /* where the window ends up on the screen */
518, /* The programs width */
425, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, SW_MAXIMIZE);
Start(&hwnd);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/****回调函数****/
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_PAINT:
DrawBknd(&hwnd);
DrawRandomPt(&hwnd);
DrawSnake(&hwnd);
break;
case WM_TIMER:
if (isstop)
break;
if (wParam == 1)
{
switch(GetNowWay())
{
case 4:
MoveRight();
break;
case 2:
MoveDown();
break;
case 3:
MoveLeft();
break;
case 1:
MoveUp();
break;
}
GameOver(&hwnd);
Move(&hwnd);
if (hard >= 2)
MoveRandomPt(&hwnd);
SendMessage(hwnd, WM_PAINT, 0, 0);
}
break;
case WM_KEYDOWN:
if (wParam == VK_UP)
SetNowWay(1);
else if (wParam == VK_DOWN)
SetNowWay(2);
else if (wParam == VK_LEFT)
SetNowWay(3);
else if (wParam == VK_RIGHT)
SetNowWay(4);
else if (wParam == VK_SPACE)
isstop = !isstop;
else if (wParam == VK_F2)
Start(&hwnd);
break;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
/*初始化游戏数据*/
void Start(HWND *pWnd)
{
isstop = false;
snake_len = 3; //初始蛇身长度
nowway = 4; //初始方向
hard = HARD; //初始难度
speed = 400; //初始速度,越小越快
mark = 0; //初始分数
totaltime = 0; //初始游戏时间
int ix = 0;
for ( ; ix != snake_len; ++ix)
{
pt[ix].x = snake_len - ix - 2;
pt[ix].y = 0;
}
srand(time(0)); //随机数种子
SetRandomPt(pWnd);
SetTimer(*pWnd,1,speed,NULL);
}
/****设置食物位置****/
void SetRandomPt(HWND *pWnd)
{
RECT rect;
GetClientRect(*pWnd, &rect);
int x, y;
rerand: x = rand() % rect.right / 30;
y = rand() % rect.bottom / 30;
int ix = 0;
for ( ; ix != snake_len; ++ix)
if (x == pt[ix].x && y == pt[ix].y)
if (hard == 2)
{
if (x < rand() % 16 && y > rand() % 12)
goto rerand;
}
else
goto rerand;
randPt.x = x;
randPt.y = y;
}
/****画蛇****/
void DrawSnake(HWND *pWnd)
{
HDC dc = GetDC(*pWnd);
HBRUSH brush;
brush = CreateSolidBrush(RGB(50, 150, 0));
HBRUSH temp_brush = (HBRUSH)SelectObject(dc, brush);
int ix = 1;
for ( ; ix != snake_len; ++ix) //开始画蛇身
{
Ellipse(dc, pt[ix].x * 30 + 3, pt[ix].y * 30 + 3,
pt[ix].x * 30 + 33, pt[ix].y * 30 + 33);
}
HBRUSH brushHead; //画蛇头
brushHead = CreateSolidBrush(RGB(243,177,48));
SelectObject(dc, brushHead);
Ellipse(dc, pt[0].x * 30 + 3, pt[0].y * 30 + 3,
pt[0].x * 30 + 33, pt[0].y * 30 + 33);
SelectObject(dc, temp_brush);
}
/****画食物****/
void DrawRandomPt(HWND *pWnd)
{
HDC dc = GetDC(*pWnd);
HBRUSH brush;
brush = CreateSolidBrush(RGB(0, 0, 255));
HBRUSH temp_brush = (HBRUSH)SelectObject(dc, brush);
Ellipse(dc, randPt.x * 30 + 3, randPt.y * 30 + 3,
randPt.x * 30 + 33, randPt.y * 30 + 33);
SelectObject(dc, temp_brush);
}
/****画背景****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -