// snake4.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
#include "math.h"
#include "time.h"
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK ChildWndProc(HWND,UINT,WPARAM,LPARAM);
void setranfood();
void setblock();
void bltblock(HDC &hdc);
void bltfood(HDC &hd);
COLORREF setrancolor();
BOOL connectchar(TCHAR *p);
struct point
{int x;
int y;
point *next;
} food;
TCHAR *szClassN="sn";
TCHAR *szChildClassN="sf";
TCHAR *szWinN="snake";
int iflag=0;
int Box[40][40]={0};
int grade=0;
class snake
{public:
snake();
void locomotion(int xnext,int ynext);
void recreate();
int retx();
int rety();
int retncount();
BOOL drawsnake(HDC &hd);
BOOL collision(int xnext,int ynext);
BOOL eatfood(int xnext,int ynext);
~snake();
private:
point *host;
int ncount;
} ;
void endmain(snake &nsk,HWND &hwnd,WPARAM wparam,LPARAM lparam);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
HWND hwnd;
WNDCLASS wc;
MSG msg;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance=hInstance;
wc.lpfnWndProc=WndProc;
wc.lpszClassName=szClassN;
wc.lpszMenuName=0;
wc.style=0;
if(!RegisterClass(&wc))
{MessageBox(NULL,"I can't execlute it","warn",0);
return 0;
}
wc.lpfnWndProc=ChildWndProc;
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName=szChildClassN;
wc.cbWndExtra=sizeof(long);
wc.hIcon=NULL;
RegisterClass(&wc);
hwnd=CreateWindow(szClassN,szWinN,WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX,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.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{static HWND hwndchild,hwndch;
switch(msg)
{case WM_SIZE:
hwndch=CreateWindow(szChildClassN,NULL,WS_CHILDWINDOW|WS_VISIBLE,0,0,0,0,hwnd,(HMENU)(2),(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
hwndchild=CreateWindow(szChildClassN,NULL,WS_CHILDWINDOW|WS_VISIBLE,0,0,0,0,hwnd,(HMENU)(1),(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
MoveWindow(hwndchild,6,6,480,480,TRUE);
MoveWindow(hwndch,540,14,150,30,TRUE);
SetWindowLong(hwndchild,0,0);
SetWindowLong(hwndch,0,1);
return 0;
case WM_KEYDOWN:
switch(wparam)
{ case VK_RIGHT:
if(iflag==0) break;
iflag=3;
break;
case VK_LEFT:
if(iflag==3) break;
iflag=0;
break;
case VK_UP:
if(iflag==1) break;
iflag=2;
break;
case VK_DOWN:
if(iflag==2) break;
iflag=1;
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,msg,wparam,lparam);
}
LRESULT CALLBACK ChildWndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
HDC hdc,hd;
RECT rect,re;
PAINTSTRUCT ps;
HBITMAP hbitmap;
HBRUSH hbrush;
HPEN hpen;
static int n=0;
static snake snk;
static TCHAR szfen[256]="分数:";
switch(msg)
{
case WM_CREATE:
if(GetWindowLong(hwnd,0)==0)
{if(n==0){setblock();
setranfood();
n++;
if(n=4) n=1;}
SetTimer(hwnd,0,100,NULL);}
GetClientRect(hwnd,&rect);
re.bottom=rect.bottom-3;re.top=rect.top+3;re.left=rect.left+3;re.right=rect.right-3;
return 0;
case WM_TIMER:
if(GetWindowLong(hwnd,0)==0)
{
GetClientRect(hwnd,&rect);
hdc=GetDC(hwnd);
hd=CreateCompatibleDC(hdc);
hpen=CreatePen(PS_SOLID,1,RGB(0,0,0));
hbrush=CreateSolidBrush(setrancolor());
hbitmap=CreateCompatibleBitmap(hdc,rect.right,rect.bottom);
SelectObject(hd,hbitmap);
FillRect(hd,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH));
Rectangle(hd,rect.left,rect.top,rect.right,rect.bottom);
bltblock(hd);
SelectObject(hd,hpen);
SelectObject(hd,hbrush);
bltfood(hd);
snk.drawsnake(hd);
BitBlt(hdc,rect.left,rect.top,rect.right,rect.bottom,hd,0,0,SRCCOPY);
DeleteObject(hbitmap);
DeleteDC(hd);
ReleaseDC(hwnd,hdc);
DeleteObject(hbrush);
DeleteObject(hpen);
switch(iflag)
{case 0:
snk.locomotion(snk.retx()-1,snk.rety());
break;
case 1:
snk.locomotion(snk.retx(),snk.rety()+1);
break;
case 2:
snk.locomotion(snk.retx(),snk.rety()-1);
break;
case 3:
snk.locomotion(snk.retx()+1,snk.rety());
break;
}
if(snk.retncount()>3000)
{KillTimer(hwnd,0);
if(MessageBox(GetParent(hwnd),"you win the game!! go on?","congradulations",MB_YESNO)==IDYES)
endmain(snk,hwnd,wparam,lparam);
else SendMessage(GetParent(hwnd),WM_DESTROY,wparam,lparam);}
if(snk.collision(snk.retx(),snk.rety()))
{KillTimer(hwnd,0);
if(MessageBox(GetParent(hwnd),"you lost the game!! go on?","sorry",MB_YESNO)==IDYES)
endmain(snk,hwnd,wparam,lparam);
else SendMessage(hwnd,WM_DESTROY,wparam,lparam);}
if(snk.eatfood(snk.retx(),snk.rety()))
{grade+=10;
setranfood();
}
}
if(GetWindowLong(hwnd,0)==1)
{InvalidateRect(hwnd,&re,TRUE);
GetClientRect(hwnd,&rect);
hdc=GetDC(hwnd);
Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);
connectchar(szfen);
TextOut(hdc,15,7,szfen,lstrlen(szfen));
ReleaseDC(hwnd,hdc);
}
return 0;
case WM_PAINT:
InvalidateRect(hwnd,NULL,TRUE);
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,msg,wparam,lparam);
}
snake::snake()
{ point *create;
int cx=22,cy=20;
host=new point;
host->next=NULL;
host->x=cx;
host->y=cy;
Box[host->x][host->y]=1;
ncount=3;
while(--ncount)
{create=new point;
create->next=host;
host=create;
host->x=--cx;
host->y=cy;
Box[(host->x)][(host->y)]=1;
}
}
snake::~snake()
{point *select;
while(host!=NULL)
{select=host->next;
delete (host);
host=select;
}
delete (select);
}
void snake::locomotion(int xnext,int ynext)
{ point select,create,*pointhost;
pointhost=host;
select=*pointhost;
pointhost->x=xnext;
pointhost->y=ynext;
while(pointhost->next!=NULL)
{pointhost=pointhost->next;
create=*pointhost;
pointhost->x=select.x;
pointhost->y=select.y;
select=create;
}
Box[select.x][select.y]=0;
}
BOOL snake::drawsnake(HDC &hd)
{point *pointhost;
pointhost=host;
while(pointhost!=NULL)
{Rectangle(hd,12*(pointhost->x),12*(pointhost->y),12*(pointhost->x)+12,12*(pointhost->y)+12);
pointhost=pointhost->next;
}
return TRUE;
}
BOOL snake::collision(int xnext,int ynext)
{if(Box[xnext][ynext]==1||Box[xnext][ynext]==3)
{
return TRUE;
}else
if(Box[xnext][ynext]==0)
Box[xnext][ynext]=1;
return FALSE;
}
BOOL snake::eatfood(int xnext,int ynext)
{point *create;
if(Box[xnext][ynext]==2)
{Box[xnext][ynext]=1;
switch(iflag)
{
case 0:xnext--;break;
case 1:ynext++;break;
case 2:ynext--;break;
case 3:xnext++;break;
}
create=new point;
create->x=xnext;
create->y=ynext;
create->next=host;
host=create;
Box[xnext][ynext]=1;
ncount++;
return TRUE;
}
return FALSE;
}
int snake::retx()
{return host->x;
}
int snake::retncount()
{return ncount;
}
int snake::rety()
{return host->y;
}
void setranfood()
{
srand((unsigned)time( NULL ));
do{
food.x=rand()%40;
food.y=rand()%40;
}while(Box[food.x][food.y]!=0);
Box[food.x][food.y]=2;
}
void setblock()
{
int i,j;
for(i=0;i<=39;i++)
for(j=0;j<=39;j++)
{if(i==0||i==39||j==0||j==39)
Box[i][j]=3;
}
}
void bltblock(HDC &hd)
{Rectangle(hd,12,12,468,468);
}
void bltfood(HDC &hd)
{Rectangle(hd,(food.x)*12,(food.y)*12,(food.x)*12+12,(food.y)*12+12);
}
BOOL connectchar(TCHAR *p)
{TCHAR con[8]="\0";
int j=1,i,n=grade,z=grade;
if(grade<10)
{con[0]='0'+grade;
p[6]='\0';
lstrcat(p,con);
return TRUE;
}
while((n=(n/10))>=10)
j++;
for(i=0;i<=j;i++)
{con[i]=(int)(z/pow(10,j-i))+'0';
z=z-(int)(((int