日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

QT写贪吃蛇

發布時間:2024/1/1 c/c++ 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QT写贪吃蛇 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

QT寫貪吃蛇

QT寫貪吃蛇的話,分幾個部分,一個是界面一個是算法,界面有游戲界面和貼圖界面。

1.界面需要注意的部分

void MainWindow::intbackground(){setAutoFillBackground(true);QPalette palette;palette.setBrush(QPalette::Background, QBrush(QPixmap(":/images/back.jpg").scaled(this->size())));setPalette(palette);ui->pushButton->setIcon(QIcon(":/images/quit.png"));//將圖片貼到pushbuttom上 ui->pushButton->setIconSize(QSize(100,100));//調整貼圖大小ui->pushButton->setFlat(true);//是否露出邊框ui->pushButton_2->setIcon(QIcon(":/images/start.png"));ui->pushButton_2->setIconSize(QSize(100,100));ui->pushButton_2->setFlat(true);// //ui->pushButton_3->setIcon(QIcon(":/images/AUTO1.png"));//ui->pushButton_3->setIconSize(QSize(100,100));//ui->pushButton_3->setFlat(true);} void MainWindow::intwindow(){setWindowTitle("貪吃蛇");//這個就是設計左上角的那段文字setWindowIcon(QIcon(":/images/icon.png"));//可以給一個貼圖在左上角 } //結束 void MainWindow::on_pushButton_clicked() { exit(1); } //開始 void MainWindow::on_pushButton_2_clicked()// { she *game=new she(this);//我建了一個叫做蛇的子類 game->show();//在創一個窗口 }

可能你們貼圖的時候無法顯示出來,這個問題,我也遇見過,說什么資源文件過大,你需要用ps處理下,好像是打開圖片另存為,在那個選項中去掉啥子,詳細過程可以去百度下。

//建立分數以及難度表格scorelable=new QLabel(this);//新建一個lablelevelable=new QLabel(this);scorelable->setText("score:");//添加文字scorelable->setGeometry(900,100,120,90);//位置處理levelable->setText("level:");levelable->setGeometry(900,140,120,90);scorenumber=new QLabel(this);levelablenum=new QLabel(this);lp=new QLabel(this);scorenumber->setText(QString::number(score)+"分");scorenumber->setGeometry(960,100,120,90);levelablenum->setText(QString::number(diff)+"級");levelablenum->setGeometry(960,140,120,90);//新建按鈕starpush=new QPushButton(this);pause=new QPushButton(this);exitpush=new QPushButton(this);autp=new QPushButton(this);autp->setIcon(QIcon(":/images/AUTO1.png"));//貼圖autp->setIconSize(QSize(60,60));//大小autp->setGeometry(850,600,60,60);//位置autp->setFlat(1);Timer=new QTimer(this);//新建一個timerconnect(exitpush,SIGNAL(clicked(bool)),this,SLOT(exit_push()));//將按鈕和函數鏈接起來connect(starpush,SIGNAL(clicked(bool)),this,SLOT(star_push()));connect(pause,SIGNAL(clicked(bool)),this,SLOT(pause_push()));connect(Timer,SIGNAL(timeout()),this,SLOT(time_p()));//這個特殊將一個函數與timer鏈接起來connect(autp,SIGNAL(clicked(bool)),this,SLOT(ai()));

關于蘋果的生成就幾個部分
1.隨機函數
2.2個數組來儲存位置
3.沖突的判斷
4.用一個私有成員來記錄蘋果的個數,來達到更新的作用

關于蛇的話就簡單的多,只要處理好碰撞問題,和方向問題就行了

//方向用一個私有成員代替direct void she::keyPressEvent(QKeyEvent *event){switch(event->key()){case Qt::Key_W:if(direct!=2)direct=1;break;case Qt::Key_S:if(direct!=1)direct=2;break;case Qt::Key_A:if(direct!=4)direct=3;break;case Qt::Key_D:if(direct!=3)direct=4;break;} } void she::pause_push(){Timer->stop();//停止connect(starpush,SIGNAL(clicked(bool)),this,SLOT(star_push()));starpush->setIcon(QIcon(":/images/Play.png"));disconnect(starpush,SIGNAL(clicked(bool)),this,SLOT(pause_push())); } void she::star_push(){Timer->start(500);沒500ms調用一次與timer鏈接的函數disconnect(starpush,SIGNAL(clicked(bool)),this,SLOT(star_push()));starpush->setIcon(QIcon(":/images/start.png"));connect(starpush,SIGNAL(clicked(bool)),this,SLOT(pause_push())); } 之前我們用timer和這個函數鏈接起來,我們在開始函數 void she::time_p() { //如果蛇頭碰到了蘋果if(snake[0][0]==foodx[lef]/40&&snake[0][1]==foody[lef]/40){data[n2][lef]='1';n_len=0;body++;score=score+10;//加分lef++;//吃到蘋果的個數int n4;n4=data[n2].size();if(n2==0)n4++;if(lef==n4-1){diff++;Timer->start(500-n2*20);}levelablenum->setText(QString::number(diff)+"級");scorenumber->setText(QString::number(score)+"分");}int n3=data[n2].size(); //如果吃到全部的蘋果if(lef>=n3){delete []foodx;delete []foody;n2++;n1=data[n2].size();foodx=new int[n1];foody=new int[n1];lp->setText(data2[n2]);for(int i=0;i<n1;i++){foodx[i]=(rand()%19)*40;if(foodx[i]==0)foodx[i]=40;foody[i]=(rand()%19)*40;if(foody[i]==0)foody[i]=40;}}lef=0;}for(int i=body;i!=0;i--){snake[i][0]=snake[i-1][0];snake[i][1]=snake[i-1][1];cap[snake[i][0]][snake[i][1]]=1;}}QPainter painter(this);switch (direct) { case 1:--snake[0][1];break;//,painter.drawImage(QRect((40*snake[0][0]),(40*snake[0][1]),40,40),QImage(":/images/shu.png"));break; case 2:++snake[0][1];break;//painter.drawImage(QRect((40*snake[0][0]),(40*snake[0][1]),40,40),QImage(":/images/shu.png"));break; case 3:--snake[0][0];break;//,painter.drawImage(QRect((40*snake[0][0]),(40*snake[0][1]),40,40),QImage(":/images/heng.png"));break; case 4:++snake[0][0];break;//,painter.drawImage(QRect((40*snake[0][0]),(40*snake[0][1]),40,40),QImage(":/images/heng.png"));break; default:break;}this->update();//更新 connect(Timer,SIGNAL(timeout()),this,SLOT(game_over()));//鏈接結束 } void she::game_over(){if(snake[0][0]<0||snake[0][0]>20||snake[0][1]<0||snake[0][1]>20){Timer->stop(); QMessageBox::Yes==QMessageBox::information(this,"Tips:","Game Over!",QMessageBox::Yes);this->hide();delete this;}elsefor(int i=1;i<body;i++){if(snake[0][0]==snake[i][0]&&snake[0][1]==snake[i][1]){Timer->stop();this->hide();QMessageBox::Yes==QMessageBox::information(this,"Tips:","Game Over!",QMessageBox::Yes);delete this;}}this->update();; }

總結

以上是生活随笔為你收集整理的QT写贪吃蛇的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。