这是一个贪吃蛇游戏
里面有窗口程序,具体我也了解的不够。
// MySnake.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include "time.h"
#define MAX_LOADSTRING 100
HINSTANCE hInst;
TCHAR szTitle[MAX_LOADSTRING];
TCHAR szWindowClass[MAX_LOADSTRING];
ATOM
MyRegisterClass(HINSTANCE hInstance);
BOOL
InitInstance(HINSTANCE, int);
LRESULT CALLBACK
WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR
lpCmdLine,
int
nCmdShow)
{
MSG msg;
HACCEL hAccelTable;
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MYSNAKE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MYSNAKE);
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style
= CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc
= (WNDPROC)WndProc;
wcex.cbClsExtra
= 0;
wcex.cbWndExtra
= 0;
wcex.hInstance
= hInstance;
wcex.hIcon
= LoadIcon(hInstance, (LPCTSTR)IDI_MYSNAKE);
wcex.hCursor
= LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground
= (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName
= (LPCSTR)IDC_MYSNAKE;
wcex.lpszClassName
= szWindowClass;
wcex.hIconSm
= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
HDC hdc;
int row=20;
int col=20;
#define
WIDTH 30
#define
HEIGHT 30
#define
MAXSIZE
50
POINT m_food;
struct Snake
{
POINT m_pos[MAXSIZE];
int m_direction;
int len;
};
Snake m_snake;
void generatefood()
{
srand((unsigned)time(NULL));
m_food.x=rand()%20;
m_food.y=rand()%20;
}
void init_snake()
{
m_snake.len=3;
m_snake.m_pos[0].x=0;
m_snake.m_pos[0].y=0;
m_snake.m_direction=1;
for(int n=1;n<MAXSIZE;n++)
{
m_snake.m_pos[n].x=-1;
m_snake.m_pos[n].y=-1;
}
generatefood();
}
void MyPaint(HDC hdc)
{
HBRUSH hbr=CreateSolidBrush(RGB(255,0,0));
SelectObject(hdc,hbr);
for(int y=0;y<row;y++)
{
for(int x=0;x<col;x++)
{
Rectangle(hdc,x*WIDTH,y*HEIGHT,(x+1)*WIDTH,(y+1)*HEIGHT);
}
}
HBRUSH hbrgreen=CreateSolidBrush(RGB(0,255,0));
SelectObject(hdc,hbrgreen);
for(int n=0;n<m_snake.len;n++)
{
Rectangle(hdc,m_snake.m_pos[n].x*WIDTH,m_snake.m_pos[n].y*HEIGHT,(m_snake.m_pos[n].x+1)*WIDTH,
(m_snake.m_pos[n].y+1)*HEIGHT);
}
HBRUSH hbrblue=CreateSolidBrush(RGB(0,0,255));
SelectObject(hdc,hbrblue);
Rectangle(hdc,m_food.x*WIDTH,m_food.y*HEIGHT,(m_food.x+1)*WIDTH,(m_food.y+1)*HEIGHT);
}
void OnStart(HWND hWnd)
{
init_snake();
SetTimer(hWnd,1,300,NULL);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance;
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
RECT crc,wrc;
GetClientRect(hWnd,&crc);
GetWindowRect(hWnd,&wrc);
int borderwidth=(wrc.right-wrc.left)-(crc.right-crc.left);
int borderheight=(wrc.bottom-wrc.top)-(crc.bottom-crc.top);
SetWindowPos(hWnd,NULL,0,0,600+borderwidth,600+borderheight,SWP_NOMOVE);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
hdc=GetDC(hWnd);
OnStart(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int n;
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_COMMAND:
wmId
= LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_UP:
m_snake.m_direction=0;
break;
case VK_RIGHT:
m_snake.m_direction=1;
break;
case VK_DOWN:
m_snake.m_direction=2;
break;
case
VK_LEFT:
m_snake.m_direction=3;
break;
}
break;
case WM_TIMER:
for( n=m_snake.len-1;n>=1;n--)
{
m_snake.m_pos[n]=m_snake.m_pos[n-1];
}
if(0==m_snake.m_direction)
{
m_snake.m_pos[0].y=m_snake.m_pos[0].y-1;
}
if(1==m_snake.m_direction)
{
m_snake.m_pos[0].x=m_snake.m_pos[0].x+1;
}
if(2==m_snake.m_direction)
{
m_snake.m_pos[0].y=m_snake.m_pos[0].y+1;
}
if( 3==m_snake.m_direction)
{
m_snake.m_pos[0].x=m_snake.m_pos[0].x-1;
}
if(m_snake.m_pos[0].x==m_food.x&&m_snake.m_pos[0].y==m_food.y)
{
m_snake.len++;
generatefood();
}
if(m_snake.m_pos[0].x>19||m_snake.m_pos[0].y>19||m_snake.m_pos[0].x<0||m_snake.m_pos[0].y<0)
{
KillTimer(hWnd,1);
if(IDYES==MessageBox(hWnd,"Game over,你想重新开始吗?","提示",MB_YESNO))
{
OnStart(hWnd);
}
else
{
PostQuitMessage(0);
}
}
hdc=GetDC(hWnd);
MyPaint(hdc);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
MyPaint(hdc);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}