#include<Windows.h>
#include<stdio.h>
#include<time.h>
const UINT WM_RESTART=WM_USER+100;
const int M(1),Max_Len(30),MaxN(100),size(20),Time(1000);
POINT pt[Max_Len],food;
char used[MaxN][MaxN];
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int nCmdShow)
{
srand(time(NULL));
static TCHAR szAppName[]=TEXT("Snake");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wndclass);
hwnd=CreateWindow(szAppName,
TEXT("Snake"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.lParam;
}
POINT randFood(int Max_x,int Max_y)
{
POINT p;
do
{
p.x=rand()%(Max_x)+1;
p.y=rand()%(Max_y)+1;
}while(used[p.x][p.y]);
used[p.x][p.y]=2;
return p;
}
void restart(HWND hwnd,int &Length,BOOL &bStart,POINT &food,int Max_x,int Max_y,POINT pt[],UINT &Next)
{
Next=VK_RIGHT;
bStart=0;
Length=1;
memset(used,0,sizeof(used));
used[1][1]=1;
food=randFood(Max_x,Max_y);
used[food.x][food.y]=2;
pt[0].x=pt[0].y=size;
SetTimer(hwnd,M,Time+10,NULL);
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static int xCount,yCount,hight,width;
static int Length(1);
static UINT Next(VK_RIGHT);
static BOOL bStart(0),bWin(0);
HDC hdc;
PAINTSTRUCT ps;
// HPEN hPen=CreatePen(PS_INSIDEFRAME,2,RGB(0,0,255));
HBRUSH hBrush=(HBRUSH)GetStockObject(BLACK_BRUSH);
HBRUSH hBrush2=(HBRUSH)GetStockObject(GRAY_BRUSH);
switch(message)
{
case WM_TIMER:
if(bStart)
SendMessage(hwnd,WM_KEYDOWN,Next,0);
break;
case WM_SIZE:
hight=HIWORD(lParam);
width=LOWORD(lParam);
xCount=width/size;
yCount=hight/size;
if(xCount==0||yCount==0)
bStart=false;
else
restart(hwnd,Length,bStart,food,xCount,yCount,pt,Next);
break;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SelectObject(hdc,hBrush);
for(int i=0;i<=width;i+=size)
{
MoveToEx(hdc,i,0,NULL);
LineTo(hdc,i,yCount*size);
}
for(int j=0;j<=hight;j+=size)
{
MoveToEx(hdc,0,j,NULL);
LineTo(hdc,xCount*size,j);
}
for(int i=0;i<Length;i++)
Rectangle(hdc,pt[i].x-size,pt[i].y-size,pt[i].x,pt[i].y);
DeleteObject(hBrush);
SelectObject(hdc,hBrush2);
Rectangle(hdc,(food.x-1)*size,(food.y-1)*size,food.x*size,food.y*size);
DeleteObject(hBrush2);
EndPaint(hwnd,&ps);
break;
case WM_KEYDOWN:
{
int x,y;
switch(wParam)
{
case VK_UP:
if(Next==VK_DOWN)
{
SendMessage(hwnd,WM_RESTART,0,0);
return 0;
}
y=pt[Length-1].y-size;
x=pt[Length-1].x;
break;
case VK_DOWN:
if(Next==VK_UP)
{
SendMessage(hwnd,WM_RESTART,0,0);
return 0;
}
y=pt[Length-1].y+size;
x=pt[Length-1].x;
break;
case VK_LEFT:
if(Next==VK_RIGHT)
{
SendMessage(hwnd,WM_RESTART,0,0);
return 0;
}
y=pt[Length-1].y;
x=pt[Length-1].x-size;
break;
case VK_RIGHT:
if(Next==VK_LEFT)
{
SendMessage(hwnd,WM_RESTART,0,0);
return 0;
}
y=pt[Length-1].y;
x=pt[Length-1].x+size;
break;
default :
return 0;
}
if(x>xCount*size||y>yCount*size||x<size||y<size)
{
SendMessage(hwnd,WM_RESTART,0,0);
return 0;
}
if(used[x/size][y/size]==1&&!(pt[0].x==x&&pt[0].y==y))
{
SendMessage(hwnd,WM_RESTART,0,0);
return 0;
}
else
{
bStart=true;
if(used[x/size][y/size]==2)
{
pt[Length].x=x;
pt[Length].y=y;
used[x/size][y/size]=1;
Length++;
if(Length==Max_Len)
{
bWin=true;
SendMessage(hwnd,WM_RESTART,0,0);
bWin=false;
return 0;
}
SetTimer(hwnd,M,Time-Time*Length/Max_Len+10,NULL);
food=randFood(xCount,yCount);
}
else
{
used[pt[0].x/size][pt[0].y/size]=0;
for(int i=1;i<Length;i++)
pt[i-1]=pt[i];
used[x/size][y/size]=1;
pt[Length-1].x=x;
pt[Length-1].y=y;
}
InvalidateRect(hwnd,NULL,TRUE);
Next=wParam;
}
break;
}
case WM_RESTART:
restart(hwnd,Length,bStart,food,xCount,yCount,pt,Next);
if(MessageBox(hwnd,bWin?TEXT("你赢了!\n要继续吗?"):TEXT("你输了!\n要继续吗?"),TEXT("消息"),MB_YESNO)==IDNO)
{
SendMessage(hwnd,WM_DESTROY,0,0);
return 0;
}
InvalidateRect(hwnd,NULL,TRUE);
break;
case WM_DESTROY:
KillTimer(hwnd,M);
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
自己写的贪吃蛇
最新推荐文章于 2024-06-13 14:48:03 发布