问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

贪吃蛇编程

发布网友 发布时间:2022-04-23 13:57

我来回答

1个回答

热心网友 时间:2023-10-16 23:11

这里有我以前敲过的贪吃蛇,不介意可以参考一下,(ps:需要建一了application而不是console application)

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <time.h>

#define speed 200
#define left 100
#define top 50
#define right 500
#define bottom 350

struct snake
{
POINT pos;
snake *front,*next;
};

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
bool InitWindow(HINSTANCE,int);
void items(HDC,int,int);
void MoveSnake(int);
void remove();
void destroy();
void inf(int,int,int,int);
void inf(const char*,int,int,int,int);
void inf(const char*,int,int,int);
bool is_fail();
void eat();
void wall();
void show_fruit();

UINT ShowMode;
PAINTSTRUCT ps;
bool onoff;
HFONT hf;
char judge[20];
int dir;
HDC hdc;
HWND hwnd;
HBRUSH BlackBrush,MainBrush,NULLBrush;
HPEN MainPen,BlackPen;
snake *head,*tail,*temp;
POINT fruit;

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
onoff=1;
int i;
dir=-2;
tail=NULL;
InitWindow(hInstance,nCmdShow);
for(i=0;i<5;i++)
{
if(!tail)
{
head=new snake;
tail=head;
head->pos.x=300+i*10;
head->pos.y=200;
items(hdc,head->pos.x,head->pos.y);
}
else
{
tail->next=new snake;
tail->next->front=tail;
tail=tail->next;
tail->pos.x=300+i*10;
tail->pos.y=200;
tail->next=NULL;
items(hdc,tail->pos.x,tail->pos.y);
}
}
hf=CreateFont(20,0,0,0,400,0,0,0,GB2312_CHARSET,
OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,"华文行楷");
SelectObject(hdc,hf);
SetBkColor(hdc,RGB(124,146,131));
SelectObject(hdc,MainBrush);
wall();
show_fruit();
inf("得分:",260,0,0);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
destroy();
return 0;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
static int seed=15;
switch(msg)
{
case WM_TIMER:
MoveSnake(dir);
if(!is_fail())
{
KillTimer(hwnd,1);
MessageBox(hwnd,"you are lost","caption",0);
}
eat();
break;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
wall();
EndPaint(hwnd,&ps);
hdc=GetDC(hwnd);
case WM_KEYDOWN:
switch(wParam)
{
case VK_UP:dir=(dir!=1?-1:dir);break;
case VK_DOWN:dir=(dir!=-1?1:dir);break;
case VK_RIGHT:dir=(dir!=-2?2:dir);break;
case VK_LEFT:dir=(dir!=2?-2:dir);break;
}
break;
case WM_CLOSE:
if(MessageBox(hwnd,"确定退出?","caption",MB_YESNO)==IDYES)
DestroyWindow(hwnd);
break;
case WM_DESTROY:
ReleaseDC(hwnd,hdc);
KillTimer(hwnd,1);
PostQuitMessage(0);
break;
default:return DefWindowProc(hwnd,msg,wParam,lParam);
}

return 1;
}

bool InitWindow(HINSTANCE hInstance,int nCmdShow)
{
WNDCLASS wc;
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpszClassName="test";
wc.lpszMenuName=NULL;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=CreateSolidBrush(RGB(124,146,131));
wc.hIcon=NULL;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hInstance=hInstance;
wc.lpfnWndProc=WndProc;

MainBrush=CreateSolidBrush(RGB(124,146,131));
BlackBrush=(HBRUSH)GetStockObject(BLACK_BRUSH);
NULLBrush=(HBRUSH)GetStockObject(NULL_BRUSH);
MainPen=CreatePen(PS_SOLID,1,RGB(124,146,131));
BlackPen=CreatePen(PS_SOLID,1,RGB(0,0,0));

if(!RegisterClass(&wc)) return false;
hwnd=CreateWindow("test","贪吃蛇",WS_SYSMENU,400,150,616,400,NULL,NULL,hInstance,NULL);
hdc=BeginPaint(hwnd,&ps);
if(!hwnd) return false;

ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
SetTimer(hwnd,1,speed,NULL);
return true;
}

