求vc++6.0贪食蛇源代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求vc++6.0贪食蛇源代码相关的知识,希望对你有一定的参考价值。

请将源代码发送到evil0angel@126.com并标明您的昵称以便我给您最优答案。
我希望的效果是:
1,当我运行附件目录下的.cpp文件时可以直接运行。
2,希望里面有需要的所有头文件。
3,最好有解释说明。
小白在此拜谢大虾们~~~
#include"snaker.h"
#include"table.h"
这两个头文件没有啊,希望你能给出来~~

#include<windows.h> //main.cpp
#include <time.h>

#include"snaker.h"
#include"table.h"

//游戏状态定义
#define GAME_STATE_WAIT 0 //游戏等待状态
#define GAME_STATE_RUN 1 //游戏运行状态
#define GAME_STATE_END 2 //游戏结束状态

//界面相关物件尺寸定义
#define WALL_WIDTH 80 //外墙从左部到游戏区的宽度
#define WALL_HEIGHT 80 //外墙从顶部到游戏区的高度

#define BMP_SCORE_BOARD_WIDTH 256 //分数位图板的宽度
#define BMP_SCORE_BOARD_HEIGHT 55 //分数位图板的高度

#define BMP_WALL_WIDTH 16 //墙位图的宽度
#define BMP_WALL_HEIGHT 16 //墙位图的高度

LRESULT CALLBACK WndProc(HWND hWnd,UINT message,
WPARAM wParam,LPARAM lParam);
void DrawGame(void);
void ShellDraw( HDC hdc );
void GameAreaDraw(HDC hdc);

void OnTimer(UINT uTIMER_ID);
void StartGame( void );
void EndGame( void );

//创建一个桌子
CTable table;

int tableBlockWidth = 0; //桌子的格子的宽度
int tableBlockHeight = 0; //桌子的格子的高度
int iScores = 0; //游戏的得分

UINT uGameState = GAME_STATE_WAIT; //当前游戏状态

HDC windowDC = NULL; //windows屏幕设备
HDC bufferDC = NULL; //缓冲设备环境
HDC picDC = NULL; //snake图像内存设备
HDC endDC = NULL; //游戏终结图像内存设备
HDC scoreDC = NULL; //分数板内存设备

HWND hAppWnd = NULL; //本application窗口句柄

HBITMAP picBMP = NULL; //snake图像位图句柄
HBITMAP bufferBMP = NULL; //缓冲位图句柄
HBITMAP endBMP = NULL; //游戏终结图像内存句柄
HBITMAP hbmpWall = NULL; //墙位图句柄
HBITMAP hbmpScore = NULL; //分数板位图句柄

HBRUSH hbrushWall = NULL; //墙画刷

//定时器标识
UINT uSnakeMoveTimer; //蛇的移动
UINT uFoodAddTimer; //水果的产生

//框架的位置数据定义
//GDI RECT 而不是 MFC CRect
RECT g_ClientRect;
RECT g_GameValueRect;
int g_iClientWidth;
int g_iClientHeight;

int WINAPI WinMain(HINSTANCE hCurrentInst,HINSTANCE hPrevInstance,
PSTR lpszCmdLine,int nCmdShow)

WNDCLASS wndClass;
HWND hWnd;
MSG msg;
UINT width,height;

//定义窗口
wndClass.style=CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc=WndProc;
wndClass.cbClsExtra=0;
wndClass.cbWndExtra=0;
wndClass.hInstance=hCurrentInst;
wndClass.hIcon=LoadIcon(NULL,MAKEINTRESOURCE(IDI_MAIN));
wndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName=NULL;
wndClass.lpszClassName="Snake_CLASS";
//注册窗口
RegisterClass(&wndClass);

//取整个屏幕的尺寸
width=GetSystemMetrics(SM_CXSCREEN);
height= GetSystemMetrics(SM_CYSCREEN);
//创建窗口
hWnd=CreateWindow(
"Snake_CLASS",
"skyblue snake",
WS_POPUP,
0,0,
width,height,
NULL,NULL,
hCurrentInst,
NULL);

