MFC贪吃蛇实验报告

更新时间:2023-05-21 18:06:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

高级语言程序设计实验报告

设计题目 贪吃蛇

目录

1 课程设计的目的和要求 1.1 课程设计目的和任务 1.2 课程设计的要求 2 课程设计任务内容 2.1 本次课程设计的主要任务如下 2.2 游戏规则 2.3 设计流程 3 软件使用说明 3.1 编辑程序的主要软件 3.2 程序使用过程 3.2.1 贪吃蛇进入 3.2.2 贪吃蛇的玩法 1

1

1

1 1

2 2

3 3

3

3 3

3.2.3 贪吃蛇的结束 4

4 课程设计的心得体会 4

4.1 课程设计的心得 4

4.2 课程设计的体会 4

参考文献 附录:部分程序清单 1 课程设计目的和要求

1.1 课程设计的目的和任务

1. 巩固加深对C++语言基本知识的理解和掌握 2. 掌握运用高级语言程序设计的基本技能 3. 利用C++语言进行基本的程序设计 4. 掌握书写程序设计说明文档的能力 5. 提高运用C++语言解决实际问题的能力

1.2 课程设计的要求

1. 分析题目的要求 2. 写出详细的设计说明

3. 编写程序代码,调试程序使其能正确运行 4. 设计完成的软件要便于操作和使用 5. 设计完成后做实验报告号并准备答辩

2 课程设计任务内容

5 5

2.1 本次课程设计的主要任务如下

1) 通过编写“贪吃蛇游戏”程序,掌握结构化、模块化程序设计的思想,培养解决实际问题的能力。

2) 设计好数组元素与蛇、食物的对应关系。 3) 随机产生食物。

4) 有分数统计、同步显示功能。

通过本次课程设计,使我们能更加深入的了解高级语言程序设计的思想。从而进一步提高我们解决实际问题的能力。

2.2 游戏规则

通过一个学期对高级语言程序设计的学习,经过课程设计,完善我们对高

级语言程序设计学习的漏洞和未学会的知识,更加熟练的掌握高级语言程序设计,锻炼我们自主设计程序编写代码的能力。

2.3

3 软件使用说明

3.1 编辑程序主要软件

对于这次课程设计,主要使用的软件visual c++ 6.0,在平日的学习中,

我们也经常用这种软件进行编程,已基本掌握了此软件的用法。

3.2 程序使用过程

3.2.1 贪吃蛇进入

进入界面如下图所示

3.2.2 贪吃蛇的玩法

贪吃蛇的玩法如图wsad分别为上下左右,进行控制,使蛇头触碰到实物。

3.2.3 贪吃蛇的结束

游戏结束界面如上图所示,游戏结束后,会显示往你家的最终得分。

4 课程设计的心得体会

4.1课程设计的心得

由于所学有限,经验不足,代码经常出现错误,最终终于能够正确的运行。但在过程中遇到了不少问题。比如在贪吃蛇行进中急转死亡的bug。食物生成不出来的问题,界面优化的问题,最终通过查阅资料、请教他人修改了参数才得以解决。另外,很多在学习中没有涉及的函数,只能只能上网查起。慢慢了解mfc各方面的知识。

4.2 课程设计的体会

设计过程中,考察的不仅是一个人程序的编辑能力,更多的是考验一

个人的耐心,对高级语言程序设计的感兴趣,才能更加投入的进行下去。眼看c++语言、高级语言程序设计、数据结构等都陆续学习完,自己多少还是掌握了一些知识、规则和技巧。然而所学所知毕竟有限,在编写贪吃蛇游戏中并不感到轻松,但最终还是坚持了下来。由此可见两点:第一,坚持不懈定能取得成果。在后计算机专业课的学习中要坚持不屑的努力。 第二,在计算机编程方面的知识欠缺。应当吸取更多编程的知识勤加锻炼。 我想这次程序所设计不单单是对学习的一种巩固我们可以在今后的工作学习中运用这次坚持不懈的精神,不断加强自己各方面的能力。我们很感激老师提供我们这样一个机会,让我们接触学习到专业知识以外的东西,这对我们来说,不仅仅是怎样处理问题,更多的是了解编程的不易,培养我们坚定的毅力,让我们有信心和勇气去面对将来更大的挑战。

人员分工

程 序 设 计:刘磊、张国栋

实验报告制作:刘磊

