您的位置:首页 > 其它

贪吃蛇(版本1.0还有bug,但是基本能使了)

2016-07-20 15:20 274 查看
</pre><pre code_snippet_id="1773311" snippet_file_name="blog_20160720_2_6912133" name="code" class="cpp"></pre><pre code_snippet_id="1773311" snippet_file_name="blog_20160720_2_6912133" name="code" class="cpp">

.h 窗体内运动范围为红线内的

630*350,一个单元是10*10

// G716Dlg.h : 头文件
//

#pragma once

// CG716Dlg 对话框
class CG716Dlg : public CDialogEx
{
// 构造
public:
CG716Dlg(CWnd* pParent = NULL);	// 标准构造函数

// 对话框数据
enum { IDD = IDD_G716_DIALOG };

protected:
virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV 支持

// 实现
protected:
HICON m_hIcon;

// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedButton1();
afx_msg void OnTimer(UINT_PTR nIDEvent);
int mtime;
int speed;
void cell(int x,int y);
void clearcell(int x,int y);
int positionx;
int positiony;
int number;
BOOL PreTranslateMessage(MSG* pMsg);
int direction;
CPoint food();
CPoint foodposition;
CPoint headposition;
CPoint tailposition;
CPoint snakebody[20];
//	CPoint temppoint;
int length;//snake's length
void snakemove(int direc);
int eaten;
void drawedge();
int failtest();
};


.cpp

// G716Dlg.cpp : 实现文件
//

#include "stdafx.h"
#include "G716.h"
#include "G716Dlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();

// 对话框数据
enum { IDD = IDD_ABOUTBOX };

protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()

// CG716Dlg 对话框

CG716Dlg::CG716Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CG716Dlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CG716Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CG716Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CG716Dlg::OnBnClickedButton1)
ON_WM_TIMER()
END_MESSAGE_MAP()

// CG716Dlg 消息处理程序

BOOL CG716Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();

// 将“关于...”菜单项添加到系统菜单中。

// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
//  执行此操作
SetIcon(m_hIcon, TRUE);			// 设置大图标
SetIcon(m_hIcon, FALSE);		// 设置小图标

// TODO: 在此添加额外的初始化代码
mtime = 0;
length = 0;
return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void CG716Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CG716Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文

SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// 绘制图标
dc.DrawIcon(x, y, m_hIcon);

}
else
{
CDialogEx::OnPaint();
}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CG716Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}

void CG716Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
GetDlgItem(IDC_BUTTON1)->ShowWindow(SW_HIDE);
SetTimer(1,1000,NULL);
speed = 300;
SetTimer(2,speed,NULL);

drawedge();

length = 2;//init
direction = 1;//up
positionx = 30;positiony = 15;

snakebody[0].x = positionx;
snakebody[0].y = positiony;
cell(snakebody[0].x , snakebody[0].y);//the first cell
snakebody[1].x = positionx;
snakebody[1].y = positiony-1;
cell(snakebody[1].x , snakebody[1].y);//the second cell
snakebody[length].x = positionx;
snakebody[length].y = positiony-2;
cell(snakebody[2].x , snakebody[2].y);//the third cell

headposition.x = snakebody[0].x;
headposition.y = snakebody[0].y;
tailposition.x = snakebody[length].x;
tailposition.y = snakebody[length].y;

foodposition = food();
}

void CG716Dlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CString str;
switch(nIDEvent)
{
case 1:
mtime++;
str.Format(_T("%d min,%d s"),mtime/60,mtime%60);
SetDlgItemText(IDC_STATIC,str);
break;
case 2:
drawedge();
cell(foodposition.x,foodposition.y);
snakemove(direction);
//exam whether meet the food
if(headposition.x == foodposition.x && headposition.y == foodposition.y)
{
eaten = 1;
length++;
speed -= 25;
SetTimer(2,speed,NULL);
}
if(failtest())
{
KillTimer(1);
KillTimer(2);
AfxMessageBox(_T("Sorry, you are failed!"));
GetDlgItem(IDC_BUTTON1)->ShowWindow(SW_SHOW);
CString stri;
stri.Format(_T("restart?"));
SetDlgItemText(IDC_BUTTON1,stri);
}

break;
}

CDialogEx::OnTimer(nIDEvent);
}

void CG716Dlg::cell(int x,int y)
{
y = y*10 + 50;
x = x*10;
CRect rect;
GetClientRect(rect);

CClientDC aDC(this);
CPen pen(PS_SOLID,1,RGB(0,0,0));
CPen* pold=aDC.SelectObject(&pen);//控制颜色

CPoint mp;
//画线
mp.x = x;	mp.y = y;	aDC.MoveTo(mp);		mp.x = x+10;	mp.y = y;	aDC.LineTo(mp);//AB
mp.x = x+10;	mp.y = y;	aDC.MoveTo(mp);		mp.x = x+10;	mp.y = y+10;	aDC.LineTo(mp);//BC
mp.x = x+10;	mp.y = y+10;	aDC.MoveTo(mp);		mp.x = x;	mp.y = y+10;	aDC.LineTo(mp);//BD
mp.x = x;	mp.y = y+10;	aDC.MoveTo(mp);		mp.x = x;	mp.y = y;	aDC.LineTo(mp);//AB

aDC.SelectObject(pold);//控制颜色
}