hAppWnd = hWnd;

//显示窗口
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);

//获取窗体大小
GetClientRect(hAppWnd, &g_ClientRect);
g_iClientWidth = g_ClientRect.right-g_ClientRect.left;
g_iClientHeight = g_ClientRect.bottom-g_ClientRect.top;

//将游戏区域分成纵,横均为20块的小方块
//并计算每块区域的大小
tableBlockWidth = (g_iClientWidth-2*WALL_WIDTH)/20;
tableBlockHeight = (g_iClientHeight-2*WALL_HEIGHT)/20;

//获取当前主窗口设备与windowDC关联
windowDC=GetDC(NULL);
//创建与windowDC兼容的内存设备环境
bufferDC=CreateCompatibleDC(windowDC);
picDC=CreateCompatibleDC(windowDC);
endDC=CreateCompatibleDC(windowDC);
scoreDC=CreateCompatibleDC(windowDC);

//位图的初始化或载入位图
bufferBMP=CreateCompatibleBitmap(windowDC,g_iClientWidth,g_iClientHeight);
picBMP=(HBITMAP)LoadImage(NULL,"snake.bmp",IMAGE_BITMAP,160,80,LR_LOADFROMFILE);
hbmpWall=(HBITMAP)LoadImage(NULL,"brick.bmp",IMAGE_BITMAP,16,16,LR_LOADFROMFILE);
endBMP = (HBITMAP)LoadImage(NULL,"end.bmp",IMAGE_BITMAP,369,300,LR_LOADFROMFILE);
hbmpScore=(HBITMAP)LoadImage(NULL,"scoreboard.bmp",IMAGE_BITMAP,265,55,LR_LOADFROMFILE);

//声明位图与设备环境的关联
SelectObject(bufferDC,bufferBMP);
SelectObject(picDC,picBMP);
SelectObject(endDC,endBMP);
SelectObject(scoreDC,hbmpScore);

//建立画刷与其名相对应的图像的关联,
//以备用刷子将墙刷出来,用PatBlt()实现
hbrushWall = CreatePatternBrush(hbmpWall);

StartGame();

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

TranslateMessage(&msg);
DispatchMessage(&msg);

return msg.wParam;


LRESULT CALLBACK WndProc(HWND hWnd,UINT message,
WPARAM wParam,LPARAM lParam)


switch(message)

case WM_TIMER :
OnTimer((UINT)wParam);
break;

case WM_KEYDOWN:
switch(wParam)

case VK_ESCAPE:
exit(0);
break;
case VK_UP:
table.ChangeSnakeDirect(S_UP);
break;
case VK_DOWN:
table.ChangeSnakeDirect(S_DOWN);
break;
case VK_LEFT:
table.ChangeSnakeDirect(S_LEFT);
break;
case VK_RIGHT:
table.ChangeSnakeDirect(S_RIGHT);
break;
case VK_SPACE:
if( uGameState == GAME_STATE_END )

StartGame();
break;



return 0;
case WM_SETCURSOR:
SetCursor(NULL);
return 0;
case WM_DESTROY:
ReleaseDC(hWnd,picDC);
ReleaseDC(hWnd,bufferDC);
ReleaseDC(hWnd,windowDC);
PostQuitMessage(0);
return 0;

return DefWindowProc(hWnd,message,wParam,lParam);


void DrawGame(void)

//绘制外壳区域到缓冲
ShellDraw(bufferDC);

//绘制游戏区域到缓冲
GameAreaDraw(bufferDC);

//将整个画面从缓冲DC拷贝出屏幕
BitBlt(windowDC,0,0,g_iClientWidth,g_iClientHeight,bufferDC,0,0,SRCCOPY);


void OnTimer(UINT uTIMER_ID)

if ( uTIMER_ID == uSnakeMoveTimer )

//移动蛇
table.SnakeMove();

//检测是否碰到身体(环绕),结束游戏
if(table.GetSnake()->IsHeadTouchBody(table.GetSnake()->GetPos()[0].x,table.GetSnake()->GetPos()[0].y))

