第一范文网 - 专业文章范例文档资料分享平台

Qt课程设计--贪吃蛇游戏设计

来源:用户分享 时间:2025/9/29 16:00:12 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

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 \ #include #include #include #include #include #include #include #include #include #include \ #include #include #include deathsnake::deathsnake() {

setWindowTitle(tr(\死亡模式\)); this->resize(800,480);

9

QTextCodec::setCodecForTr(QTextCodec::codecForName(\)); 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)+\); 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()5+100,qrand()5+100,qrand()5+100)));

10

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;n

int bhx=data[n]->x(); int bhy=data[n]->y(); while(fx==bhx&&fy==bhy) {

fx=(qrand()%(gwidth/20))*20; fy=(qrand()%(gheight/20))*20; } }

//食物不能在墙上 int ws=wdata.size(); for(n=0;n

int wx=wdata[n]->x(); int wy=wdata[n]->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() {

11

QLabel *food=new QLabel(this); food->resize(20,20);

food->setAutoFillBackground(true);

food->setPalette(QPalette(QColor(qrand()5+100,qrand()5+100,qrand()5+100))); food->setFrameStyle(QFrame::Box | QFrame::Sunken); food->setLineWidth(3); food->setMidLineWidth(1); food->move(1280,769); food->show(); return food; }

//初始化蛇头位置

QLabel* deathsnake::initSnake() {

QLabel *food=new QLabel(this); food->resize(20,20);

food->setAutoFillBackground(true);

food->setPalette(QPalette(QColor(qrand()5+100,qrand()5+100,qrand()5+100))); food->setFrameStyle(QFrame::Box | QFrame::Sunken); food->setLineWidth(3); food->setMidLineWidth(3); food->move(100,200); food->show(); QMessageBox msg; //level 1

msg.setWindowTitle(tr(\正在进入死亡模式:\)); msg.setText(\ many secs can you hold on?\); msg.setStandardButtons(QMessageBox::Ok); msg.show(); msg.exec(); return food; }

//舍身移动

void deathsnake::smove() {

int nhx=data[0]->x(); int nhy=data[0]->y();

12

搜索更多关于: Qt课程设计--贪吃蛇游戏设计 的文档
Qt课程设计--贪吃蛇游戏设计.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c5g4an0w96y3y3j84vsq02xzhu2kzn0009ua_3.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top