演示ppt制作:张国栋

参考文献

【1】 《C++程序设计》。谭浩强 清华大学出版社。

【2】 《数据结构》(第2版)。王红梅 胡明 王涛 清华大学出版社。 【3】 《高级语言程序设计》。迟成文 经济科学出版社。

附录:部分程序清单

// snakeDlg.cpp : implementation file

// Download by

#include "stdafx.h"

4 #include "snake.h" #include "snakeDlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW #undef THIS_FILE

static char THIS_FILE[] = __FILE__; #endif

///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About

typedef struct SnakeNode { int x; int y; }SnakeNode;

typedef struct Snake {

SnakeNode data[100]; int curr; }Snake;

typedef struct food { int x; int y; }SnakeFood;

Snake snake; SnakeFood food; int x, y; int direct;

int nGameState; int score; int speed;

int d; // 该变量用于控制蛇身不能后退

void InitSnake(Snake *snake); void MoveSnake(Snake *snake); void AddSnake(Snake *snake);

void DrawSnake(CDC *pDC , Snake snake); void UnDrawSnake(Snake snake);

void DrawFood(CDC *pDC, SnakeFood f); int GameOver();

void InitSnake(Snake *snake) { int t; direct=0; nGameState=0; score=0; speed=200; d=0; snake->curr=2; x=4; y=4; snake->data[0].x=x; snake->data[0].y=y;

food.x=rand()%20; food.y=rand()%30; switch(direct) {

case 0: x+=1;break; //右 case 1: y+=1;break; case 2: x-=1;break; case 3: y-=1;break; } snake->data[1].x=x; snake->data[1].y=y; t=snake->curr-1; while(t) { if(snake->data[t].x==food.x&&snake->data[t].y==food.y) { food.x=rand()%20;t=snake->curr-1; food.y=rand()%31;t=snake->curr-1; t=snake->curr-1; } t--; } }

void DrawFood(CDC *pDC, SnakeFood f) { CString ss; pDC->SetBkColor(RGB(210,215,210)); ss.Format("得分:%d",score); pDC->TextOut(220,30,ss); pDC->Rectangle(f.x*10+11,f.y*10+11,f.x*10+18,f.y*10+18); }

void AddSnake(Snake *snake) { int t=1,tx,ty,xx,yy; if(snake->curr<100)snake->curr++; xx=snake->data[0].x; yy=snake->data[0].y;

{ tx=snake->data[t].x; ty=snake->data[t].y; snake->data[t].x=xx; snake->data[t].y=yy; xx=tx; yy=ty; t++; }while(t<snake->curr); }

int GameOver() { int t; if(x<0||x>19||y<0||y>30)return 1; t = snake.curr-2; while(t) { if(snake.data[t].x==x&&snake.data[t].y==y)return 1; t--; } return 0; }

void MoveSnake(Snake *snake) { int t, xx, yy; int tx, ty; switch(direct) {

case 0: x+=1;break; //右 case 1: y+=1;break; case 2: x-=1;break; case 3: y-=1;break; }

if(GameOver())return ; xx=x;

if(xx==food.x&&yy==food.y) { t=snake->curr-1; food.y=rand()%31; food.x=rand()%20; while(t) { if(snake->data[t].x==food.x&&snake->data[t].y==food.y) { food.y=rand()%31; food.x=rand()%20; t=snake->curr-1; } t--; } score+=1; if((score%10)==0)speed-=20; AddSnake(snake); } t=snake->curr-1; do { tx=snake->data[t].x; ty=snake->data[t].y; snake->data[t].x=xx; snake->data[t].y=yy; xx=tx; yy=ty; t--; }while(t>=0); }

void DrawSnake(CDC *pDC,Snake snake) { int t;

t=snake.curr-1; pDC->SelectStockObject(BLACK_BRUSH); pDC->SelectStockObject(BLACK_PEN); pDC->Rectangle(snake.data[t].x*10+11,snake.data[t].y*10+11,snake.data[t].x*10+18,snake.data[t].y*10+18); t--; pDC->SelectStockObject(HOLLOW_BRUSH); while(t>=0) { pDC->Rectangle(snake.data[t].x*10+11,snake.data[t].y*10+11,snake.data[t].x*10+18,snake.data[t].y*10+18); t--; } }

void UnDrawSnake(CDC *pDC, Snake snake) { pDC->Rectangle(10,10,210,320); }

class CAboutDlg : public CDialog {

public: CAboutDlg();

// Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL

// Implementation protected: //{{AFX_MSG(CAboutDlg)

//}}AFX_MSG DECLARE_MESSAGE_MAP() };

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT }

void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP }

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) //}}AFX_MSG_MAP END_MESSAGE_MAP()

///////////////////////////////////////////////////////////////////////////// // CSnakeDlg dialog

CSnakeDlg::CSnakeDlg(CWnd* pParent /*=NULL*/) : CDialog(CSnakeDlg::IDD, pParent) { //{{AFX_DATA_INIT(CSnakeDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); }

void CSnakeDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSnakeDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP }

BEGIN_MESSAGE_MAP(CSnakeDlg, CDialog)

//{{AFX_MSG_MAP(CSnakeDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_COMMAND(IDM_HELP_ABOUT, OnHelpAbout) ON_WM_TIMER() ON_COMMAND(IDM_GAME_START, OnGameStart) ON_WM_KEYDOWN() ON_COMMAND(IDM_GAME_PAUSE_START, OnGamePauseStart) ON_COMMAND(IDM_GAME_EXIT, OnGameExit) //}}AFX_MSG_MAP END_MESSAGE_MAP()

///////////////////////////////////////////////////////////////////////////// // CSnakeDlg message handlers

BOOL CSnakeDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon InitSnake(&snake);

// TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control }

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

// If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework.

void CSnakeDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle 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; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else {

}

CDC *pDC = GetDC();

pDC->SetBkColor(RGB(210,215,210));

pDC->TextOut(220,30,"得分:0"); pDC->TextOut(220,80,"上:W"); pDC->TextOut(220,100,"下:S"); pDC->TextOut(220,120,"左:A"); pDC->TextOut(220,140,"右:D"); pDC->TextOut(220,160,"空格暂停"); //pDC->Rectangle(10,10,210,320); UnDrawSnake(pDC,snake); DrawSnake(pDC,snake); }

// The system calls this to obtain the cursor to display while the user drags // the minimized window.

HCURSOR CSnakeDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; }