EndGame();


//根据蛇头所在的区域作出相应的处理
switch(table.GetData(table.GetSnake()->GetPos()[0].x,table.GetSnake()->GetPos()[0].y))

case TB_STATE_FOOD:
table.ClearFood(table.GetSnake()->GetPos()[0].x,table.GetSnake()->GetPos()[0].y);
table.AddBlock((rand())%tableBlockWidth,(rand())%tableBlockHeight);
table.GetSnake()->AddBody();
++iScores;
break;
case TB_STATE_BLOCK:
case TB_STATE_SBLOCK:
//检测是否碰到障碍物,结束游戏
EndGame();
break;


//显示
DrawGame();

else if ( uTIMER_ID == uFoodAddTimer )
//定时加食物
table.AddFood((rand())%tableBlockWidth,(rand())%tableBlockHeight);



void StartGame()

iScores=0;

//桌面的初始化
table.InitialTable(tableBlockWidth,tableBlockHeight);
table.GetSnake()->ChangeDirect(S_RIGHT);
table.GetSnake()->SetHeadPos(tableBlockWidth/2,tableBlockHeight/2);

//预先随机产生几个食物
srand( (unsigned)time(NULL) );
for(int iFoodNum=0; iFoodNum<4; iFoodNum++)

table.AddFood((rand())%tableBlockWidth,(rand())%tableBlockHeight);

//不部下种子也可以直接用rand(),
//但是每次游戏开始都是产生完全一样的伪随即序列

//打开定时器
uSnakeMoveTimer = SetTimer(hAppWnd,500,100,NULL);
uFoodAddTimer = SetTimer(hAppWnd,600,7000,NULL);

uGameState = GAME_STATE_RUN;


void EndGame( void )

//关闭定时器
KillTimer(hAppWnd,uSnakeMoveTimer);
KillTimer(hAppWnd,uFoodAddTimer);
uGameState = GAME_STATE_END;


void ShellDraw( HDC hdc )

// HDC hMemDc;
// BOOL ret = TRUE;
// HDC hMemoryDC;
// HBITMAP hbmp;
//分数提示显示文字
char szText[30] = "Score: ";
char szNum[20];

int iNowScore = iScores*100;
itoa(iNowScore,szNum,10);
strcat(szText, szNum);

RECT rt, rect;
GetClientRect(hAppWnd, &rt);
//墙的绘制
SelectObject(hdc,hbrushWall);
PatBlt(hdc,rt.left,rt.top,rt.right,rt.bottom,PATCOPY);
//内部游戏区的白色底平面
rect.left = rt.left+WALL_WIDTH;
rect.top = rt.top + WALL_HEIGHT;
rect.right = rt.right - WALL_WIDTH;
rect.bottom = rt.bottom - WALL_HEIGHT;
FillRect(hdc, &rect, (HBRUSH) (COLOR_WINDOW+1));

//分数提示板的绘制
/* hMemoryDC = CreateCompatibleDC(NULL);
hbmp = CreateCompatibleBitmap(hdc,265,55);
SelectObject(hMemoryDC,hbrushScore);
SelectObject(hMemoryDC,hbmp);
PatBlt(hMemoryDC,0,0,256,55,PATCOPY);
BitBlt(hdc,GetSystemMetrics(SM_CXSCREEN)/3,
10,256,55,hMemoryDC,0,0,SRCCOPY);
*/
BitBlt(hdc,GetSystemMetrics(SM_CXSCREEN)/3,
10,256,55,scoreDC,0,0,SRCCOPY);

//分数的打印绘制
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc, GetSystemMetrics(SM_CXSCREEN)/3+50, 30,szText,strlen(szText) );


void GameAreaDraw(HDC hdc)

int i,j;
int x,y, x_pos,y_pos;
BitmapState state;
char strMark[20];

//绘制水果与 毒果
for(i=0;i<tableBlockHeight;i++)
for(j=0;j<tableBlockWidth;j++)

x_pos = j*20+WALL_WIDTH;
y_pos = i*20+WALL_HEIGHT;