void CG716Dlg::clearcell(int x,int y)
{
y = y*10 + 50;
x = x*10;
CRect rect;
GetClientRect(rect);

CClientDC aDC(this);
CPen pen(PS_SOLID,1,RGB(240,240,240));
CPen* pold=aDC.SelectObject(&pen);//控制颜色

CPoint mp;
//画线
mp.x = x;	mp.y = y;	aDC.MoveTo(mp);		mp.x = x+10;	mp.y = y;	aDC.LineTo(mp);//AB
mp.x = x+10;	mp.y = y;	aDC.MoveTo(mp);		mp.x = x+10;	mp.y = y+10;	aDC.LineTo(mp);//BC
mp.x = x+10;	mp.y = y+10;	aDC.MoveTo(mp);		mp.x = x;	mp.y = y+10;	aDC.LineTo(mp);//BD
mp.x = x;	mp.y = y+10;	aDC.MoveTo(mp);		mp.x = x;	mp.y = y;	aDC.LineTo(mp);//AB

aDC.SelectObject(pold);//控制颜色
}

//按键响应函数
BOOL CG716Dlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN)
{
switch(pMsg->wParam)
{
case VK_DOWN:	direction = 1;break;
case VK_UP:		direction = 0;break;
case VK_LEFT:	direction = 3;break;
case VK_RIGHT:	direction = 2;break;
}
//	return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);

}

CPoint CG716Dlg::food()
{
eaten = 0;
int fx = rand()%63;
int fy = rand()%35;
cell(fx,fy);
CPoint point;
point.x = fx;
point.y = fy;
return point;
}

void CG716Dlg::snakemove(int direc)
{
if(eaten)
{
eaten = 0;
foodposition = food();
}
else
clearcell(tailposition.x,tailposition.y);

for(int i = length ; i>0 ; i--)//update the body
snakebody[i] = snakebody[i-1];//keep the head
tailposition.x = snakebody[length].x;
tailposition.y = snakebody[length].y;

switch (direc)
{
case 0://up
positionx = headposition.x;
positiony = headposition.y - 1;
cell(positionx,positiony);
snakebody[0].x = positionx;
snakebody[0].y = positiony;
headposition.x = snakebody[0].x;
headposition.y = snakebody[0].y;
break;
case 1://back
positionx = headposition.x;
positiony = headposition.y + 1;
cell(positionx,positiony);
snakebody[0].x = positionx;
snakebody[0].y = positiony;
headposition.x = snakebody[0].x;
headposition.y = snakebody[0].y;
break;
case 2://right
positionx = headposition.x + 1;
positiony = headposition.y;
cell(positionx,positiony);
snakebody[0].x = positionx;
snakebody[0].y = positiony;
headposition.x = snakebody[0].x;
headposition.y = snakebody[0].y;
break;
case 3://left
positionx = headposition.x - 1;
positiony = headposition.y;
cell(positionx,positiony);
snakebody[0].x = positionx;
snakebody[0].y = positiony;
headposition.x = snakebody[0].x;
headposition.y = snakebody[0].y;
break;
}
cell(snakebody[length].x,snakebody[length].y);
}

void CG716Dlg::drawedge()
{
CRect rect;
GetClientRect(rect);

CClientDC aDC(this);
CPen pen(PS_SOLID,1,RGB(255,0,0));
CPen* pold=aDC.SelectObject(&pen);//控制颜色

CPoint mp;
//画线
mp.x = 0;	mp.y = 48;	aDC.MoveTo(mp);	mp.x = rect.right;	aDC.LineTo(mp);
mp.x = 0;	mp.y = 49;	aDC.MoveTo(mp);	mp.x = rect.right;	aDC.LineTo(mp);
mp.x = 0;	mp.y = 50;	aDC.MoveTo(mp);	mp.x = rect.right;	aDC.LineTo(mp);

mp.x = 630;	mp.y = 50;	aDC.MoveTo(mp);	mp.y = rect.bottom;	aDC.LineTo(mp);
mp.x = 630+1;	mp.y = 50;	aDC.MoveTo(mp);	mp.y = rect.bottom;	aDC.LineTo(mp);
mp.x = 630+2;	mp.y = 50;	aDC.MoveTo(mp);	mp.y = rect.bottom;	aDC.LineTo(mp);

mp.x = 0;	mp.y = 400;	aDC.MoveTo(mp);	mp.x = rect.right;	aDC.LineTo(mp);
mp.x = 0;	mp.y = 401;	aDC.MoveTo(mp);	mp.x = rect.right;	aDC.LineTo(mp);
mp.x = 0;	mp.y = 402;	aDC.MoveTo(mp);	mp.x = rect.right;	aDC.LineTo(mp);

aDC.SelectObject(pold);//控制颜色
}

int CG716Dlg::failtest()
{
int fail;
if(headposition.x < 0 || headposition.y > 63 || headposition.y < 0 || headposition.y > 35)
fail = 1;
else
fail = 0;
for(int i = 1; i<=length; i++)
{
if(snakebody[0] == snakebody[i])
{
fail = 1;
break;
}
}
return fail;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: