Qt课程设计--贪吃蛇游戏设计.docx
Qt课程设计-贪吃蛇游戏设计 Qt课程设计说明书题目: 贪吃蛇游戏设计 目录 功能需求 (3) 界面要求 (3) 其他要求 (4) 设计分析 (4) 操作方法及运行结果 (4) 设计体会 (6) 参考文献 (6) 功能需求 利用Qt creator制作一个贪吃蛇的小游戏,要求: (1)在窗口上显示菜单栏,帮助栏和工具栏 (2)游戏含有正常模式、死亡模式、情侣模式三种 (3)能够记录游戏时间,游戏成绩,游戏排行 (4)能够显示英雄榜 (5)能够显示帮助提示 界面要求 贪吃蛇游戏的游戏界面包括背景图片、工具栏图片、蛇移动范围的绘制等等。其中贪吃蛇的身体用什么方法绘制,才可以使得其在游戏过程中可以实现“吃”的功能是很重要的。因此在游戏界面的初始绘制时就必须考虑到游戏时可能遇到的问题。 导入位图以及菜单工具条后,游戏未开始前(win7系统下)的界面显示如图2-1所示 其他要求 能够注意各种异常处理,注重提高程序运行效率 设计分析 根据分析,贪吃蛇这个程序一共要实现如下几个功能,包括游戏方面正常模式、死亡模式以及情侣模式,游戏帮助提示与英雄榜的显示等等。具体的程序结构如下 操作方法及运行结果 根据分析后的贪吃蛇结构设计出相应的贪吃蛇流程。贪吃蛇的内容主要包括: 普通模式用上下左右控制方向,随机出现食物,但是如果碰到边框,障碍物和自己都会挂掉; 死亡模式用上下左右控制方向,小蛇获得了穿越边框的能力,但是得了恶性长胖病,可以通过吃减肥药来偏强控制,但是不要碰到自己排除的废物; 情侣模式玩家1用上下左右控制方向,用P、O、I(或者是1、2、0)来发射子弹,可以后退,不能穿越边框玩家2用W,A,S,D来控制方向,空格键释放陷阱子弹和陷阱都需要food才能补充; 英雄榜能够记录正常模式和死亡模式游戏时间,游戏成绩,游戏排行,玩家姓名; 按下帮助键可获得游戏帮助说明。 设计体会 我学会了用Qt编写贪吃蛇游戏,能熟练地掌握Qt语言,通过每章每节的学习让我知道了Qt的重要性,学习Qt能让我们深刻的知道在以后的工作中能用到,还有让我知道学习的重要意义,通过对Qt的学习能让我们解决很多的实际东西,QT能让我们锻炼自己的编程能力,还能处理许多别的语言处理不了的东西,我知道了学习Qt不仅能提高自己的认识,还巩固了对原来学过的其他语言的学习。 参考文献 Qt 4开发实践(电子工业出版社) 部分源代码: #include "games.h" Games:Games(QWidget *parent) : QMainWindow(parent) setWindowTitle(tr("MainWindow"); showWidget =new ShowWidget(this); setCentralWidget(showWidget); createActions(); createMenus(); createToolBars(); if(img.load("4.jpg") showWidget->imageLabel->setPixmap(QPixmap:fromImage(img); Games:Games() /动作的实现 void Games:createActions() /"普通模式"动作 normalAction =new QAction(QIcon("12R009143A0-30P46_lit.png"),tr("普通模式"),this); normalAction->setStatusTip(tr("进入普通模式"); connect(normalAction,SIGNAL(triggered(),this,SLOT(shownsnake(); /"死亡模式"动作 deathAction =new QAction(QIcon("12R00Z110F-Q055_lit.png"),tr("死亡模式"),this); deathAction->setStatusTip(tr("进入死亡模式"); connect(deathAction,SIGNAL(triggered(),this,SLOT(showdsnake(); /"情侣模式"动作 loversAction =new QAction(QIcon("12N3M01I10-212R6_lit.png"),tr("情侣模式"),this); loversAction->setStatusTip(tr("进入情侣模式"); connect(loversAction,SIGNAL(triggered(),this,SLOT(showlsnake(); /"英雄榜"动作 heroAction =new QAction(QIcon("12S216253R0-101527_lit.png"),tr("英雄榜"),this); heroAction->setStatusTip(tr("英雄榜"); connect(heroAction,SIGNAL(triggered(),this,SLOT(showHerolist(); /"退出"动作 exitAction =new QAction(QIcon("12N3M224630-2HE4_lit.png"),tr("退出"),this); exitAction->setStatusTip(tr("退出"); connect(exitAction,SIGNAL(triggered(),this,SLOT(close(); /"帮助"动作 helpAction =new QAction(tr("how to paly"),this); connect(helpAction,SIGNAL(triggered(),this,SLOT(showHelp(); /菜单(Menus)的实现 void Games:createMenus() /菜单 fileMenu =menuBar()->addMenu(tr("菜单"); fileMenu->addAction(normalAction); fileMenu->addAction(deathAction); fileMenu->addAction(loversAction); fileMenu->addAction(heroAction); fileMenu->addSeparator(); fileMenu->addAction(exitAction); /帮助 helpMenu =menuBar()->addMenu(tr("帮助"); helpMenu ->addAction(helpAction); /工具栏的实现 void Games:createToolBars() /文件工具栏 fileTool =addToolBar("File"); fileTool->addAction(normalAction); fileTool->addAction(deathAction); fileTool->addAction(loversAction); fileTool->addAction(heroAction); fileTool->addAction(exitAction); void Games:showHelp() newhelp =new help(); newhelp->show(); void Games:showHerolist() newherolist =new herolist(); newherolist->show(); void Games:shownsnake() nsnake newnsnake; newnsnake.show(); newnsnake.exec(); void Games:showdsnake() dsnake newdsnake; newdsnake.show(); newdsnake.exec(); void Games:showlsnake() lsnake newlsnake; newlsnake.show(); newlsnake.exec(); /死亡模式 #include"deathsnake.h" #include #include #include #include #include #include #include #include #include #include"dialog.h" #include #include #include deathsnake:deathsnake() setWindowTitle(tr("死亡模式"); this->resize(800,480); QTextCodec:setCodecForTr(QTextCodec:codecForName("GBK"); dire=d_right; time=300; sec=0; min=0; hou=0; clear=false; /计时器 jsq=new QLabel(this); jsq->resize(128,128); jsq->move(336,176); jsq->setPixmap(QString:number(sec)+".png"); jsq2=new QLabel(this); jsq3=new QLabel(this); food=getFood(); data.push_back(initSnake(); qsrand(QTime().currentTime().msec(); /蛇move timer=new QTimer(); timer->setInterval(time); timer->start(); Ttimer=this->startTimer(1000); connect(timer,SIGNAL(timeout(),this,SLOT(smove(); deathsnake:deathsnake() /蛇吃食物 QLabel*deathsnake:getFood() int gwidth=800; int gheight=480; /规定食物大小20,生成的位置是20的倍数 /位置随机,并且在界面范围之内 QLabel*food=new QLabel(this); food->resize(20,20); food->setAutoFillBackground(true); food->setPalette(QPalette(QColor(qrand()%155+100,qrand()%155+100,qrand()%155+100); food->setFrameStyle(QFrame:Box|QFrame:Sunken); food->setLineWidth(3); food->setMidLineWidth(1); int fx=(qrand()%(gwidth/20)*20; int fy=(qrand()%(gheight/20)*20; int n; /食物不在蛇身上 int s=data.size(); for(n=0;nx(); int bhy=datan->y(); while(fx=bhx&&fy=bhy) fx=(qrand()%(gwidth/20)*20; fy=(qrand()%(gheight/20)*20; /食物不能在墙上 int ws=wdata.size(); for(n=0;nx(); int wy=wdatan->y(); while(fx=wx&&fy=wy) fx=(qrand()%(gwidth/20)*20; fy=(qrand()%(gheight/20)*20; food->move(fx,fy); food->show(); return food; /蛇生长 QLabel*deathsnake:snakeGrow()