switch(table.GetData(j,i))

case TB_STATE_FOOD:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,100,0,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,20,0,SRCAND);
break;
case TB_STATE_BLOCK:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,80,0,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,0,0,SRCAND);
break;



//根据当前的状态绘制蛇头
x=table.GetSnake()->GetPos()[0].x;
y=table.GetSnake()->GetPos()[0].y;
x_pos = x*20+WALL_WIDTH;
y_pos = y*20+WALL_HEIGHT;
state=table.GetSnake()->GetStateArray()[0];

switch(state)

case M_UP_UP:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,80,20,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,0,20,SRCAND);
break;
case M_DOWN_DOWN:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,140,20,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,60,20,SRCAND);
break;
case M_LEFT_LEFT:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,100,20,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,20,20,SRCAND);
break;
case M_RIGHT_RIGHT:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,120,20,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,40,20,SRCAND);
break;


//根据各个蛇身节点的状态绘制蛇身的形状
for(i=1;i<table.GetSnake()->GetLength()-1;i++)

x=table.GetSnake()->GetPos()[i].x;
y=table.GetSnake()->GetPos()[i].y;
x_pos = x*20+WALL_WIDTH;
y_pos = y*20+WALL_HEIGHT;
state=table.GetSnake()->GetStateArray()[i];
switch(state)

case M_UP_UP:
case M_DOWN_DOWN:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,80,40,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,0,40,SRCAND);
break;
case M_LEFT_LEFT:
case M_RIGHT_RIGHT:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,100,40,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,20,40,SRCAND);
break;
case M_RIGHT_DOWN:
case M_UP_LEFT:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,100,60,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,20,60,SRCAND);
break;
case M_LEFT_DOWN:
case M_UP_RIGHT:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,80,60,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,0,60,SRCAND);
break;
case M_RIGHT_UP:
case M_DOWN_LEFT:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,140,40,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,60,40,SRCAND);
break;
case M_LEFT_UP:
case M_DOWN_RIGHT:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,120,40,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,40,40,SRCAND);
break;




//绘制蛇尾巴
x=table.GetSnake()->GetPos()[table.GetSnake()->GetLength()-1].x;
y=table.GetSnake()->GetPos()[table.GetSnake()->GetLength()-1].y;
x_pos = x*20+WALL_WIDTH;
y_pos = y*20+WALL_HEIGHT;
state=table.GetSnake()->GetStateArray()[table.GetSnake()->GetLength()-1];
switch(state)

case M_UP_UP:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,120,60,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,40,60,SRCAND);
break;
case M_DOWN_DOWN:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,120,0,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,40,0,SRCAND);
break;
case M_LEFT_LEFT:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,140,60,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,60,60,SRCAND);
break;
case M_RIGHT_RIGHT:
BitBlt(hdc,x_pos,y_pos,20,20,picDC,140,0,SRCPAINT);
BitBlt(hdc,x_pos,y_pos,20,20,picDC,60,0,SRCAND);
break;


if(uGameState == GAME_STATE_END)
//绘制游戏结束图像
int x_pos = g_iClientWidth/3;
int y_pos = g_iClientHeight/4;

BitBlt(hdc, x_pos, y_pos, 369, 300, endDC, 0, 0, SRCCOPY);



#include "snake.h"

CSnake::CSnake(int x_pos,int y_pos,int len)

if(len<1) len=1;
int i;

m_length=len; //蛇的身体体长

//初始化蛇的坐标位置
m_pPos=new SPoint[m_length+2];
m_pPos[0].x=x_pos;m_pPos[0].y=y_pos;
for(i=1;i<m_length+2;i++)

m_pPos[i].x=0;m_pPos[i].y=0;


//初始化蛇的运动状态
m_newSnake.head=S_NONE;
m_oldSnake.head=S_NONE;
m_newSnake.body=new MoveState[m_length];
m_oldSnake.body=new MoveState[m_length];
for(i=0;i<m_length;i++)

m_newSnake.body[i]=S_NONE;
m_newSnake.body[i]=S_NONE;

