当前位置:首页 > Qt课程设计--贪吃蛇游戏设计
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
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
共分享92篇相关文档