void items(HDC hdc,int x,int y)
{
SelectObject(hdc,BlackPen);
SelectObject(hdc,NULLBrush);
Rectangle(hdc,x,y,x+10,y+10);
SelectObject(hdc,BlackBrush);
Rectangle(hdc,x+2,y+2,x+8,y+8);
// DeleteObject(BlackPen);
// DeleteObject(BlackBrush);
}

void MoveSnake(int dir)
{
static int i=0;
remove();
tail=tail->front;
delete tail->next;
tail->next=NULL;
temp=new snake;
temp->next=head;
head->front=temp;
temp->pos.x=head->pos.x+(dir/2)*10;
temp->pos.y=head->pos.y+(dir%2)*10;
head=temp;
i++;
items(hdc,head->pos.x,head->pos.y);
}

void remove()
{
SelectObject(hdc,MainBrush);
SelectObject(hdc,MainPen);
Rectangle(hdc,tail->pos.x,tail->pos.y,tail->pos.x+10,tail->pos.y+10);
// DeleteObject(MainBrush);
// DeleteObject(MainPen);
}

void destroy()
{
while(head->next)
{
head=head->next;
delete head->front;
}
delete tail;
}

void inf(int x,int y,int px,int py)
{
inf("",x,y,px,py);
}
void inf(const char*str,int x,int y,int scores)
{
sprintf(judge,"%s%d",str,scores);
TextOut(hdc,x,y,judge,strlen(judge));
}
void inf(const char*str,int x,int y,int px,int py)
{
sprintf(judge,"%s(%d,%d) ",str,px,py);
TextOut(hdc,x,y,judge,strlen(judge));
}

bool is_fail()
{
temp=head;
int px=head->pos.x,py=head->pos.y;
if(px<left||px>=right||py<top||py>=bottom)
return 0;
while(temp->next)
{
temp=temp->next;
if(px==temp->pos.x&&py==temp->pos.y)
return 0;
}
return 1;

}

void show_fruit()
{
srand((UINT)time(NULL));
fruit.x=10*((rand()%(right-left-10))/10)+left;
fruit.y=10*((rand()%(bottom-top-10))/10)+top;
items(hdc,fruit.x,fruit.y);
}

void eat()
{
inf("食物:",0,25,fruit.x,fruit.y);
inf("蛇头:",0,0,head->pos.x,head->pos.y);
static int scores=0;
if(head->pos.x==fruit.x&&head->pos.y==fruit.y)
{
scores++;
inf("得分:",260,0,scores);
KillTimer(hwnd,1);
temp=new snake;
temp->next=head;
head->front=temp;
temp->pos.x=head->pos.x+(dir/2)*10;
temp->pos.y=head->pos.y+(dir%2)*10;
head=temp;
items(hdc,head->pos.x,head->pos.y);
SetTimer(hwnd,1,speed,NULL);
show_fruit();
}
}

void wall()
{
SelectObject(hdc,MainBrush);
Rectangle(hdc,left-1,top-1,right+1,bottom+1);
// DeleteObject(MainBrush);
}

热心网友 时间:2023-11-08 00:09

这里有我以前敲过的贪吃蛇,不介意可以参考一下,(ps:需要建一了application而不是console application)

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <time.h>

#define speed 200
#define left 100
#define top 50
#define right 500
#define bottom 350

struct snake
{
POINT pos;
snake *front,*next;
};

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
bool InitWindow(HINSTANCE,int);
void items(HDC,int,int);
void MoveSnake(int);
void remove();
void destroy();
void inf(int,int,int,int);
void inf(const char*,int,int,int,int);
void inf(const char*,int,int,int);
bool is_fail();
void eat();
void wall();
void show_fruit();