m_newSnake.tail=S_NONE;
m_oldSnake.tail=S_NONE;

//初始化蛇的位图显示状态
m_pStateArray=new BitmapState[m_length+2];
for(i=0;i<m_length+2;i++)
m_pStateArray[i]=M_NONE;


CSnake::~CSnake()

SAFE_DELETE_ARRAY(m_pStateArray);
SAFE_DELETE_ARRAY(m_pPos);


//
//根据新旧两个身体的运动趋势情况,返回当前应当显示的身体状态
//
BitmapState CSnake::GetRightState(MoveState oldDirect,MoveState newDirect)

BitmapState res;
switch(oldDirect)

case S_NONE:
switch(newDirect)

case S_NONE:
res=M_NONE;
break;
case S_UP:
res=M_UP_UP;
break;
case S_DOWN:
res=M_DOWN_DOWN;
break;
case S_LEFT:
res=M_LEFT_LEFT;
break;
case S_RIGHT:
res=M_RIGHT_RIGHT;
break;

break;
case S_UP:
switch(newDirect)

case S_UP:
res=M_UP_UP;
break;
case S_LEFT:
res=M_UP_LEFT;
break;
case S_RIGHT:
res=M_UP_RIGHT;
break;

break;
case S_DOWN:
switch(newDirect)

case S_DOWN:
res=M_DOWN_DOWN;
break;
case S_LEFT:
res=M_DOWN_LEFT;
break;
case S_RIGHT:
res=M_DOWN_RIGHT;
break;

break;
case S_LEFT:
switch(newDirect)

case S_LEFT:
res=M_LEFT_LEFT;
break;
case S_UP:
res=M_LEFT_UP;
break;
case S_DOWN:
res=M_LEFT_DOWN;
break;

break;
case S_RIGHT:
switch(newDirect)

case S_RIGHT:
res=M_RIGHT_RIGHT;
break;
case S_UP:
res=M_RIGHT_UP;
break;
case S_DOWN:
res=M_RIGHT_DOWN;
break;

break;

return res;


//
//改变方向
//
void CSnake::ChangeDirect(MoveState d)

// 改变方向的条件:非对立方向
// 只能为其左,前,右方
switch(d)

case S_NONE:
m_newSnake.head=d;
break;
case S_UP:
if(m_newSnake.head!=S_DOWN) m_newSnake.head=d;
break;
case S_DOWN:
if(m_newSnake.head!=S_UP) m_newSnake.head=d;
break;
case S_LEFT:
if(m_newSnake.head!=S_RIGHT) m_newSnake.head=d;
break;
case S_RIGHT:
if(m_newSnake.head!=S_LEFT) m_newSnake.head=d;
break;



//
//蛇移动
//
void CSnake::Move(void)

int i;
//1.计算新状态各个节点的状态
//保存蛇身体各个部位的形状
for(i=0;i<m_length;i++)

m_oldSnake.body[i]=m_newSnake.body[i];


//将蛇身体的状态根据前面的状态变动一次
m_newSnake.tail=m_newSnake.body[m_length-1];
for(i=m_length-1;i>0;i--)

m_newSnake.body[i]=m_newSnake.body[i-1];

m_newSnake.body[0]=m_newSnake.head;

//根据新旧状态特性取正确的状态
m_pStateArray[0]=GetRightState(m_oldSnake.head,m_newSnake.head);
for(i=0;i<m_length;i++)
m_pStateArray[i+1]=GetRightState(m_oldSnake.body[i],m_newSnake.body[i]);
m_pStateArray[m_length+1]=GetRightState(m_oldSnake.tail,m_newSnake.tail);

//2.将整个蛇的坐标移动
//除蛇头外,其他部分的新位置为其前一部分的原来位置
for(i=m_length+1;i>0;i--)
m_pPos[i]=m_pPos[i-1];
//蛇头的新位置根据蛇的运动方向判断做相应偏移
switch(m_newSnake.head)

