贪吃蛇图形界面版可编译运行.docx

上传人:b****5 文档编号:6826987 上传时间:2023-01-10 格式:DOCX 页数:17 大小:18.45KB
下载 相关 举报
贪吃蛇图形界面版可编译运行.docx_第1页
第1页 / 共17页
贪吃蛇图形界面版可编译运行.docx_第2页
第2页 / 共17页
贪吃蛇图形界面版可编译运行.docx_第3页
第3页 / 共17页
贪吃蛇图形界面版可编译运行.docx_第4页
第4页 / 共17页
贪吃蛇图形界面版可编译运行.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

贪吃蛇图形界面版可编译运行.docx

《贪吃蛇图形界面版可编译运行.docx》由会员分享,可在线阅读,更多相关《贪吃蛇图形界面版可编译运行.docx(17页珍藏版)》请在冰豆网上搜索。

贪吃蛇图形界面版可编译运行.docx

贪吃蛇图形界面版可编译运行

只要建个WIN32应用程序就行了。

实测可编译运行!

#include

#include

#include

#include//rand()

#include//time()为了用时间产生随机数种子srand()

#defineRowOfFrame20

#defineColumnOfFrame20

#defineWidth16

#defineHeight16

boolkeydown=false;

intrandomx;

intrandomy;

structNode//蛇节

{

intx;

inty;

Node*next;

};

typedefstruct//蛇

{

intlength;//蛇长

Node*head;//蛇头

Node*tail;//蛇尾

}Snake;

enumDirection

{

up=1,down,left,right

};

 

DirectionCurrentDirection;

Snake*greedsnake=newSnake;//贪吃蛇

LRESULTCALLBACKWinProc(

HWNDhwnd,

UINTuMsg,

WPARAMwParam,

LPARAMlParam);

intWINAPIWinMain(

HINSTANCEhInstance,

HINSTANCEhPrevInstance,

LPSTRlpCmdLine,

intnShowCmd)

{

//designthewindowclass

WNDCLASSwndcls;

wndcls.cbClsExtra=0;

wndcls.cbWndExtra=0;

wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);

wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);

wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);

wndcls.hInstance=hInstance;

wndcls.lpfnWndProc=WinProc;

wndcls.lpszClassName="weixin";

wndcls.lpszMenuName=NULL;

wndcls.style=CS_HREDRAW|CS_VREDRAW;

//registerthewindowclass

RegisterClass(&wndcls);

//createthewindow

HWNDhwnd;

hwnd=CreateWindow("weixin","贪吃蛇游戏",WS_OVERLAPPEDWINDOW&(~WS_MAXIMIZEBOX)&(~WS_SIZEBOX),0,0,330,360,NULL,NULL,hInstance,NULL);

//showthewindow

ShowWindow(hwnd,SW_SHOWNORMAL);

UpdateWindow(hwnd);

//themessageloop

MSGmsg;