void CSnakeDlg::OnHelpAbout() { // TODO: Add your command handler code here CAboutDlg dlg; KillTimer(1); dlg.DoModal(); if(!GameOver()&&nGameState)SetTimer(1,speed,NULL); }

void CSnakeDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default CDC *pDC =GetDC(); CBrush bus; if(GameOver()) { pDC->SetTextColor(RGB(255,0,0)); pDC->TextOut(70,130,"GameOver!!!"); nGameState=0; KillTimer(1);

else { pDC->SelectStockObject(BLACK_PEN); UnDrawSnake(pDC,snake); bus.CreateSolidBrush(RGB(0,0,0)); pDC->SelectObject(&bus); MoveSnake(&snake); DrawFood(pDC,food); DrawSnake(pDC,snake);

SetTimer(1,speed,NULL);

d=direct; //保留方向的副本 } CDialog::OnTimer(nIDEvent); }

void CSnakeDlg::OnGameStart() { // TODO: Add your command handler code here SetTimer(1,speed,NULL); InitSnake(&snake); nGameState=1; }

void CSnakeDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: Add your message handler code here and/or call default switch(nChar) { case 'D': if(direct!=2&&d!=2) direct=0; break; case 'S': if(direct!=3&&d!=3) direct=1; break; case 'A':

if(direct!=0&&d!=0) direct=2; break; case 'W': if(direct!=1&&d!=1) direct=3; break; } if(nChar==VK_SPACE) { if(nGameState==0) { if(!GameOver()) { SetTimer(1,speed,NULL); nGameState=1; } } else { if(!GameOver()) { KillTimer(1); nGameState=0; } } } CDialog::OnKeyDown(nChar, nRepCnt, nFlags); }

void CSnakeDlg::OnGamePauseStart() { // TODO: Add your command handler code here if(nGameState==0) { if(!GameOver()) { SetTimer(1,speed,NULL); nGameState=1; } } else

{ if(!GameOver()) { KillTimer(1); nGameState=0; } } }

void CSnakeDlg::OnGameExit() { // TODO: Add your command handler code here AfxGetMainWnd()->SendMessage(WM_CLOSE); }

本文来源:https://www.bwwdw.com/article/6tm4.html

Top