case S_UP:
m_pPos[0].y-=SNAKE_MOVE;
break;
case S_DOWN:
m_pPos[0].y+=SNAKE_MOVE;
break;
case S_LEFT:
m_pPos[0].x-=SNAKE_MOVE;
break;
case S_RIGHT:
m_pPos[0].x+=SNAKE_MOVE;
break;




//
//蛇的身体增长
//
void CSnake::AddBody(int n)


// 分配临时的"save类型"变量,用作保留
// 蛇的各种数据状态
int i;
Snake_Struct saveOldSnake,saveNewSnake;
BitmapState *savestateArray;
SPoint *savePos;

//保存蛇的位置信息
// pos
savePos=new SPoint[m_length+2];
for(i=0;i<m_length+2;i++)
savePos[i]=m_pPos[i];

//保存蛇的状态信息
// 1.oldSnake
// 2.newSnake
// 3.stateArray

//1
saveOldSnake.head=m_oldSnake.head;
saveOldSnake.body=new MoveState[m_length];
for(i=0;i<m_length;i++)
saveOldSnake.body[i]=m_oldSnake.body[i];
saveOldSnake.tail=m_oldSnake.tail;
//2
saveNewSnake.head=m_newSnake.head;
saveNewSnake.body=new MoveState[m_length];
for(i=0;i<m_length;i++)
saveNewSnake.body[i]=m_newSnake.body[i];
saveNewSnake.tail=m_newSnake.tail;
//3
savestateArray=new BitmapState[m_length+2];
for(i=0;i<m_length+2;i++)
savestateArray[i]=m_pStateArray[i];

//将长度增长
m_length+=n;

//释放所有蛇的身体存储数据空间
delete[] m_oldSnake.body;m_oldSnake.body=NULL;
delete[] m_newSnake.body;m_newSnake.body=NULL;
delete[] m_pStateArray;m_pStateArray=NULL;
delete[] m_pPos;m_pPos=NULL;

//创建并初始化增长后的蛇的存储数据空间

m_newSnake.head=S_NONE;
m_oldSnake.head=S_NONE;
m_newSnake.body=new MoveState[m_length];
m_oldSnake.body=new MoveState[m_length];
for(i=0;i<m_length;i++)

m_newSnake.body[i]=S_NONE;
m_newSnake.body[i]=S_NONE;

m_newSnake.tail=S_NONE;
m_oldSnake.tail=S_NONE;

m_pStateArray=new BitmapState[m_length+2];
for(i=0;i<m_length+2;i++)
m_pStateArray[i]=M_NONE;
m_pPos=new SPoint[m_length+2];
for(i=0;i<m_length+2;i++)

m_pPos[i].x=0;
m_pPos[i].y=0;


//恢复原来长度的数据(新的用初始化的数据)
//a. newSnake ,oldSnake状态
//b. stateArray
//c. pos

//a
m_newSnake.head=saveNewSnake.head;
m_oldSnake.head=saveOldSnake.head;
for(i=0;i<m_length-n;i++)

m_newSnake.body[i]=saveNewSnake.body[i];
m_oldSnake.body[i]=saveOldSnake.body[i];

m_newSnake.tail=saveNewSnake.tail;
m_oldSnake.tail=saveOldSnake.tail;

//b
for(i=0;i<m_length-n+2;i++)
m_pStateArray[i]=savestateArray[i];

//c
for(i=0;i<m_length-n+2;i++)
m_pPos[i]=savePos[i];


//
//设置蛇头的坐标
//
void CSnake::SetHeadPos(int x,int y)

m_pPos[0].x=x;m_pPos[0].y=y;


//
//取蛇的状态标识数组
//
BitmapState* CSnake::GetStateArray(void)

return m_pStateArray;


//
//取蛇的位置数组
//
SPoint* CSnake::GetPos(void)

return m_pPos;


//
//取蛇身的长度
//
int CSnake::GetLength(void)

return m_length+2;


//
//检测蛇头是否触碰到其身体
//
bool CSnake::IsHeadTouchBody(int x,int y)

int i;
for(i=1;i<m_length+2;i++)
if(m_pPos[i].x==x&&m_pPos[i].y==y) return true;
return false;