UINT ShowMode;
PAINTSTRUCT ps;
bool onoff;
HFONT hf;
char judge[20];
int dir;
HDC hdc;
HWND hwnd;
HBRUSH BlackBrush,MainBrush,NULLBrush;
HPEN MainPen,BlackPen;
snake *head,*tail,*temp;
POINT fruit;

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
onoff=1;
int i;
dir=-2;
tail=NULL;
InitWindow(hInstance,nCmdShow);
for(i=0;i<5;i++)
{
if(!tail)
{
head=new snake;
tail=head;
head->pos.x=300+i*10;
head->pos.y=200;
items(hdc,head->pos.x,head->pos.y);
}
else
{
tail->next=new snake;
tail->next->front=tail;
tail=tail->next;
tail->pos.x=300+i*10;
tail->pos.y=200;
tail->next=NULL;
items(hdc,tail->pos.x,tail->pos.y);
}
}
hf=CreateFont(20,0,0,0,400,0,0,0,GB2312_CHARSET,
OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,"华文行楷");
SelectObject(hdc,hf);
SetBkColor(hdc,RGB(124,146,131));
SelectObject(hdc,MainBrush);
wall();
show_fruit();
inf("得分:",260,0,0);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
destroy();
return 0;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
static int seed=15;
switch(msg)
{
case WM_TIMER:
MoveSnake(dir);
if(!is_fail())
{
KillTimer(hwnd,1);
MessageBox(hwnd,"you are lost","caption",0);
}
eat();
break;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
wall();
EndPaint(hwnd,&ps);
hdc=GetDC(hwnd);
case WM_KEYDOWN:
switch(wParam)
{
case VK_UP:dir=(dir!=1?-1:dir);break;
case VK_DOWN:dir=(dir!=-1?1:dir);break;
case VK_RIGHT:dir=(dir!=-2?2:dir);break;
case VK_LEFT:dir=(dir!=2?-2:dir);break;
}
break;
case WM_CLOSE:
if(MessageBox(hwnd,"确定退出?","caption",MB_YESNO)==IDYES)
DestroyWindow(hwnd);
break;
case WM_DESTROY:
ReleaseDC(hwnd,hdc);
KillTimer(hwnd,1);
PostQuitMessage(0);
break;
default:return DefWindowProc(hwnd,msg,wParam,lParam);
}

return 1;
}

bool InitWindow(HINSTANCE hInstance,int nCmdShow)
{
WNDCLASS wc;
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpszClassName="test";
wc.lpszMenuName=NULL;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=CreateSolidBrush(RGB(124,146,131));
wc.hIcon=NULL;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hInstance=hInstance;
wc.lpfnWndProc=WndProc;

MainBrush=CreateSolidBrush(RGB(124,146,131));
BlackBrush=(HBRUSH)GetStockObject(BLACK_BRUSH);
NULLBrush=(HBRUSH)GetStockObject(NULL_BRUSH);
MainPen=CreatePen(PS_SOLID,1,RGB(124,146,131));
BlackPen=CreatePen(PS_SOLID,1,RGB(0,0,0));

if(!RegisterClass(&wc)) return false;
hwnd=CreateWindow("test","贪吃蛇",WS_SYSMENU,400,150,616,400,NULL,NULL,hInstance,NULL);
hdc=BeginPaint(hwnd,&ps);
if(!hwnd) return false;

ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
SetTimer(hwnd,1,speed,NULL);
return true;
}

void items(HDC hdc,int x,int y)
{
SelectObject(hdc,BlackPen);
SelectObject(hdc,NULLBrush);
Rectangle(hdc,x,y,x+10,y+10);
SelectObject(hdc,BlackBrush);
Rectangle(hdc,x+2,y+2,x+8,y+8);
// DeleteObject(BlackPen);
// DeleteObject(BlackBrush);
}

void MoveSnake(int dir)
{
static int i=0;
remove();
tail=tail->front;
delete tail->next;
tail->next=NULL;
temp=new snake;
temp->next=head;
head->front=temp;
temp->pos.x=head->pos.x+(dir/2)*10;
temp->pos.y=head->pos.y+(dir%2)*10;
head=temp;
i++;
items(hdc,head->pos.x,head->pos.y);
}

void remove()
{
SelectObject(hdc,MainBrush);
SelectObject(hdc,MainPen);
Rectangle(hdc,tail->pos.x,tail->pos.y,tail->pos.x+10,tail->pos.y+10);
// DeleteObject(MainBrush);
// DeleteObject(MainPen);
}

void destroy()
{
while(head->next)
{
head=head->next;
delete head->front;
}
delete tail;
}

void inf(int x,int y,int px,int py)
{
inf("",x,y,px,py);
}
void inf(const char*str,int x,int y,int scores)
{
sprintf(judge,"%s%d",str,scores);
TextOut(hdc,x,y,judge,strlen(judge));
}
void inf(const char*str,int x,int y,int px,int py)
{
sprintf(judge,"%s(%d,%d) ",str,px,py);
TextOut(hdc,x,y,judge,strlen(judge));
}

