您的位置:首页 > 其它

WIN32 贪食蛇改进版,新增计分、等级及暂停功能

2015-07-10 12:49 639 查看


#include<Windows.h>

#include<tchar.h>

#include<stdlib.h>

#include<stdio.h>

#define WIDTH 10;

#define LENGTH(x) x*WIDTH;

char str1[20],str2[20];

int mum1,mum2;

enum IsSnack{isSnack,isNotsnack,isFood};

IsSnack Map[40][40];

RECT playground;

class Snack

{

public:

Snack( int a,int b,Snack *c=NULL):x(a),y(b),next(c){}

public:

int x,y;

Snack *next;

}*head,*tail;

int direct=0;

int pause;

LRESULT CALLBACK WindowProc(HWND,UINT,WPARAM,LPARAM);

void Initializer();

void Controller(Snack *);

void Move(HWND);

void Putfood();

int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevhinstance,LPSTR lpcmdline,int cmdshow)

{

WNDCLASS wndclass;

wndclass.lpfnWndProc=WindowProc;

wndclass.cbWndExtra=0;

wndclass.cbClsExtra=0;

wndclass.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);

wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);

wndclass.hIcon = 0;

wndclass.hInstance=hinstance;

wndclass.lpszClassName=_T("我的窗体");

wndclass.lpszMenuName=0;

wndclass.style=CS_VREDRAW|CS_HREDRAW;

if(!RegisterClass(&wndclass))

{

MessageBox(0,_T("注册窗体类失败!"),_T("贪食蛇游戏"),MB_OK);

return 0;

}

HWND hwnd=CreateWindow(_T("我的窗体"),_T("贪食蛇游戏"),

WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,hinstance,0);

ShowWindow(hwnd,SW_SHOW);

UpdateWindow(hwnd);

MSG msg;

while(TRUE){

if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))

{

if(msg.message==WM_QUIT) break;

TranslateMessage(&msg);

DispatchMessage(&msg);

}

else Move(hwnd);

}

return msg.wParam;

}

LRESULT CALLBACK WindowProc(HWND hwnd,UINT umsg,WPARAM wparam,LPARAM lparam)

{

switch(umsg)

{

case WM_CLOSE:

PostQuitMessage(0);

case WM_CREATE:

Initializer();

MoveWindow(hwnd,50,50,550,550,TRUE);

case WM_KEYDOWN:

switch(wparam){

case VK_LEFT:

if(direct!=VK_RIGHT)

direct=VK_LEFT;

break;

case VK_RIGHT:

if(direct!=VK_LEFT)

direct=VK_RIGHT;

break;

case VK_DOWN:

if(direct!=VK_UP)

direct=VK_DOWN;

break;

case VK_UP:

if(direct!=VK_DOWN)

direct=VK_UP;

break;

case VK_SPACE:

++pause;

break;

default:

break;

}

return 0;

case WM_PAINT:

PAINTSTRUCT ps;

HDC hdc=BeginPaint(hwnd,&ps);

TextOut(hdc,100,0,_T("请按上、下、左、右键控制方向,空格键暂停!"),41);

HPEN hpen=CreatePen(PS_SOLID,1,RGB(255,255,255));

SelectObject(hdc,hpen);

Rectangle(hdc,75,28,450,55);

TextOut(hdc,130,30,str1,_tcslen(str1));

TextOut(hdc,300,30,str2,_tcslen(str2));

SetViewportOrgEx(hdc,60,60,NULL);

HBRUSH hbrush=CreateSolidBrush(RGB(122,180,255));

SelectObject(hdc,hbrush);

Rectangle(hdc,playground.left,playground.top,playground.right,playground.bottom);

DeleteObject(hbrush);

hbrush=CreateSolidBrush(RGB(255,0,0));

SelectObject(hdc,hbrush);

for(int i=0;i<40;i++)

for(int j=0;j<40;j++)

if(Map[i][j]==isSnack||Map[i][j]==isFood)

{

Rectangle(hdc,i*10,j*10,(i+1)*10,(j+1)*10);

}

DeleteObject(hbrush);

EndPaint(hwnd,&ps);

}

return DefWindowProc(hwnd,umsg,wparam,lparam);

}

void Initializer()

{

for(int i=0;i<40;i++)

for(int j=0;j<40;j++)

Map[i][j]=isNotsnack;

for(int i=0;i<5;i++)

{

if(i==0)

head=tail=new Snack(i,0);

else

{

Snack *temp=new Snack(i,0);

tail->next=temp;

tail=temp;

}

Map[i][0]=isSnack;

}

playground.left=playground.top=0;

playground.right=playground.bottom=400;

direct=VK_RIGHT;

Putfood();

mum1=0;

sprintf_s(str1,"吃掉食物:%d",mum1);

sprintf_s(str2,"当前等级:%d",mum2);

}

void Putfood()

{

int x,y;

do{

x=rand()%40;

y=rand()%40;

}while(Map[x][y]==isSnack);

Map[x][y]=isFood;

}

void Move(HWND hwnd)

{

Snack *temp=NULL;

switch(direct)

{

case VK_LEFT:

temp=new Snack(tail->x-1,tail->y);

break;

case VK_RIGHT:

temp=new Snack(tail->x+1,tail->y);

break;

case VK_UP:

temp=new Snack(tail->x,tail->y-1);

break;

case VK_DOWN:

temp=new Snack(tail->x,tail->y+1);

break;

default:

break;

}

if(pause%2==0)

Controller(temp);

InvalidateRect(hwnd,NULL,FALSE);

Sleep(300-28*mum2);

}

void Controller(Snack *temp)

{

if(temp->x<0||temp->x>=40||temp->y<0||temp->y>=40||Map[temp->x][temp->y]==isSnack)

{

MessageBox(0,_T("\\^_^/游戏结束了\\^_^/"),_T("贪食蛇游戏"),MB_OK);

delete temp;

while(head!=NULL)

{

Snack *p=head;

head=head->next;

delete p;

}

head=tail=temp=NULL;

Initializer();

return;

}

else if(Map[temp->x][temp->y]==isNotsnack)

{

Map[temp->x][temp->y]=isSnack;

Map[head->x][head->y]=isNotsnack;

Snack *ptr=head;

head=head->next;

delete ptr;

tail->next=temp;

tail=temp;

}

else

{

Map[temp->x][temp->y]=isFood;

tail->next=temp;

tail=temp;

Putfood();

++mum1;

sprintf_s(str1,"吃掉食物:%d",mum1);

}

mum2=int(mum1/10);

sprintf_s(str2,"当前等级:%d",mum2);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  win32 贪食蛇