在线求助 在线求助啊 我有个vc2008的贪吃蛇 总是有点小问题 求大虾帮忙看看 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //CTanSnakeView.h#pragma once#define Right 0#define Down 1#define Left 2#define Up 3class CTanSnakeView : public CView{public: //贪吃蛇的困难度 int m_iDifficulty; //贪吃蛇的状态 BOOL m_iGameState; //贪吃蛇的方向 int m_iAspect; //贪吃蛇的长度 int m_iSnakeLength; //食物状态 BOOL m_iFootState; //定义蛇的长度操作数组 CPoint a[100]; CPoint b; //定义一个标记点标记食物的位置 CPoint foot; //定义游戏的面积 CPoint m_GameAreaStart; CPoint m_GameAreaEnd; //得分 CString m_strGameSocre; // CRect rct;private: //判断游戏结束的函数 void GameEnd(); //产生食物的函数 void FootCreate(); //初始化蛇的状态 void InitSnakeState(); //蛇的移动 void SnakeMove(); //判断是否吃到食物 void SnakeEat(); //显示其得分 void GameScore(); //创建蛇 void SnakeCreate();protected: DECLARE_MESSAGE_MAP()public: afx_msg void OnStart(); afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); afx_msg void OnTimer(UINT_PTR nIDEvent); afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);};#ifndef _DEBUG // TanSnakeView.cpp 中的调试版本inline CTanSnakeDoc* CTanSnakeView::GetDocument() const { return reinterpret_cast<CTanSnakeDoc*>(m_pDocument); }#endifCTanSnakeView.cppvoid CTanSnakeView::OnTimer(UINT_PTR nIDEvent){ // TODO: 在此添加消息处理程序代码和/或调用默认 KillTimer(1); CClientDC MemDC(this); /*CDC* pDC=GetDC();*/ MemDC.Rectangle(CRect(10,10,440,440)); MemDC.Rectangle(CRect(20,20,430,430)); FootCreate(); /*SnakeCreate(); SnakeMove(); SnakeEat(); GameEnd();*/ /*int m_Timer=SetTimer(1,m_iDifficulty*30,NULL);*/ CView::OnTimer(nIDEvent);}int CTanSnakeView::OnCreate(LPCREATESTRUCT lpCreateStruct){ if (CView::OnCreate(lpCreateStruct) == -1) return -1; // TODO: 在此添加您专用的创建代码 /*InitSnakeState();*/ SetTimer(1,10,NULL); srand(unsigned(time(0))); return 0;}void CTanSnakeView::InitSnakeState(){ /* 0向右 1向下 2向左 3向上 */ m_iAspect=Right;//初始化方向向右移动 m_iFootState=1;//产生食物 m_iDifficulty=2;//困难程度 m_iGameState=1;//游戏状态 m_iSnakeLength=3;//初始化蛇的长度 //初始化游戏边框的面积 m_GameAreaStart.x=20; m_GameAreaStart.y=20; m_GameAreaEnd.x=420; m_GameAreaEnd.y=420; a[0].x=90; a[0].y=80; a[1].x=80; a[1].y=80; a[2].x=70; a[2].y=80;}void CTanSnakeView::FootCreate(){ if (m_iFootState==1&&m_iGameState==1) { foot.x=m_GameAreaStart.x+int((m_GameAreaEnd.x-m_GameAreaStart.x)*rand()/(RAND_MAX+1.0)); foot.y=m_GameAreaStart.y+int((m_GameAreaEnd.y-m_GameAreaStart.y)*rand()/(RAND_MAX+1.0)); for (int i=0;i<m_iSnakeLength;i++) { while(foot.x==a[i].x&&foot.y==a[i].y) { foot.x=m_GameAreaStart.x+int ((m_GameAreaEnd.x-m_GameAreaStart.x)*rand()/(RAND_MAX+1.0)); foot.y=m_GameAreaStart.y+int ((m_GameAreaEnd.y-m_GameAreaStart.y)*rand()/(RAND_MAX+1.0)); } } m_iFootState=0; } //食物的位置和样式 CClientDC MemDC(this); CBrush FootStyle(RGB(0,255,0)); MemDC.SelectObject(&FootStyle); MemDC.Rectangle(foot.x,foot.y,foot.x+10,foot.y+10); MemDC.SelectStockObject(BLACK_BRUSH); FootStyle.DeleteObject(); }void CTanSnakeView::SnakeCreate(){ if(m_iGameState==1) { //创建蛇的头部 CClientDC MemDC(this); CBrush head(RGB(255,0,0)); MemDC.SelectObject(&head); rct=new CRect(a[0].x,a[0].y,a[0].x+10,a[0].y+10); MemDC.Rectangle(rct); //创建贪吃蛇的身体 for(int i=1;i<m_iSnakeLength;i++) { CBrush body(RGB(255,255,0)); MemDC.SelectObject(&body); rct=new CRect(a[i].x,a[i].y,a[i].x+10,a[i].y+10); MemDC.Rectangle(rct); //把设备重置回去 MemDC.SelectStockObject(BLACK_BRUSH); } }}void CTanSnakeView::SnakeEat(){ if (m_iGameState==1) { if (a[0].x==foot.x&&a[0].y==foot.y) { m_iFootState=1; m_iSnakeLength++; /************************************************************************/ /*判断 */ /************************************************************************/ a[m_iSnakeLength-1].x=a[m_iSnakeLength-2].x; a[m_iSnakeLength-1].y=a[m_iSnakeLength-2].y; } } }void CTanSnakeView::GameScore(){ m_strGameSocre.Format("分数:%d",(m_iSnakeLength-3)*10); CClientDC dc(this); dc.TextOut(m_GameAreaEnd.x+10,m_GameAreaEnd.y/2,m_strGameSocre);}void CTanSnakeView::SnakeMove(){ if (m_iGameState==1) { b.x=a[0].x; b.y=a[0].y; if (m_iAspect==Right) { //向右移动 移动单位为10 a[0].x=a[0].x+10; } else if (m_iAspect==Down) { //向下移动 移动单位为10 a[0].y=a[0].y+10; }else if (m_iAspect==Left) { //向左移动 移动单位为10 a[0].x=a[0].x-10; }else if (m_iAspect==Up) { ////向上移动 移动单位为10 a[0].y=a[0].y-10; } //贪吃蛇的移动 for (int i=1;i<m_iSnakeLength-1;i++) { a[m_iSnakeLength-i].x=a[m_iSnakeLength-i-1].x; a[m_iSnakeLength-i].y=a[m_iSnakeLength-i-1].y; } a[1].x=b.x; a[1].y=b.y; }}void CTanSnakeView::GameEnd(){ //判断游戏是否结束 for (int i=0;i<m_iSnakeLength;i++) { if (a[0].x==a[i].x&&a[0].y==a[i].y) { m_iGameState=0; } else if ((a[0].x<20&&a[0].x>420)||(a[0].y<20&&a[0].y>420)) { m_iGameState=0; } }}void CTanSnakeView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags){ // TODO: 在此添加消息处理程序代码和/或调用默认值 if (m_iGameState==1) { if (m_iAspect !=0&& nChar==37)//判断向左移动 { m_iAspect=Left; } else if (m_iAspect!=1&&nChar==38)//判断向上移 { m_iAspect=Up; } else if (m_iAspect!=2&&nChar==39)//判断向右移动 { m_iAspect=Right; } else if (m_iAspect!=3&&nChar==40)//判断向下移动 { m_iAspect=Down; } } CView::OnKeyDown(nChar, nRepCnt, nFlags);}void CTanSnakeView::OnStart(){ // TODO: 在此添加命令处理程序代码 InitSnakeState(); SetTimer(1,10,NULL); srand(unsigned(time(0))); } 问一个386的"线性地址的问题" 非模态窗口显示的问题 串口数据用界面显示,用listbox会死掉? 能否向Windows注册一个外部"事件" 如何编程获知系统是否已经打上某个补丁 以二进制形式收到的图片(在内存中),如何显示为图片呢? 利用Connection对象的Execute方法执行SQL命令 异常处理问题--初学者 ADO问题!!高手敬请关注………(在线等待) afx_msg void OnKeyUp//if(nChar==82)是什么意思??? 心情不好,再散分 mfc CListCtrl 中显示图片列
#pragma once
#define Right 0
#define Down 1
#define Left 2
#define Up 3class CTanSnakeView : public CView
{
public:
//贪吃蛇的困难度
int m_iDifficulty;
//贪吃蛇的状态
BOOL m_iGameState;
//贪吃蛇的方向
int m_iAspect;
//贪吃蛇的长度
int m_iSnakeLength;
//食物状态
BOOL m_iFootState;
//定义蛇的长度操作数组
CPoint a[100];
CPoint b;
//定义一个标记点标记食物的位置
CPoint foot;
//定义游戏的面积
CPoint m_GameAreaStart;
CPoint m_GameAreaEnd;
//得分
CString m_strGameSocre;
//
CRect rct;private:
//判断游戏结束的函数
void GameEnd();
//产生食物的函数
void FootCreate();
//初始化蛇的状态
void InitSnakeState();
//蛇的移动
void SnakeMove();
//判断是否吃到食物
void SnakeEat();
//显示其得分
void GameScore();
//创建蛇
void SnakeCreate();protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnStart();
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
};#ifndef _DEBUG // TanSnakeView.cpp 中的调试版本
inline CTanSnakeDoc* CTanSnakeView::GetDocument() const
{ return reinterpret_cast<CTanSnakeDoc*>(m_pDocument); }
#endifCTanSnakeView.cpp
void CTanSnakeView::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认
KillTimer(1);
CClientDC MemDC(this);
/*CDC* pDC=GetDC();*/
MemDC.Rectangle(CRect(10,10,440,440));
MemDC.Rectangle(CRect(20,20,430,430));
FootCreate();
/*SnakeCreate();
SnakeMove();
SnakeEat();
GameEnd();*/
/*int m_Timer=SetTimer(1,m_iDifficulty*30,NULL);*/ CView::OnTimer(nIDEvent);}
int CTanSnakeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1; // TODO: 在此添加您专用的创建代码
/*InitSnakeState();*/
SetTimer(1,10,NULL);
srand(unsigned(time(0))); return 0;
}
void CTanSnakeView::InitSnakeState()
{
/*
0向右
1向下
2向左
3向上
*/
m_iAspect=Right;//初始化方向向右移动
m_iFootState=1;//产生食物
m_iDifficulty=2;//困难程度
m_iGameState=1;//游戏状态
m_iSnakeLength=3;//初始化蛇的长度
//初始化游戏边框的面积
m_GameAreaStart.x=20;
m_GameAreaStart.y=20; m_GameAreaEnd.x=420;
m_GameAreaEnd.y=420;
a[0].x=90;
a[0].y=80;
a[1].x=80;
a[1].y=80;
a[2].x=70;
a[2].y=80;}void CTanSnakeView::FootCreate()
{
if (m_iFootState==1&&m_iGameState==1)
{
foot.x=m_GameAreaStart.x+int((m_GameAreaEnd.x-m_GameAreaStart.x)*rand()/(RAND_MAX+1.0));
foot.y=m_GameAreaStart.y+int((m_GameAreaEnd.y-m_GameAreaStart.y)*rand()/(RAND_MAX+1.0));
for (int i=0;i<m_iSnakeLength;i++)
{
while(foot.x==a[i].x&&foot.y==a[i].y)
{
foot.x=m_GameAreaStart.x+int ((m_GameAreaEnd.x-m_GameAreaStart.x)*rand()/(RAND_MAX+1.0));
foot.y=m_GameAreaStart.y+int ((m_GameAreaEnd.y-m_GameAreaStart.y)*rand()/(RAND_MAX+1.0)); }
}
m_iFootState=0;
}
//食物的位置和样式
CClientDC MemDC(this);
CBrush FootStyle(RGB(0,255,0));
MemDC.SelectObject(&FootStyle);
MemDC.Rectangle(foot.x,foot.y,foot.x+10,foot.y+10);
MemDC.SelectStockObject(BLACK_BRUSH);
FootStyle.DeleteObject();
}
void CTanSnakeView::SnakeCreate()
{ if(m_iGameState==1)
{
//创建蛇的头部
CClientDC MemDC(this); CBrush head(RGB(255,0,0));
MemDC.SelectObject(&head);
rct=new CRect(a[0].x,a[0].y,a[0].x+10,a[0].y+10);
MemDC.Rectangle(rct);
//创建贪吃蛇的身体
for(int i=1;i<m_iSnakeLength;i++)
{
CBrush body(RGB(255,255,0));
MemDC.SelectObject(&body);
rct=new CRect(a[i].x,a[i].y,a[i].x+10,a[i].y+10);
MemDC.Rectangle(rct);
//把设备重置回去
MemDC.SelectStockObject(BLACK_BRUSH);
}
}
}
void CTanSnakeView::SnakeEat()
{
if (m_iGameState==1)
{
if (a[0].x==foot.x&&a[0].y==foot.y)
{
m_iFootState=1;
m_iSnakeLength++; /************************************************************************/
/*判断 */
/************************************************************************/
a[m_iSnakeLength-1].x=a[m_iSnakeLength-2].x;
a[m_iSnakeLength-1].y=a[m_iSnakeLength-2].y; }
}
}
void CTanSnakeView::GameScore()
{
m_strGameSocre.Format("分数:%d",(m_iSnakeLength-3)*10);
CClientDC dc(this);
dc.TextOut(m_GameAreaEnd.x+10,m_GameAreaEnd.y/2,m_strGameSocre);}void CTanSnakeView::SnakeMove()
{
if (m_iGameState==1)
{
b.x=a[0].x;
b.y=a[0].y;
if (m_iAspect==Right)
{
//向右移动 移动单位为10
a[0].x=a[0].x+10;
} else if (m_iAspect==Down)
{
//向下移动 移动单位为10
a[0].y=a[0].y+10;
}else if (m_iAspect==Left)
{
//向左移动 移动单位为10
a[0].x=a[0].x-10;
}else if (m_iAspect==Up)
{
////向上移动 移动单位为10
a[0].y=a[0].y-10;
}
//贪吃蛇的移动
for (int i=1;i<m_iSnakeLength-1;i++)
{
a[m_iSnakeLength-i].x=a[m_iSnakeLength-i-1].x;
a[m_iSnakeLength-i].y=a[m_iSnakeLength-i-1].y;
}
a[1].x=b.x;
a[1].y=b.y;
}
}
void CTanSnakeView::GameEnd()
{
//判断游戏是否结束
for (int i=0;i<m_iSnakeLength;i++)
{
if (a[0].x==a[i].x&&a[0].y==a[i].y)
{
m_iGameState=0;
}
else if ((a[0].x<20&&a[0].x>420)||(a[0].y<20&&a[0].y>420))
{
m_iGameState=0;
}
}
}
void CTanSnakeView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (m_iGameState==1)
{
if (m_iAspect !=0&& nChar==37)//判断向左移动
{
m_iAspect=Left;
}
else if (m_iAspect!=1&&nChar==38)//判断向上移
{
m_iAspect=Up;
}
else if (m_iAspect!=2&&nChar==39)//判断向右移动
{
m_iAspect=Right;
}
else if (m_iAspect!=3&&nChar==40)//判断向下移动
{
m_iAspect=Down;
}
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CTanSnakeView::OnStart()
{
// TODO: 在此添加命令处理程序代码
InitSnakeState();
SetTimer(1,10,NULL);
srand(unsigned(time(0)));
}