widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include<QPushButton>
#include <QWidget>
#include<QKeyEvent>
#include<QTimer>
#include<QPainter>
#include<QMessageBox>
#include<QMediaPlayer>
#include<QMediaPlaylist>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
enum DRECT{
a_LEFT,a_RIGHT,a_DOWM,a_UP
};
class Widget : public QWidget
{
Q_OBJECT
void run() ;
public:
Widget(QWidget *parent = nullptr);
~Widget();
int foodx;
int foody;
protected:
void paintEvent(QPaintEvent *event) ;
void addTop();
void addDown();
void addRight();
void addLeft();
void deletelast();
void addnewreword();
void keyPressEvent(QKeyEvent *event) ;
bool check();
void M_startPush();
void M_exitPush();
void begingame();
void endgame();
void initStatus(QMediaPlayer::MediaStatus status);
QPushButton *startPush;
QPushButton *exitPush;
QMediaPlayer *player;
QMediaPlaylist* playerList;
private:
Ui::Widget *ui;
int movedrect=a_UP;
bool gamestart;
bool gameover;
//定时器
QTimer *timer;
int time=150;
//蛇
QList<QRectF>snake;
//小方块大小
QRectF rewardnode;
int nodeWidth=40;
int nodeHeight=40;
protected slots:
void timeout();
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//使用定时器
timer=new QTimer();
//信号槽
resize(1200,700);
QRectF rect(30,200,nodeWidth,nodeHeight);
snake.append(rect);
addTop();
addTop();
addnewreword();
update();
connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
startPush=new QPushButton(this);
exitPush=new QPushButton(this);
startPush->setIconSize(QSize(920,45));
startPush->setGeometry(500,360,150,65);
startPush->setIcon(QIcon("C:\\Users\\MALING\\Pictures\\Saved Pictures\\玩.jpg"));
startPush->setFlat(1);
exitPush->setIconSize(QSize(920,45));
exitPush->setGeometry(500,420,150,65);
exitPush->setIcon(QIcon("C:\\Users\\MALING\\Pictures\\Saved Pictures\\exit.jpg"));
exitPush->setFlat(1);//设置按钮的透明属性
this->setWindowTitle("派大星吃蟹黄堡");
connect(startPush,&QPushButton::clicked,this,&Widget::begingame);
connect(exitPush,&QPushButton::clicked,this,&Widget::endgame);
playerList = new QMediaPlaylist();
player = new QMediaPlayer();
playerList->addMedia(QMediaContent(QUrl("qrc:/C:/Users/MALING/Downloads/hmbbb.mp3")));
playerList->setPlaybackMode(QMediaPlaylist::Loop);
player->setPlaylist(playerList);
player->setVolume(100);
player->play();
}
void Widget::M_startPush()
{
startPush=new QPushButton(this);
startPush->setIconSize(QSize(920,45));
startPush->setGeometry(500,360,150,65);
startPush->setIcon(QIcon("C:\\Users\\MALING\\Pictures\\Saved Pictures\\玩.jpg"));
connect(startPush,&QPushButton::clicked,this,&Widget::begingame);
}
void Widget::begingame()
{
gamestart=true;
addnewreword();
update();
timer->start(time);
if(startPush!=nullptr){
delete startPush;
delete exitPush;
startPush=nullptr;
exitPush=nullptr;
}
}
void Widget::endgame()
{
delete this;
exit(0);
}
Widget::~Widget()
{
delete ui;
}
void Widget::keyPressEvent(QKeyEvent *event){
switch(event->key()){
case Qt::Key_Up:
if(movedrect!=a_DOWM){
movedrect=a_UP;
}
break;
case Qt::Key_Down:
if(movedrect!=a_DOWM){
movedrect=a_DOWM;
}
break;
case Qt::Key_Right:
if(movedrect!=a_LEFT){
movedrect=a_RIGHT;
}
break;
case Qt::Key_Left:
if(movedrect!=a_RIGHT){
movedrect=a_LEFT;
}
break;
case Qt::Key_Space:
if(gamestart == false)
{
gamestart = true;//如果游戏暂停,则开始
timer->start(time);//超时时间间隔,刷新
}
else
{
gamestart = false;//如果游戏已经开始,则暂停
timer->stop();//游戏暂停,移到打印字体的语句中
}
break;
case Qt::Key_Escape:
timer->stop();
QMessageBox::StandardButton a=QMessageBox::question(this,"game ostop",QString::fromLocal8Bit("GAME STOP,do you really want to escape?"));
if(a==QMessageBox::Yes){
exit(0);}
else{
gamestart=false;
}
}
}
void Widget::timeout(){
//判断重合
int count=1;
if(snake[0].intersects(rewardnode)){
count++;
addnewreword();
}
while(count--){
switch(movedrect){
case a_UP:
addTop();
break;
case a_DOWM:
addDown();
break;
case a_LEFT:
addLeft();
break;
case a_RIGHT:
addRight();
break;
default:
break;
}
}
deletelast();
update();
}
void Widget::addTop(){
QPointF leftTop;
QPointF rightBotom;
if(snake[0].y()-nodeHeight<0)
{
leftTop=QPointF(snake[0].x(),this->height()-nodeHeight);
rightBotom=QPointF(snake[0].x()+nodeWidth,this->height());
}else{
leftTop=QPointF(snake[0].x(),snake[0].y()-nodeHeight);
rightBotom=snake[0].topRight();
}
snake.insert(0,QRectF(leftTop,rightBotom));
}
void Widget::paintEvent(QPaintEvent *event){
QPainter painter(this);
QPen pen;
QBrush brush;
//背景图片
QPixmap pix;
pix.load("C:\\Users\\MALING\\Pictures\\Saved Pictures\\013e1d57d575ed0000018c1b96c745.jpg@1280w_1l_2o_100sh.jpg");
painter.drawPixmap(0,0,1200,700,pix);
//蛇
QPixmap S;
S.load("C:\\Users\\MALING\\Pictures\\Saved Pictures\\派大星白版.png");
painter.drawPixmap(snake[0].x(),snake[0].y(),60,60,S);
for(int i=0;i<snake.length();i++){
painter.drawPixmap(snake[i].x(),snake[i].y(),60,60,S);
}
//奖励
QPixmap bale;
bale.load("C:\\Users\\MALING\\Pictures\\Saved Pictures\\retouch_2023092021045324.png");
painter.drawPixmap(foodx,foody,50,50,bale);
//其他
painter.setPen(pen);
painter.setFont(font());
if(gamestart==true){
painter.setFont(QFont("方正舒体",10));
painter.drawText(40,40,QString("当前得分:")+QString("%10").arg(snake.length()-3));}
painter.drawText(40,80,QString("按空格暂停,按ESC退出"));
if(check()){
QFont font("方正舒体",60,QFont::ExtraLight,false);
painter.setFont(font);
painter.drawText((this->width()-300)/2,
(this->height()-30)/2,
QString("游戏结束!"));
timer->stop();
QMessageBox::StandardButton a=QMessageBox::question(this,"game over",QString::fromLocal8Bit("GAME OVER,do you want to try again?"));
if(a==QMessageBox::No){
exit(0);
}
else{
gameover = false;
snake.clear();
snake.append(QRectF(QPoint(100,100),QPoint(150,150)));
snake.append(QRectF(QPoint(100,110),QPoint(150,170)));
snake.append(QRectF(QPoint(100,120),QPoint(150,180)));
addnewreword();
update();;
timer->start(time);
gamestart = false;
}
}
QWidget::paintEvent(event);
}
void Widget::deletelast(){
snake.removeLast();
}
void Widget::addDown(){
QPointF leftTop;
QPointF rightBotton;
if(snake[0].y()+nodeHeight*2>this->height()){
leftTop=QPointF(snake[0].x(),0);
rightBotton=QPointF(snake[0].x()+nodeWidth,nodeHeight);
}else {
leftTop=snake[0].bottomLeft();
rightBotton=snake[0].bottomRight()+ QPointF(0,nodeHeight);
}
snake.insert(0,QRectF(leftTop, rightBotton));
}
void Widget::addLeft(){
QPointF lefttop;
QPointF rightbotton;
if(snake[0].x()-nodeWidth<0){
lefttop=QPointF(this->width()-nodeWidth,snake[0].y());
}else {
lefttop=snake[0].topLeft()-QPointF(nodeWidth,0);
}
rightbotton=lefttop+QPointF(nodeWidth,nodeHeight);
snake.insert(0,QRectF(lefttop,rightbotton));
}
void Widget::addRight(){
QPointF lefttop;
QPointF rightbotton;
if(snake[0].x()+nodeWidth*2>this->width())
{
lefttop=QPointF(0,snake[0].y());
}else{
lefttop=snake[0].topRight();
}
rightbotton=lefttop+QPointF(nodeWidth,nodeWidth);
snake.insert(0,QRectF(lefttop,rightbotton));
}
void Widget::addnewreword(){
foodx=(rand()%20)*30;
foody=(rand()%15)*30;
if(foodx!=snake[0].x(),foody!=snake[0].y()){
rewardnode=QRectF(
foodx,foody,
nodeWidth,
nodeHeight);}
}//蛇身判断
bool Widget::check()
{
for(int i=1;i<snake.length();i++)
{
if(snake[0]==snake[i]){
return true;
}
}
return false;
}