Qt课程设计贪吃蛇游戏设计Word格式文档下载.docx

上传人:b****4 文档编号:16906078 上传时间:2022-11-27 格式:DOCX 页数:20 大小:552.13KB
下载 相关 举报
Qt课程设计贪吃蛇游戏设计Word格式文档下载.docx_第1页
第1页 / 共20页
Qt课程设计贪吃蛇游戏设计Word格式文档下载.docx_第2页
第2页 / 共20页
Qt课程设计贪吃蛇游戏设计Word格式文档下载.docx_第3页
第3页 / 共20页
Qt课程设计贪吃蛇游戏设计Word格式文档下载.docx_第4页
第4页 / 共20页
Qt课程设计贪吃蛇游戏设计Word格式文档下载.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

Qt课程设计贪吃蛇游戏设计Word格式文档下载.docx

《Qt课程设计贪吃蛇游戏设计Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Qt课程设计贪吃蛇游戏设计Word格式文档下载.docx(20页珍藏版)》请在冰豆网上搜索。

Qt课程设计贪吃蛇游戏设计Word格式文档下载.docx

部分源代码:

#include"

games.h"

Games:

:

Games(QWidget*parent)

:

QMainWindow(parent)

{

setWindowTitle(tr("

MainWindow"

));

showWidget=newShowWidget(this);

setCentralWidget(showWidget);

createActions();

createMenus();

createToolBars();

if(img.load("

4.jpg"

))

{

showWidget->

imageLabel->

setPixmap(QPixmap:

fromImage(img));

}

}

~Games()

{}

//动作的实现

voidGames:

createActions()

//"

普通模式"

动作

normalAction=newQAction(QIcon("

12R009143A0-30P46_lit.png"

),tr("

),this);

normalAction->

