题目是我们c++的一个课设题目,用了将近一个礼拜的的时间写成了贪吃蛇的大框,写了两个版本的贪吃蛇,第二个版本的相较于第一个版本有点画面和功能上有了一定的改善,但是内部逻辑是基本没有变化的
源码如下
1
#include<windows.h>
#include<tchar.h>
#include<math.h>
#include<stdio.h>
#include<time.h>
#define NUM 1000
BOOL InitInstance(HINSTANCE hInstance,int nCmdShow);
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL PreTranslateMessage(MSG *pMsg);
HWND hWnd;//窗口创建句柄
static int cxClient, cyClient,ge;//屏幕大小
HDC hdc;
PAINTSTRUCT ps;
POINT grid[20][20];
int d=VK_DOWN,predect;
struct point//蛇的节点的结构体嵌入在蛇类中的
{
int x;
int y;
point* next;
};
class FOOD//食物类包含了刷新位置以及初始位置,引用了一个point节点
{
public:
POINT decision;
FOOD()
{
srand(time(NULL));
decision.x=rand()%20;
decision.y=rand()%20;
}
void re()
{
srand(time(NULL));
decision.x=rand()%20;
decision.y=rand()%20;
}
}food;
class SNAKE//蛇的头和蛇的身体
{
public:
point head;
SNAKE()
{
head={
9,9,NULL};
head.next=new point;
*(head.next)={
9,8,NULL};
}
}snake;
void show()//内容包括蛇和食物渐变色的绘制
{
hdc=BeginPaint(hWnd,&ps);//开始绘画
point* p=&snake.head;
for(int i=0;i<ge/20;i++)//食物
{
for(int j=0;j<ge/20;j++)
{
SetPixel(hdc,grid[food.decision.x][food.decision.y].x+i,grid[food.decision.x][food.decision.y].y+j,RGB(i*5,j*5,rand()));
}
}
for(int i=0;i<cyClient;i++)//以下四个是四周的绘制
SetPixel(hdc,cyClient,i,RGB(i*5,i*5,rand()));
for(int i=0;i<cyClient;i++)
SetPixel(hdc,1,cyClient-i,RGB(i*5,i*5,rand()));
for(int i=0;i<cyClient;i++)
SetPixel(hdc,i,0,RGB(i*5,i*5,rand()));
for(int i=0;i<cyClient;i++)
SetPixel(hdc,i,cyClient-2,RGB(i*5,i*5,rand()));
while(p)//蛇绘画
{
for(int i=0;i<ge/20;i++)
{
for(int j=0;j<ge/20;j++)
{
SetPixel(hdc,grid[p->x][p->y].x+i,grid[p->x][p->y].y+j,RGB(i*10,j*10,rand()));
}
}
p=p->next;
}
EndPaint(hWnd,&ps);
}
bool dead()//整个函数是用来判断是否死亡的
{
int i,j;
point* judge=&snake.head;
i=judge->x;
j=judge->y;
while(judge)//撞自己身上;
{
if(d==VK_DOWN)
{
if(snake.head.y+1==judge->y&&snake.head.x==judge->x)
return false;
}
else if(d==VK_RIGHT)
{
if(snake.head.x+1==judge->x&&snake.head.y==judge->y)
return false;
}
else if(d==VK_LEFT)
{
if(snake.head.x-1==judge->x&&snake.head.y==judge->y)
return false;
}
else if(d==VK_UP)
{
if(snake.head.y-1==judge->y&&snake.head.x==judge->x)
return false;
}
judge=judge->next;
}
if(d==VK_DOWN)//撞墙
{
j++;
if(j==20)
return false;
}
else if(d==VK_RIGHT)
{
i++;
if(i==20)
return false;
}
else if(d==VK_LEFT)
{
i--;
if(i==-1)
return false;
}
else if(d==VK_UP)
{
j--;
if(j==-1)
return false;
}
return true;
}
void eat()//判断是否吃到食物
{
point* eaten=snake.head.next;
while(eaten->next)
{
eaten=eaten->next;
}
if(snake.head.x==food.decision.x&&snake.head.y==food.decision.y)
{
eaten->next=new point;
eaten->next->x=eaten->x;
eaten->next->y=eaten->y;
eaten->next->next=NULL;
food.re();
}
}
void predected()//避免一次移动相反方向
{
if(predect==VK_UP)
if(d==VK_DOWN);
else
d=predect;
if(predect==VK_DOWN)
if(d==VK_UP);
else
d=predect;
if(predect==VK_RIGHT)
if(d==VK_LEFT);
else
d=predect;
if(predect==VK_LEFT)
if(d==VK_RIGHT);
else
d=predect;
}
void move()
{
predected();
int& i=snake.head.x,& j=snake.head.y;
if(dead())
{
point* moving=&snake.head,change1=snake.head,change2;
while(moving->next)
{
change2=*(moving->next);
moving->next->x=change1.x;
moving->next->y=change1.y;
moving=moving->next;
change1=change2;
}
if(d==VK_DOWN)
{
j++;
}
else if(d==VK_RIGHT)
{
i++;
}
else if(d==VK_LEFT)
{
i--;
}