資源簡介
貪吃蛇

代碼片段和文件信息
#include?“gamewidget.h“
#include?“ui_gamewidget.h“
#include?
#include?
#include?
#include?
#include?
#include?
#include?
gamewidget::gamewidget(QWidget?*parent)?:
????QDialog(parent)
????ui(new?Ui::gamewidget)
{
????ui->setupUi(this);
????ui->setupUi(this);
????this->setAutoFillBackground(true);
????this->setWindowIcon(QIcon(“:/new/prefix1/img/1.png“));
????QPalette?palette;
????palette.setBrush(QPalette::BackgroundQBrush(QPixmap(“:/new/prefix1/img/green.jpg“).scaled(this->size())));
????this->setPalette(palette);
????//聲音區
????sound=new?QSound(“:/listen/img/5611.wav“);
????sound1=new?QSound(“:/listen/img/die.wav“);
????//設置分數等級為0
????score?=?0;
????level?=?0;
????str1=QString::number(score);
????str2=QString::number(level);
????ui->scoreshow->setText(str1);
????ui->levelshow->setText(str2);
????foodcount=0;??//食物初始化
????foodx=qrand()%COL;??//初始化X
????foody=qrand()%ROW;?//初始化?y
????direction=qrand()%4;?//方向初始化
????snake[0][0]=qrand()%COL;
????snake[0][1]=qrand()%ROW;
????int??i?=?1;
????while(i)?//食物出現的位置?不能跟snake?頭?位置一樣
????{
????????if(snake[0][0]==foodx&&snake[0][1]==foody)
????????{
????????????foodx=qrand()%COL;
????????????foody=qrand()%ROW;
????????????i=1;
????????}
????????else
????????{
????????????i=0;
????????}
????}
????timer=new?QTimer(this);
????timer->setInterval(500);??//設置snake?跑?快慢速度
????connect(timerSIGNAL(timeout())thisSLOT(timeoutSlot()));
????//設置按鈕操作
????connect(ui->leftbtnSIGNAL(clicked())thisSLOT(leftbtnSlot()));
????connect(ui->rightbtnSIGNAL(clicked())thisSLOT(rightbtnSlot()));
????connect(ui->upbtnSIGNAL(clicked())thisSLOT(upbtnSlot()));
????connect(ui->downbtnSIGNAL(clicked())thisSLOT(downbtnSlot()));
????connect(ui->startbtnSIGNAL(clicked())thisSLOT(startbtnSlot()));
????connect(ui->stopbtnSIGNAL(clicked())thisSLOT(stopbtnSlot()));
????connect(ui->returnbtnSIGNAL(clicked())thisSLOT(returnbtnSlot()));
????//設置鍵盤操作
????connect(thisSIGNAL(UpSignal())?ui->upbtnSLOT(click()));
????connect(thisSIGNAL(DownSignal())?ui->downbtnSLOT(click()));
????connect(thisSIGNAL(LeftSignal())?ui->leftbtnSLOT(click()));
????connect(thisSIGNAL(RightSignal())?ui->rightbtnSLOT(click()));
}
void?gamewidget::timeoutSlot()
{
????//判斷是否吃到食物
????if(snake[0][0]==foodx?&&?snake[0][1]==foody)??//當蛇頭跟食物位置重合后
????{
????????//吃到食物發出聲音
????????sound->play();
????????//如果吃到食物,食物坐標更新
????????foodcount++;
????????score++;
????????str1=QString::number(score);
????????ui->scoreshow->setText(str1);
????????foodx=qrand()%COL;???//隨機產生新的食物
????????foody=qrand()%ROW;
????????int?i=0;
????????while(i<=foodcount)
????????{
????????????i++;
????????????//判斷食物是否出現在蛇身上,如果出現在蛇身上重新產生食物
????????????if(snake[i][0]==foodx?&&?snake[i][1]==foody)
????????????{
????????????????foodx=qrand()%COL;
????????????????foody=qrand()%ROW;
????????????????i=0;
????????????}
????????}
????????switch(score)
????????{
????????case?10:
????????????timer->setInterval(350
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-07-08?01:52??snakegame\
?????目錄???????????0??2015-12-02?11:18??snakegame\img\
?????文件????????1204??2017-07-07?09:13??snakegame\snakegame.ui
?????文件????????1898??2017-07-07?10:29??snakegame\snakegame.cpp
?????文件????????3475??2017-07-07?12:06??snakegame\gamewidget.ui
?????文件?????????416??2017-07-07?09:22??snakegame\snakegame.h
?????文件????????1128??2017-07-07?11:05??snakegame\gamewidget.h
?????文件?????????482??2017-07-07?09:24??snakegame\snakegame.pro
?????文件???????13598??2017-07-08?01:52??snakegame\gamewidget.cpp
?????文件????????1210??2017-07-07?09:10??snakegame\snakeresource.qrc
?????文件???????13121??2017-07-07?09:09??snakegame\snakegame.pro.user
?????文件?????????130??2017-07-07?09:10??snakegame\listenresource.qrc
?????文件?????????399??2017-07-07?09:14??snakegame\main.cpp
?????文件????????1568??2013-07-31?04:32??snakegame\img\v_body.png
?????文件????????1553??2013-07-31?04:30??snakegame\img\tr_corner.png
?????文件????????1565??2013-07-31?04:29??snakegame\img\tl_corner.png
?????文件????????1586??2013-07-31?09:11??snakegame\img\tailup.png
?????文件????????1440??2013-07-31?09:10??snakegame\img\tailright.png
?????文件????????1442??2013-07-31?09:10??snakegame\img\tailleft.png
?????文件????????1573??2013-07-31?09:10??snakegame\img\taildown.png
?????文件????????1599??2013-07-31?05:01??snakegame\img\tail.png
?????文件???????25874??2015-04-21?02:38??snakegame\img\start.png
?????文件????????3516??2013-08-21?18:33??snakegame\img\rats.png
?????文件????????2944??2013-08-19?11:40??snakegame\img\rat.png
?????文件??????108809??2015-01-27?07:43??snakegame\img\quit.png
?????文件???????42745??2013-07-30?16:47??snakegame\img\logo.png
?????文件???????15116??2015-01-27?01:50??snakegame\img\icon.png
?????文件????????1713??2013-07-31?06:11??snakegame\img\headup.png
?????文件????????1734??2013-07-31?06:11??snakegame\img\headright.png
?????文件????????1714??2013-07-31?06:11??snakegame\img\headleft.png
?????文件????????1734??2013-07-31?06:11??snakegame\img\headdown.png
............此處省略16個文件信息
- 上一篇:湖南工業大學大型數據庫應用歷年試卷
- 下一篇:CUDA JPEG編碼
評論
共有 條評論