setStatusTip(tr("

进入普通模式"

connect(normalAction,SIGNAL(triggered()),this,SLOT(shownsnake()));

死亡模式"

deathAction=newQAction(QIcon("

12R00Z110F-Q055_lit.png"

deathAction->

进入死亡模式"

connect(deathAction,SIGNAL(triggered()),this,SLOT(showdsnake()));

情侣模式"

loversAction=newQAction(QIcon("

12N3M01I10-212R6_lit.png"

loversAction->

进入情侣模式"

connect(loversAction,SIGNAL(triggered()),this,SLOT(showlsnake()));

英雄榜"

heroAction=newQAction(QIcon("

12S216253R0-101527_lit.png"

heroAction->

connect(heroAction,SIGNAL(triggered()),this,SLOT(showHerolist()));

退出"

exitAction=newQAction(QIcon("

12N3M224630-2HE4_lit.png"

exitAction->

connect(exitAction,SIGNAL(triggered()),this,SLOT(close()));

帮助"

helpAction=newQAction(tr("

howtopaly"

connect(helpAction,SIGNAL(triggered()),this,SLOT(showHelp()));

//菜单(Menus)的实现

createMenus()

//菜单

fileMenu=menuBar()->

addMenu(tr("

菜单"

fileMenu->

addAction(normalAction);

addAction(deathAction);

addAction(loversAction);

addAction(heroAction);

addSeparator();

addAction(exitAction);

//帮助

helpMenu=menuBar()->

helpMenu->

addAction(helpAction);

//工具栏的实现

createToolBars()

//文件工具栏

fileTool=addToolBar("

File"

);

fileTool->

showHelp()

newhelp=newhelp();

newhelp->

show();

showHerolist()

newherolist=newherolist();

newherolist->

shownsnake()

nsnakenewnsnake;

newnsnake.show();

newnsnake.exec();

showdsnake()

dsnakenewdsnake;

newdsnake.show();

newdsnake.exec();

showlsnake()

lsnakenewlsnake;

newlsnake.show();

newlsnake.exec();

//死亡模式

deathsnake.h"

#include<

QPalette>

QColor>

QTime>

QMessageBox>

QPainter>

QFrame>

QString>

QPixmap>

dialog.h"

QFile>

QTextStream>

QTextCodec>

deathsnake:

deathsnake()

this->

resize(800,480);

QTextCodec:

setCodecForTr(QTextCodec:

codecForName("

GBK"

dire=d_right;

time=300;

sec=0;

min=0;

hou=0;

clear=false;

//计时器

jsq=newQLabel(this);

jsq->

resize(128,128);

move(336,176);

setPixmap(QString:

number(sec)+"

.png"

jsq2=newQLabel(this);

jsq3=newQLabel(this);

food=getFood();

data.push_back(initSnake());

qsrand(QTime().currentTime().msec());

//蛇move

timer=newQTimer();

timer->

setInterval(time);

start();

Ttimer=this->

startTimer(1000);

connect(timer,SIGNAL(timeout()),this,SLOT(smove()));

~deathsnake()

//蛇吃食物

QLabel*deathsnake:

getFood()

intgwidth=800;

intgheight=480;

//规定食物大小20,生成的位置是20的倍数

//位置随机,并且在界面范围之内

QLabel*food=newQLabel(this);

food->

resize(20,20);

setAutoFillBackground(true);

setPalette(QPalette(QColor(qrand()%155+100,qrand()%155+100,qrand()%155+100)));

setFrameStyle(QFrame:

Box|QFrame:

Sunken);

setLineWidth(3);

setMidLineWidth

(1);

intfx=(qrand()%(gwidth/20))*20;

intfy=(qrand()%(gheight/20))*20;

intn;

//食物不在蛇身上

ints=data.size();

for(n=0;

n<

s;

n++)

intbhx=data[n]->

x();

intbhy=data[n]->

y();

while(fx==bhx&

&

fy==bhy)

fx=(qrand()%(gwidth/20))*20;

fy=(qrand()%(gheight/20))*20;

//食物不能在墙上

intws=wdata.size();

ws;

intwx=wdata[n]->

intwy=wdata[n]->

while(fx==wx&

fy==wy)

move(fx,fy);

returnfood;

//蛇生长

snakeGrow()

move(1280,769);

//初始化蛇头位置

initSnake()

setMidLineWidth(3);

move(100,200);

QMessageBoxmsg;

//level1

msg.setWindowTitle(tr("

正在进入死亡模式:

"

msg.setText("

Howmanysecscanyouholdon?

msg.setStandardButtons(QMessageBox:

Ok);

msg.show();

msg.exec();

//舍身移动

voiddeathsnake:

smove()

intnhx=data[0]->

intnhy=data[0]->

//蛇头不能与蛇身重合

for(n=1;

if(nhx==bhx&

nhy==bhy)

QStringword="

GameOver!

\nEatyourself!

Soyoudie!

;

GameOver(word);

//蛇头不能撞墙

if(nhx==wx&

nhy==wy)

\nEatyourshit!

//蛇头与食物重合

if(nhx==food->

x()&

nhy==food->

y())

if(data.size()>

6)

for(intcc=0;

cc<

5;

cc++)

data[data.size()-1]->

setPalette(QPalette(QColor(130,130,130)));

wdata.push_back(data[data.size()-1]);

data.pop_back();

hide();

//蛇头位置超过边界

if(nhx<

0)

nhx=800;

if(nhx>

800)

nhx=-20;

if(nhy<

nhy=480;

if(nhy>

480)

nhy=-20;

//移动

s=data.size();

switch(dire)

cased_up:

nhy-=20;

break;

cased_down:

nhy+=20;

cased_left:

nhx-=20;

cased_right:

nhx+=20;

default:

//后一个向前一个移动

for(inti=s-1;

i>

0;

i--)

data[i]->

move(data[i-1]->

x(),data[i-1]->

y());

data[0]->

move(nhx,nhy);

//键盘操作

keyPressEvent(QKeyEvent*e)

if(e->

key()==Qt:

Key_Up)

if(dire==d_down);

elsedire=d_up;

elseif(e->

Key_Down)

if(dire==d_up);

elsedire=d_down;

Key_Left)

if(dire==d_right);

elsedire=d_left;

Key_Right)

if(dire==d_left);

elsedire=d_right;

else;

//计时器

timerEvent(QTimerEvent*e)

sec++;

if(sec>

9&

hou==0)

min++;

jsq2->

move(282,176);

move(390,176);

clear=true;

hou!

=0)

if(min>

9)

hou++;

jsq3->

move(228,176);

move(444,176);

number(min)+"

number(hou)+"

if(time==20)

Unbelivablebleble!

!

Youwin!

time-=2;

data.push_back(snakeGrow());

if(clear==true&

wdata.size()>

wdata[0]->

wdata.pop_front();

//游戏结束

GameOver(QStringgo)

msg.setText(go);

killTimer(Ttimer);

if(msg.exec()==QMessageBox:

Ok)

QFilefile("

dinformation.txt"

file.open(QIODevice:

ReadOnly|QIODevice:

Text);

QTextStreams(&

file);

while(!

s.atEnd())

rec.name=s.readLine();

rec.score=s.readLine().toInt();

rec.time=QDateTime:

fromString(s.readLine(),"

yyyy-MM-ddhh:

mm:

ss"

myinfo.push_back(rec);

if(myinfo.size()==0)

dialogd;

d.text

(1);

d.show();

d.exec();

d.deathwrite(hou*100+min*10+sec,1);

close();

elseif(myinfo.size()<

10)

intw;

for(w=0;

w<

myinfo.size();

w++)

if(myinfo[w].score<

(hou*100+min*10+sec))

break;

d.text(w+1);

d.deathwrite(hou*100+min*10+sec,w);

elseif(myinfo[myinfo.size()-1].score>

=(hou*100+min*10+sec))

else

for(intw=0;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 成人教育 > 自考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1