Qt实战项目——贪吃蛇

时间:2025-03-09 08:23:01
  • GameSelect::GameSelect(QWidget *parent) : QWidget(parent)
  • {
  • // 设置“难度选择”界面的图标、名字
  • this->setFixedSize(1080,720);
  • this->setWindowIcon(QIcon(":Resource/"));
  • this->setWindowTitle("难度选择");
  • QFont font("华文行楷",24);
  • GameRoom* gameRoom = new GameRoom;
  • // 设置选择难度按钮和历史战绩按钮
  • QPushButton* pushButton_easy = new QPushButton(this);
  • pushButton_easy->move(460,160);
  • pushButton_easy->setGeometry(460,160,150,80);
  • pushButton_easy->setText("简单模式");
  • pushButton_easy->setFont(font);
  • pushButton_easy->setStyleSheet("QPushButton{border:0px;color:white}");
  • QPushButton* pushButton_normal = new QPushButton(this);
  • pushButton_normal->move(460,280);
  • pushButton_normal->setGeometry(460,280,150,80);
  • pushButton_normal->setText("正常模式");
  • pushButton_normal->setFont(font);
  • pushButton_normal->setStyleSheet("QPushButton{border:0px;color:white}");
  • QPushButton* pushButton_hard = new QPushButton(this);
  • pushButton_hard->move(460,400);
  • pushButton_hard->setGeometry(460,400,150,80);
  • pushButton_hard->setText("困难模式");
  • pushButton_hard->setFont(font);
  • pushButton_hard->setStyleSheet("QPushButton{border:0px;color:white}");
  • QPushButton* pushButton_record = new QPushButton(this);
  • pushButton_record->move(460,520);
  • pushButton_record->setGeometry(460,520,150,80);
  • pushButton_record->setText("历史战绩");
  • pushButton_record->setFont(font);
  • pushButton_record->setStyleSheet("QPushButton{border:0px;color:white}");
  • // 点击不同困难模式按钮进入游戏房间
  • connect(pushButton_easy,&QPushButton::clicked,[=](){
  • this->close();
  • gameRoom->setGeometry(this->geometry());
  • gameRoom->show();
  • QSound::play(":Resource/");
  • gameRoom->setTimeout(300);
  • });
  • connect(pushButton_normal,&QPushButton::clicked,[=](){
  • this->close();
  • gameRoom->setGeometry(this->geometry());
  • gameRoom->show();
  • QSound::play(":Resource/");
  • gameRoom->setTimeout(200);
  • });
  • connect(pushButton_hard,&QPushButton::clicked,[=](){
  • this->close();
  • gameRoom->setGeometry(this->geometry());
  • gameRoom->show();
  • QSound::play(":Resource/");
  • gameRoom->setTimeout(100);
  • });
  • // 设置历史战绩窗口
  • connect(pushButton_record,&QPushButton::clicked,[=](){
  • QWidget* widget = new QWidget;
  • widget->setWindowTitle("历史战绩");
  • widget->setWindowIcon(QIcon(":Resource/"));
  • widget->setFixedSize(500,300);
  • QSound::play(":Resource/");
  • QTextEdit* edit = new QTextEdit(widget);
  • edit->setFont(font);
  • edit->setFixedSize(500,300);
  • QFile file("D:/bite/C-program/project/Snake/");
  • file.open(QIODevice::ReadOnly);
  • QTextStream in(&file);
  • int data = in.readLine().toInt();
  • edit->append("历史得分为:");
  • edit->append(QString::number(data));
  • widget->show();
  • });
  • }
  • void GameSelect::paintEvent(QPaintEvent *event)
  • {
  • (void) *event;
  • QPainter painter(this);
  • QPixmap pix(":Resource/game_select.png");
  • painter.drawPixmap(0,0,this->width(),this->height(),pix);
  • }