bool is_fail()
{
temp=head;
int px=head->pos.x,py=head->pos.y;
if(px<left||px>=right||py<top||py>=bottom)
return 0;
while(temp->next)
{
temp=temp->next;
if(px==temp->pos.x&&py==temp->pos.y)
return 0;
}
return 1;

}

void show_fruit()
{
srand((UINT)time(NULL));
fruit.x=10*((rand()%(right-left-10))/10)+left;
fruit.y=10*((rand()%(bottom-top-10))/10)+top;
items(hdc,fruit.x,fruit.y);
}

void eat()
{
inf("食物:",0,25,fruit.x,fruit.y);
inf("蛇头:",0,0,head->pos.x,head->pos.y);
static int scores=0;
if(head->pos.x==fruit.x&&head->pos.y==fruit.y)
{
scores++;
inf("得分:",260,0,scores);
KillTimer(hwnd,1);
temp=new snake;
temp->next=head;
head->front=temp;
temp->pos.x=head->pos.x+(dir/2)*10;
temp->pos.y=head->pos.y+(dir%2)*10;
head=temp;
items(hdc,head->pos.x,head->pos.y);
SetTimer(hwnd,1,speed,NULL);
show_fruit();
}
}

void wall()
{
SelectObject(hdc,MainBrush);
Rectangle(hdc,left-1,top-1,right+1,bottom+1);
// DeleteObject(MainBrush);
}
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
泰勒公式有哪些常见形式? 空调室内风机怎样工作 什么是县级以上医院 关于聚金宝 买白银问题 平安银行个人贵金属交易业务有哪些? 网易云音乐为什么歌这么少? 公路边种的有什么花 高速路上常用的绿化花卉有那些? 公路绿化用什么花 我一键开核后顺利进入WIN7 系统,可是一开网站就重启是怎么回事,我开6... 嘴唇上好多小颗粒,淡白色,密密麻麻是怎么回事,谁能告诉我要任何解决,急 求贪吃蛇的程序代码(c语言) 嘴巴上唇长很多白色小颗粒,像鱼籽一样的,请问是什么原因,应该怎么去除? 我嘴唇上长了一些小颗粒,有好几年了,不疼不痒可是好像越来越多了,嘴角处最多,这是什么病?要怎么治啊 我嘴角里面出现很多小颗粒,怎么回事 嘴唇上面长了一些白色的小颗粒要怎么消除 嘴巴周围下巴长很多小颗粒,该怎么办 嘴边有哪种摸起来的小颗粒怎么去掉? 一个女头像背景是蓝色头顶拿着花情侣头像 卡通情侣头像女的胸前拿花 求情侣头像,女的头上戴花,且有花遮住左眼,求另一半的图片。在线等,急求! 情侣头像,带花,只要带花的都给我一下 求情侣头像有花遮脸的 大概是你喜欢花,而我姓叶是情侣头像吗? 彼岸花情侣头像 情侣头像一左一右 手里拿花 情侣头像,拿着花的图片和下面配图 求带花情侣头像像这种的,类似 求有关花的情侣头像 求关于玫瑰花的情侣头像, 上嘴唇有些黄色小颗粒 怎么办? 贪吃蛇plc编程思路 我的上嘴唇长了好多小颗粒(就上唇有),不痛不痒.谁能告诉是怎么回事啊!?怎样才能让它们消失呢? c#编程贪吃蛇 嘴唇上长了很多小颗粒 一般是什么引起上嘴唇有好多白色颗粒状白点,怎样进行 贪吃蛇游戏的C语言编程 上火了,嘴角上长了几颗粒粒,怎么办? 用C语言编写贪吃蛇游戏的程序 嘴唇、下巴上长了好多的小颗粒,怎么办 我的上嘴唇长了这样的东西,密密麻麻的小颗粒在表皮内,不仔细看就是一片一片的白,不疼不痒的,就类似图 嘴角密密麻麻小颗粒 不痛不痒的。怎么办啊 好久了下不去 求贪吃蛇的C++程序代码 嘴唇上有小颗粒很多是怎么了 如何自己编写一个贪吃蛇的游戏软件? 鼻子和嘴附近有小颗粒像鸡皮疙瘩一样里面有脂肪粒怎么解决 在dos环境下c语言编程编一个贪吃蛇游戏 嘴巴周围长白包颗粒是脂肪粒吗,怎么去除 关於贪吃蛇编程 嘴唇上长了好多小颗粒,不痛不痒,密密麻麻