




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Qt课程设计说明书题 目: 贪吃蛇游戏设计院 系:辽宁科技大学软件学院专业班级:网络工程092班学 号:120093503067学生姓名:韩业斌指导教师:包晗 2012年 7 月 3日目录功能需求3界面要求4其他要求4设计分析5操作方法及运行结果5设计体会7参考文献7功能需求利用Qt creator制作一个贪吃蛇的小游戏,要求:(1) 在窗口上显示菜单栏,帮助栏 和工具栏(2) 游戏含有正常模式、死亡模式、情侣模式三种(3) 能够记录游戏时间,游戏成绩,游戏排行(4) 能够显示英雄榜(5) 能够显示帮助提示界面要求 贪吃蛇游戏的游戏界面包括背景图片、工具栏图片、蛇移动范围的绘制等等。其中贪吃蛇
2、的身体用什么方法绘制,才可以使得其在游戏过程中可以实现“吃”的功能是很重要的。因此在游戏界面的初始绘制时就必须考虑到游戏时可能遇到的问题。导入位图以及菜单工具条后,游戏未开始前(win7系统下)的界面显示如图2-1所示其他要求 能够注意各种异常处理,注重提高程序运行效率设计分析 根据分析,贪吃蛇这个程序一共要实现如下几个功能,包括游戏方面正常模式、死亡模式以及情侣模式,游戏帮助提示与英雄榜的显示等等。具体的程序结构如下操作方法及运行结果根据分析后的贪吃蛇结构设计出相应的贪吃蛇流程。贪吃蛇的内容主要包括: 普通模式用上下左右控制方向,随机出现食物,但是如果碰到边框,障碍物和自己都会挂掉;死亡模式
3、用上下左右控制方向,小蛇获得了穿越边框的能力,但是得了恶性长胖病,可以通过吃减肥药来偏强控制,但是不要碰到自己排除的废物;情侣模式玩家1用上下左右控制方向,用P、O、I(或者是1、2、0)来发射子弹,可以后退,不能穿越边框玩家2用W,A,S,D来控制方向,空格键释放陷阱子弹和陷阱都需要food才能补充;英雄榜能够记录正常模式和死亡模式游戏时间,游戏成绩,游戏排行,玩家姓名; 按下帮助键可获得游戏帮助说明。设计体会我学会了用Qt编写贪吃蛇游戏,能熟练地掌握Qt语言,通过每章每节的学习让我知道了Qt的重要性,学习Qt能让我们深刻的知道在以后的工作中能用到,还有让我知道学习的重要意义,通过 对Qt的
4、学习能让我们解决很多的实际东西,QT能让我们锻炼自己的编程能力,还能处理许多别的语言处理不了的东西,我知道了学习Qt不仅能提高自己的认识 ,还巩固了对原来学过的其他语言的学习。参考文献Qt 4开发实践(电子工业出版社)部分源代码:#include "games.h"Games:Games(QWidget *parent) : QMainWindow(parent) setWindowTitle(tr("MainWindow"); showWidget =new ShowWidget(this); setCentralWidget(showWidget);
5、 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("普通模式&quo
6、t;),this); normalAction->setStatusTip(tr("进入普通模式"); connect(normalAction,SIGNAL(triggered(),this,SLOT(shownsnake(); /"死亡模式"动作 deathAction =new QAction(QIcon("12R00Z110F-Q055_lit.png"),tr("死亡模式"),this); deathAction->setStatusTip(tr("进入死亡模式"); co
7、nnect(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(); /&q
8、uot;英雄榜"动作 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
9、.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 Gam
10、es:createMenus() /菜单 fileMenu =menuBar()->addMenu(tr("菜单"); fileMenu->addAction(normalAction); fileMenu->addAction(deathAction); fileMenu->addAction(loversAction); fileMenu->addAction(heroAction); fileMenu->addSeparator(); fileMenu->addAction(exitAction); /帮助 helpMenu
11、=menuBar()->addMenu(tr("帮助"); helpMenu ->addAction(helpAction);/工具栏的实现void Games:createToolBars() /文件工具栏 fileTool =addToolBar("File"); fileTool->addAction(normalAction); fileTool->addAction(deathAction); fileTool->addAction(loversAction); fileTool->addAction(her
12、oAction); 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 newdsna
13、ke; newdsnake.show(); newdsnake.exec();void Games:showlsnake() lsnake newlsnake; newlsnake.show(); newlsnake.exec();/死亡模式#include "deathsnake.h"#include <QPalette>#include <QColor>#include <QTime>#include <QMessageBox>#include <QPainter>#include <QFrame>
14、#include <QString>#include <QPainter>#include <QPixmap>#include "dialog.h"#include <QFile>#include <QTextStream>#include <QTextCodec>deathsnake:deathsnake() setWindowTitle(tr("死亡模式"); this->resize(800,480); QTextCodec:setCodecForTr(QTextCode
15、c: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(
16、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; /规
17、定食物大小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-&g
18、t;setMidLineWidth(1); int fx=(qrand()%(gwidth/20)*20; int fy=(qrand()%(gheight/20)*20; int n; /食物不在蛇身上 int s=data.size(); for(n=0;n<s;n+) int bhx=datan->x(); int bhy=datan->y(); while(fx=bhx&&fy=bhy) fx=(qrand()%(gwidth/20)*20; fy=(qrand()%(gheight/20)*20; /食物不能在墙上 int ws=wdata.size
19、(); for(n=0;n<ws;n+) int wx=wdatan->x(); 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() QLabel *food=new QLabel(this); food->resize(20,20); food
20、->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); food->move(1280,769); food->show(); return food;/初始化蛇头位置QLabel* dea
21、thsnake:initSnake() 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->setMidLineWidt
22、h(3); food->move(100,200); food->show(); QMessageBox msg; /level 1 msg.setWindowTitle(tr("正在进入死亡模式:"); msg.setText("How many secs can you hold on?"); msg.setStandardButtons(QMessageBox:Ok); msg.show(); msg.exec(); return food;/舍身移动void deathsnake:smove() int nhx=data0->x
23、(); int nhy=data0->y(); int s=data.size(); int ws=wdata.size(); int n; /蛇头不能与蛇身重合 for(n=1;n<s;n+) int bhx=datan->x(); int bhy=datan->y(); if(nhx=bhx&&nhy=bhy) QString word="Game Over!nEat yourself!So you die!" GameOver(word); /蛇头不能撞墙 for(n=1;n<ws;n+) int wx=wdatan-&g
24、t;x(); int wy=wdatan->y(); if(nhx=wx&&nhy=wy) QString word="Game Over!nEat yourshit!So you die!" GameOver(word); /蛇头与食物重合 if(nhx=food->x()&&nhy=food->y() if(data.size()>6) for(int cc=0;cc<5;cc+) datadata.size()-1->setPalette(QPalette(QColor(130,130,130); w
25、data.push_back(datadata.size()-1); data.pop_back(); food->hide(); food=getFood(); /蛇头位置超过边界 if(nhx<0) nhx=800; if(nhx>800) nhx=-20; if(nhy<0) nhy=480; if(nhy>480) nhy=-20; /移动 s=data.size(); switch(dire) case d_up:nhy-=20;break; case d_down:nhy+=20;break; case d_left:nhx-=20;break; ca
26、se d_right:nhx+=20;break; default:break; /后一个向前一个移动 for(int i=s-1;i>0;i-) datai->move(datai-1->x(),datai-1->y(); data0->move(nhx,nhy);/键盘操作void deathsnake:keyPressEvent(QKeyEvent *e) if(e->key()=Qt:Key_Up) if(dire=d_down); else dire=d_up; else if(e->key()=Qt:Key_Down) if(dire=d_
27、up); else dire=d_down; else if(e->key()=Qt:Key_Left) if(dire=d_right); else dire=d_left; else if(e->key()=Qt:Key_Right) if(dire=d_left); else dire=d_right; else ;/计时器void deathsnake:timerEvent(QTimerEvent *e) sec+; if(sec>9&&hou=0) min+; jsq2->resize(128,128); jsq2->move(282,1
28、76); jsq->move(390,176); sec=0; clear=true; if(sec>9&&hou!=0) min+; sec=0; if(min>9) hou+; jsq3->resize(128,128); jsq3->move(228,176); jsq2->move(336,176); jsq->move(444,176); min=0; sec=0; jsq->setPixmap(QString:number(sec)+".png"); jsq2->setPixmap(QStri
29、ng:number(min)+".png"); jsq3->setPixmap(QString:number(hou)+".png"); if(time=20) QString word="Unbelivablebleble!You win!" GameOver(word); time-=2; timer->setInterval(time); data.push_back(snakeGrow(); if(clear=true&&wdata.size()>0) wdata0->hide(); wdata.pop_front(); /游戏结束void deathsnake:GameOver(QString go) QMessageB
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年九年级语文上册 第一单元 第1课《沁园春 雪》教学设计1 新人教版
- 九年级化学下册 第8单元 金属和金属材料 课题3 金属资源的利用和保护 第2课时 金属资源的保护教学设计 (新版)新人教版
- 6营养要均衡 教学设计-2024-2025学年科学四年级上册教科版
- 自考现代教育技术实践课
- 联合申报合作协议
- ICU专科护理评审方法课件
- 《第五单元 唱歌 其多列》(教学设计)-2023-2024学年人教版(2012)音乐一年级下册
- 2024-2025版新教材高中化学 第1章 第1节 第1课时 物质的分类及物质的转化教学设计 新人教版必修第一册
- 七年级信息技术 8.3制作基本动画教学设计 人教新课标版
- 统计学培训课件
- QSPI-9708-2016-光伏电站安装施工质量验收规定
- 公路水运工程施工企业主要负责人和安全生产管理人员考核大纲和模拟试题库1
- 财政投资评审咨询服务预算和结算评审项目 投标方案(技术方案)
- 细胞制备中心建设与管理规范
- 商业空间设计(高职环境艺术设计专业和室内设计专业)全套教学课件
- 2024公路工程岩石试验规程
- 幼儿园课件:《父爱如山》
- 四川乡村振兴文旅策划方案-全面推进农业与旅游、教育、文化、健康养老等多产业带深度融合
- 2024年上海杨浦区社区工作者招聘笔试冲刺题(带答案解析)
- 个人医保代办委托书
- 2023年-2024年新《管理学原理》考试题库(含答案)
评论
0/150
提交评论