//
//初始化 用作游戏结束后重新开始
//
void CSnake::Initial(void )

//释放以前的所有存储空间
SAFE_DELETE_ARRAY(m_pStateArray);
SAFE_DELETE_ARRAY(m_pPos);

//创建蛇身长度为1的蛇,并做各种初始化
int i;
int x = 0;
int y = 0;

//初始化蛇的坐标位置
m_length=1;
m_pPos=new SPoint[m_length+2];
m_pPos[0].x=x;m_pPos[0].y=y;
for(i=1;i<m_length+2;i++)

m_pPos[i].x=0;m_pPos[i].y=0;


//初始化蛇的运动状态
m_newSnake.head=S_NONE;
m_oldSnake.head=S_NONE;
m_newSnake.body=new MoveState[m_length];
m_oldSnake.body=new MoveState[m_length];
for(i=0;i<m_length;i++)

m_newSnake.body[i]=S_NONE;
m_newSnake.body[i]=S_NONE;

m_newSnake.tail=S_NONE;
m_oldSnake.tail=S_NONE;

//初始化蛇的位图显示状态
m_pStateArray=new BitmapState[m_length+2];
for(i=0;i<m_length+2;i++)
m_pStateArray[i]=M_NONE;
参考技术A 我有完整工程何给
ps:随便搜搜多网站上都有

贪食蛇

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>贪吃蛇</title>
 <style>
  body {
   display: flex;
   height: 500px;
   margin: 0;
   padding: 0;
   justify-content: center;
   align-items: center;
  }
 </style>
</head>
<body>
 <canvas id="can" width="400" height="400" style="background-color: black">对不起,您的浏览器不支持canvas</canvas>
 <script>
  
  var snake = [41, 40],  //snake队列表示蛇身,初始节点存在但不显示
   direction = 1,   //1表示向右,-1表示向左,20表示向下,-20表示向上
   food = 43,    //食物的位置
   n,      //与下次移动的位置有关
   box = document.getElementById(‘can‘).getContext(‘2d‘);
         //从0到399表示box里[0~19]*[0~19]的所有节点,每20px一个节点
  function draw(seat, color) {
   box.fillStyle = color;
   box.fillRect(seat % 20 *20 + 1, ~~(seat / 20) * 20 + 1, 18, 18);
         //用color填充一个矩形,以前两个参数为x,y坐标,后两个参数为宽和高。
  }
  document.onkeydown = function(evt) { 
         //当键盘上下左右键摁下的时候改变direction
   direction = snake[1] - snake[0] == (n = [-1, -20, 1, 20][(evt || event).keyCode - 37] || direction) ? direction : n;
  };
  !function() {
   snake.unshift(n = snake[0] + direction); 
         //此时的n为下次蛇头出现的位置,n进入队列
   if(snake.indexOf(n, 1) > 0 || n < 0 || n > 399 || direction == 1 && n % 20 == 0 || direction == -1 && n % 20 == 19) {
         //if语句判断贪吃蛇是否撞到自己或者墙壁,碰到时返回,结束程序
    return alert("GAME OVER!");
   }
   draw(n, "lime");  //画出蛇头下次出现的位置
   if(n == food) {   //如果吃到食物时,产生一个蛇身以外的随机的点,不会去掉蛇尾
    while (snake.indexOf(food = ~~(Math.random() * 400)) > 0);
    draw(food, "yellow");
   } else {    //没有吃到食物时正常移动,蛇尾出队列
    draw(snake.pop(),"black");
   }
   setTimeout(arguments.callee, 300);  
         //每隔0.15秒执行函数一次,可以调节蛇的速度
  }();
 </script>
</body>
</html>

 

以上是关于求vc++6.0贪食蛇源代码的主要内容,如果未能解决你的问题,请参考以下文章

Python 复杂版贪食蛇(源代码)

如何创建MFC文件,编写贪食蛇小游戏

关于贪食蛇的源代码

canvas游戏之贪食蛇

Javascript贪食蛇小游戏

js贪食蛇