while(GetMessage(&msg,NULL,0,0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

return0;

}

boolJudge(Snake*greedsnake,intrandomx,intrandomy)//判断随机食物会不会在蛇身上

{

Node*node1=greedsnake->head;

intlength=greedsnake->length;

while(length--)

{

if(node1->x==randomx&&node1->y==randomy)

returntrue;//蛇中含有node1节点

node1=node1->next;

}

returnfalse;//蛇中没有node1节点

}

voidrandomize(Snake*greedsnake,int&randomx,int&randomy)//随机产生食物的位置

{

srand((unsigned)time(NULL));

randomx=rand()%RowOfFrame;

randomy=rand()%ColumnOfFrame;

while(Judge(greedsnake,randomx,randomy))

{

randomx=rand()%RowOfFrame;

randomy=rand()%ColumnOfFrame;

}

}

 

voidDrawFood(HDChdc,intrandomx,intrandomy)//在蛇吃到之前只能画一次!

{

//COLORREFcolor;

//color=SetDCBrushColor(hdc,RGB(R,G,B));

//SetDCBrushColor(hdc,RGB(R,G,B));

//HBRUSHhbrush2=(HBRUSH)GetStockObject(DC_BRUSH);

HPENhpen=CreatePen(PS_SOLID,6,RGB(20,200,200));

SelectObject(hdc,hpen);

//HBRUSHhbrush2=(HBRUSH)GetStockObject(RGB(80,80,80));

//SelectObject(hdc,hbrush2);

//SelectObject(hdc,GetStockObject(DC_BRUSH));

//SetDCBrushColor(hdc,RGB(R,G,B));

Rectangle(hdc,Width*randomx+4,Height*randomy+4,Width*randomx+12,Height*randomy+12);

//SelectObject(hdc,WHITE_BRUSH);

}

//界面设计

voidDesign(HDChdc)

{

HPENhpen;

hpen=CreatePen(PS_DOT,1,RGB(255,0,0));

HBRUSHhbrush;

hbrush=(HBRUSH)GetStockObject(WHITE_BRUSH);

SelectObject(hdc,hpen);

SelectObject(hdc,hbrush);

inti;

for(i=0;i<=RowOfFrame;i++)

{

if(MoveToEx(hdc,Width*i,0,NULL))//设置起始点

LineTo(hdc,Width*i,Width*RowOfFrame);

}

for(i=0;i<=ColumnOfFrame;i++)

{

if(MoveToEx(hdc,0,Height*i,NULL))

LineTo(hdc,Height*ColumnOfFrame,Height*i);

}

}

//初始化蛇,包括蛇长,蛇的初始位置,开始蛇的运动方向

voidInit(Snake*greedsnake)

{

Node*node1=newNode;

node1->x=0;

node1->y=0;

node1->next=NULL;

greedsnake->head=node1;

greedsnake->length=1;

greedsnake->tail=node1;

CurrentDirection=right;

}

//画蛇

voidDrawSnake(HDChdc,Snake*greedsnake)

{

intlength=greedsnake->length;

intx;

inty;

x=greedsnake->head->x*Width;

y=greedsnake->head->y*Height;

MoveToEx(hdc,x+8,y+8,NULL);

//下面注释的代码是完成画花蛇的功能,但是因为时间的问题会导致程序的运行不正确

//给画笔产生随机颜色

/*intR,G,B;

srand((unsigned)time(NULL));

R=rand()%256;

G=rand()%256;

B=rand()%256;

HPENhpen=CreatePen(PS_SOLID,6,RGB(R,G,B));

*/

//给画笔产生固定的蓝色

HPENhpen=CreatePen(PS_SOLID,6,RGB(0,0,255));

SelectObject(hdc,hpen);

if(length==1)

{

LineTo(hdc,x+16,y+8);

}

if(length>1)

{

Node*node2=newNode;

node2=greedsnake->head;

while(--length)

{

//随机产生画笔的颜色

/*R=rand()%256;

G=rand()%256;

B=rand()%256;

hpen=CreatePen(PS_SOLID,6,RGB(R,G,B));

SelectObject(hdc,hpen);

*/

node2=node2->next;

intx2;

inty2;

x2=node2->x*Width+8;

y2=node2->y*Height+8;

LineTo(hdc,x2,y2);

MoveToEx(hdc,x2,y2,NULL);

}

}

}

//判断两个节点的坐标是否相等

boolEqual(Node*node1,Node*nodetemp)

{

if(node1->x==nodetemp->x&&node1->y==nodetemp->y)

returntrue;

elsereturnfalse;

}

//判断是否犯规nodetemp表示下一个即将入队的节点

boolHit(Snake*greedsnake,Node*nodetemp)

{

//judgeifthegreedsnakehitthewall

if(nodetemp->x<0||nodetemp->x>=20||nodetemp->y<0||nodetemp->y>=20)

returntrue;//hitthewall

//judgeifthegreedsnakehititself

Node*node1;

node1=greedsnake->head;

intlength=greedsnake->length;

while(length--)

{

if(Equal(node1,nodetemp))

returntrue;//hititself

else

node1=node1->next;

}

returnfalse;

}

//求蛇下一个运动的地点

voidNextNode(Snake*greedsnake,Node*node1,charc)//calculatethenextpoint

{

keydown=true;

if(c=='w')//up

{

if(CurrentDirection==down)

{

node1->x=greedsnake->tail->x;

node1->y=greedsnake->tail->y+1;

}

else

{

node1->x=greedsnake->tail->x;

node1->y=greedsnake->tail->y-1;

CurrentDirection=up;

}

}

elseif(c=='a')//left

{

if(CurrentDirection==right)

{

node1->x=greedsnake->tail->x+1;

node1->y=greedsnake->tail->y;

}

else

{

node1->x=greedsnake->tail->x-1;

node1->y=greedsnake->tail->y;

CurrentDirection=left;

}

}

elseif(c=='s')//down

{

if(CurrentDirection==up)

{

node1->x=greedsnake->tail->x;

node1->y=greedsnake->tail->y-1;

}

else

{

node1->x=greedsnake->tail->x;

node1->y=greedsnake->tail->y+1;

CurrentDirection=down;

}

}

elseif(c=='d')//right

{

if(CurrentDirection==left)

{

node1->x=greedsnake->tail->x-1;

node1->y=greedsnake->tail->y;

}

else

{

node1->x=greedsnake->tail->x+1;

node1->y=greedsnake->tail->y;

CurrentDirection=right;

}

}

}

//将下一个地点加入到蛇身上

voidPush(Snake*greedsnake,Node*node1)

{

//判断是不是食物

if(node1->x!

=randomx||node1->y!

=randomy)//itisnotthelocationoffood

{

greedsnake->tail->next=node1;

greedsnake->tail=node1;

Node*node2=greedsnake->head;

greedsnake->head=node2->next;

deletenode2;

}

else

{

greedsnake->tail->next=node1;

greedsnake->tail=node1;

greedsnake->length++;//吃到食物,蛇的长度要增1

randomize(greedsnake,randomx,randomy);

}

}

//销毁蛇

voidDestroy(Snake*greedsnake)

{

Node*node1;

node1=greedsnake->head;

while(node1->next!

=NULL)

{

Node*node2;

node2=node1;

node1=node1->next;

deletenode2;

greedsnake->length--;

}

}

 

LRESULTCALLBACKWinProc(

HWNDhwnd,

UINTuMsg,

WPARAMwParam,

LPARAMlParam

{

switch(uMsg)

{

caseWM_CHAR:

Node*node1;

node1=newNode;

node1->next=NULL;

RECTrect;

GetClientRect(hwnd,&rect);

HDChdc;

hdc=GetDC(hwnd);

HBRUSHhbrush;

hbrush=(HBRUSH)GetStockObject(WHITE_BRUSH);

SelectObject(hdc,hbrush);

switch(wParam)

{

case'w':

//UP

NextNode(greedsnake,node1,'w');

if(Hit(greedsnake,node1))

{

charsz[40];

sprintf(sz,"Thescoreis%d.\nClickOKtoexit",greedsnake->length);

KillTimer(hwnd,1);

MessageBox(hwnd,sz,"YouFailed",MB_OK);

DestroyWindow(hwnd);

}

Push(greedsnake,node1);

break;

case'a':

//LEFT

NextNode(greedsnake,node1,'a');

if(Hit(greedsnake,node1))

{

charsz[40];

sprintf(sz,"Thescoreis%d.\nClickOKtoexit",greedsnake->length);

KillTimer(hwnd,1);

MessageBox(hwnd,sz,"YouFailed",MB_OK);

DestroyWindow(hwnd);

}

Push(greedsnake,node1);

break;

case's':

//DOWN

NextNode(greedsnake,node1,'s');

if(Hit(greedsnake,node1))

{

charsz[40];

sprintf(sz,"Thescoreis%d.\nClickOKtoexit",greedsnake->length);

KillTimer(hwnd,1);

MessageBox(hwnd,sz,"YouFailed",MB_OK);

DestroyWindow(hwnd);

}

Push(greedsnake,node1);

break;

case'd':

//RIGHT

NextNode(greedsnake,node1,'d');

if(Hit(greedsnake,node1))

{

charsz[40];

sprintf(sz,"Thescoreis%d.\nClickOKtoexit",greedsnake->length);

KillTimer(hwnd,1);

MessageBox(hwnd,sz,"YouFailed",MB_OK);

DestroyWindow(hwnd);

}

Push(greedsnake,node1);

break;

default:

//不是规定的按键,应该将node1节点空间删除掉

deletenode1;

break;

}

Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);//擦除原来的界面

//绘制新的界面

Design(hdc);

DrawSnake(hdc,greedsnake);

DrawFood(hdc,randomx,randomy);

ReleaseDC(hwnd,hdc);

break;

caseWM_PAINT:

PAINTSTRUCTps;

HDChdc2;

hdc2=BeginPaint(hwnd,&ps);

//画界面函数

Design(hdc2);

DrawSnake(hdc2,greedsnake);

DrawFood(hdc2,randomx,randomy);

EndPaint(hwnd,&ps);

break;

caseWM_CLOSE:

KillTimer(hwnd,1);

DestroyWindow(hwnd);

break;

caseWM_DESTROY:

PostQuitMessage(0);

break;

caseWM_CREATE:

//做初始化的一些工作

Init(greedsnake);

randomize(greedsnake,randomx,randomy);

MessageBox(hwnd,"上(w)下(s)左(a)右(d)","操作说明",0);

//TIMERtimer;

SetTimer(hwnd,1,1000,NULL);

break;

caseWM_TIMER:

if(!

keydown)

{

Node*node_temp;

node_temp=newNode;

node_temp->next=NULL;

 

if(CurrentDirection==up)

{

node_temp->x=greedsnake->tail->x;

node_temp->y=greedsnake->tail->y-1;

if(Hit(greedsnake,node_temp))

{

charsz[40];

sprintf(sz,"Thescoreis%d.\nClickOKtoexit",greedsnake->length);

KillTimer(hwnd,1);

MessageBox(hwnd,sz,"YouFailed",MB_OK);

DestroyWindow(hwnd);

}

}

elseif(CurrentDirection==down)

{

node_temp->x=greedsnake->tail-

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1