// wanwan.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include<time.h>
#define LEFT VK_LEFT
#define RIGHT VK_RIGHT
#define DOWN VK_DOWN
#define UP VK_UP
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
TCHAR szClassName[ ] = TEXT("WindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_GLOBALCLASS;
wincl.cbSize = sizeof (WNDCLASSEX);
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 (
WS_EX_TOOLWINDOW, /* Extended possibilites for variation */
szClassName, /* Classname */
TEXT("SNAKE"), /* Title Text */
WS_SYSMENU, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
540, /* The programs width */
380, /* 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, nFunsterStil);
/* 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;
}
typedef struct snake
{
POINT node[1000];
int num;
int direct;
int life;
}snk;
snk a={{{80,60},{60,60}},2,RIGHT,1};
void rect(HDC hdc,POINT p,int a)
{
Ellipse(hdc,p.x,p.y,p.x+a,p.y+a);//???为什么设为零不行
}
// 用来判断边界!!
int ismax(POINT a,POINT b,POINT c,int l)
{
if((a.x<b.x)||(a.x>c.x)||(a.y>c.y)||(a.y<b.y))
return 1;
else return 0;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static HPEN hpen=CreatePen(PS_SOLID,4,RGB(200,0,10)),hpen1=CreatePen(PS_SOLID,4,RGB(0,200,0)),hpen2=CreatePen(PS_SOLID,4,RGB(0,200,200));
static HPEN hpenw=CreatePen(PS_SOLID,4,RGB(255,255,255));
static int x=0,y=0,score=0,rank=1;
static POINT bound1={0,20},bound2;
static POINT food={200,200};
TCHAR sco[50];
static int intv=200;//代表200毫秒
static POINT era;
int i;
srand(time(NULL));
switch (message) /* handle the messages */
{
case WM_CREATE:
SetTimer(hwnd,1,intv,NULL);
break;
case WM_DESTROY:
KillTimer(hwnd,1);
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_SIZE:
bound2.x=LOWORD(lParam);
bound2.y=HIWORD(lParam);
x=bound2.x/2;y=bound2.y/2;
break;
case WM_TIMER:
era=a.node[a.num-1];
for(i=a.num-1;i>=1;i--)
a.node[i]=a.node[i-1];
switch(a.direct)
{
case UP:
a.node[0].y-=20;break;
case DOWN:
a.node[0].y+=20;break;
case LEFT:
a.node[0].x-=20;break;
case RIGHT:
a.node[0].x+=20;break;
}
InvalidateRect(hwnd,NULL,FALSE);
break;
case WM_PAINT:
if(a.node[0].x==food.x&&a.node[0].y==food.y)
{
a.num++;
a.node[a.num-1]=a.node[a.num-2];
//防止食物产生在蛇的身上
for(bool judge=true;judge==true;)
{food.x=rand()%(bound2.x/20)*20;
food.y=(rand()%((bound2.y-20)/20))*20+20;
for(int i=0;i<a.num;i++)
if(food.x==a.node[i].x&&food.y==a.node[i].y)
judge=true;
else
judge=false;
}
score+=10;
if(score%100==0)//能被100,蛇的移动速度减慢一次
{
intv-=30;
KillTimer(hwnd,1);
SetTimer(hwnd,1,intv,NULL);
rank++;//在记录分数的时候,分数每增长一位rank需要加1
}
}
for(i=1;i<=a.num-1;i++)
{
if((a.node[0].x==a.node[i].x&&a.node[0].y==a.node[i].y)||ismax(a.node[0],bound1,bound2,19))
a.life=0;
}
if(a.life==1)
{
wsprintf(sco,TEXT("SCORE: %5d LV: %d"),score,rank);
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,20,0,sco,lstrlen(sco));
SelectObject(hdc,hpen2);
MoveToEx(hdc,0,1,NULL);
LineTo(hdc,bound2.x,1);
MoveToEx(hdc,0,16,NULL);
LineTo(hdc,bound2.x,16);
SelectObject(hdc,hpen1);
rect(hdc,food,17);
SelectObject(hdc,hpen);
rect(hdc,a.node[0],17);
SelectObject(hdc,hpenw);
rect(hdc,era,17);
EndPaint(hwnd,&ps);
}
else
{
if(MessageBox(NULL,TEXT("a"),TEXT("b"),0)==IDOK);
PostQuitMessage (0);
}
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_UP:
if(a.direct!=UP&&a.direct!=DOWN)
a.direct=UP;
break;
case VK_DOWN:
if(a.direct!=UP&&a.direct!=DOWN)
a.direct=DOWN;
break;
case VK_RIGHT:
if(a.direct!=RIGHT&&a.direct!=LEFT)
a.direct=RIGHT;
break;
case VK_LEFT:
if(a.direct!=RIGHT&&a.direct!=LEFT)
a.direct=LEFT;
break;
}
InvalidateRect(hwnd,NULL,FALSE);
return 0;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
效果图:
Windows API 贪吃蛇代码
最新推荐文章于 2015-12-27 15:50:36 发布