#include "mainwidget.h"
#include "ui_mainwidget.h"
Mainwidget::Mainwidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Mainwidget)
{
ui->setupUi(this);
//窗口设置
this->setWindowFlag(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
this->setStyleSheet(
"background-color: white;"
);
this->setFixedSize(700,500);//固定窗口大小
QLabel *lbl=new QLabel(this);
lbl->move(490,34);
lbl->resize(150,150);
lbl->setStyleSheet(
"QLabel{"
"border-image:url(:/image/dabai.png)"
"}"
);
openDB();
//初始化按钮
botton();
connect(closebtn,&QPushButton::clicked,this,&Mainwidget::close);//关闭
connect(minbtn,&QPushButton::clicked,this,&Mainwidget::showMinimized);//最小化
connect(startbtn,&QPushButton::clicked,this,&Mainwidget::selectmode_change);
connect(storebtn,&QPushButton::clicked,this,&Mainwidget::store_change);
connect(gametop,&QPushButton::clicked,this,&Mainwidget::rankinglist_change);
connect(gametip,&QPushButton::clicked,this,&Mainwidget::gamehelp_change);
connect(musicstartbtn,&QPushButton::clicked,this,&Mainwidget::musicstart);//关音乐
connect(musicstopbtn,&QPushButton::clicked,this,&Mainwidget::musicstop);
}
Mainwidget::~Mainwidget()
{
delete ui;
}
//绘制背景
void Mainwidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); // 设置抗锯齿,不然边框会有明显锯齿
painter.setBrush(Qt::white); // 设置窗体颜色
QRect rect = this->rect();
painter.drawRoundedRect(rect,20,20);
QPixmap pix;//QPixmap用于处理位图图像 用于在屏幕上显示图像
pix.load(":/image/jiemian.png");//加载图像
painter.drawPixmap(0,0,700,500,pix);//画家绘图
}
//按钮
void Mainwidget::botton()
{
widget=new QWidget(this);
QVBoxLayout *vlayut=new QVBoxLayout(widget);
widget->move(30,215);
widget->resize(220,240);
vlayut->setSpacing(15);
vlayut->setContentsMargins(0, 0, 0, 0);
//开始游戏
startbtn = new QPushButton("开始游戏", widget);
startbtn->setFixedSize(220, 45);
startbtn->setStyleSheet(
"QPushButton {"
" background-color: white;"
" border: 2px solid black;"
" border-radius: 4px;"
" padding: 10px;"
" font-size: 20px;"
"}"
);
vlayut->addWidget(startbtn); // 将登录按钮添加到布局中
//商店
storebtn = new QPushButton("游戏商城", widget);
storebtn->setFixedSize(220, 45);
storebtn->setStyleSheet(
"QPushButton {"
" background-color: white;"
" border: 2px solid black;"
" border-radius: 4px;"
" padding: 10px;"
" font-size: 20px;"
"}"
);
vlayut->addWidget(storebtn);
//排行榜
gametop = new QPushButton("排行榜", widget);
gametop->setFixedSize(220, 45);
gametop->setStyleSheet(
"QPushButton {"
" background-color: white;"
" border: 2px solid black;"
" border-radius: 4px;"
" padding: 10px;"
" font-size: 20px;"
"}"
);
vlayut->addWidget(gametop);
//游戏帮助
gametip = new QPushButton("游戏帮助", widget);
gametip->setFixedSize(220, 45);
gametip->setStyleSheet(
"QPushButton {"
" background-color: white;"
" border: 2px solid black;"
" border-radius: 4px;"
" padding: 10px;"
" font-size: 20px;"
"}"
);
vlayut->addWidget(gametip);
//关闭
closebtn=new QPushButton(this);
closebtn->move(650, 25);//调整按钮在窗口上的位置
closebtn->setText("");
closebtn->resize(25,25);//设置按钮大小
// 设置样式表使按钮透明
closebtn->setStyleSheet(
"QPushButton {"
" background-color: transparent;"// 背景透明
" border: none;"// 边框透明
"}"
);
// 加载图片
QIcon originalIcon2(":/image/close.png");
QPixmap pixmap2 = originalIcon2.pixmap(QSize(25, 25));
QIcon icon2(pixmap2);
closebtn->setIcon(icon2);
closebtn->setIconSize(QSize(25, 25));
//最小化
minbtn=new QPushButton(this);
minbtn->move(610, 15);
minbtn->setText("");
minbtn->resize(30,30);
minbtn->setStyleSheet(
"QPushButton {"
" background-color: transparent;"
" border: none;"
"}"
);
QIcon originalIcon3(":/image/hide.png");
QPixmap pixmap3 = originalIcon3.pixmap(QSize(30, 30));
QIcon icon3(pixmap3);
minbtn->setIcon(icon3);
minbtn->setIconSize(QSize(30, 30));
//开启音乐图标
musicstartbtn = new QPushButton(this);
musicstartbtn->move(600, 60);
musicstartbtn->setText("");
musicstartbtn->resize(40,35);
// 设置样式表使按钮透明
musicstartbtn->setStyleSheet(
"QPushButton {"
" background-color: transparent;" // 背景透明
" border: none;" // 边框透明
"}"
);
// 加载图片
QIcon originalIcon(":/image/musicstart.png");
QPixmap pixmap = originalIcon.pixmap(QSize(30, 30)); // 如果需要保持原始图片的纵横比,则可能不需要scaled方法
// 如果原图不是正方形或者你想要强制缩放,使用下面的代码
// pixmap = originalIcon.pixmap(QSize(35, 35)).scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation);
// 使用调整大小后的QPixmap创建QIcon(如果使用了scaled方法)
QIcon icon(pixmap);
musicstartbtn->setIcon(icon); // 设置按钮的图标
musicstartbtn->setIconSize(QSize(30, 30)); // 设置图标显示的大小
//关闭音乐图标
musicstopbtn = new QPushButton(this);
musicstopbtn->setVisible(false);//设置关闭音乐为不可见
musicstopbtn->move(600, 60);
musicstopbtn->setText("");
musicstopbtn->resize(40,30);
// 设置样式表使按钮透明
musicstopbtn->setStyleSheet(
"QPushButton {"
" background-color: transparent;" // 背景透明
" border: none;" // 边框透明(如果需要的话)
"}"
);
// 加载图片
QIcon originalIcon1(":/image/musicstop1.png");
QPixmap pixmap1 = originalIcon1.pixmap(QSize(35, 35)); // 如果需要保持原始图片的纵横比,则可能不需要scaled方法
QIcon icon1(pixmap1);
musicstopbtn->setIcon(icon1); // 设置按钮的图标
musicstopbtn->setIconSize(QSize(30, 30)); // 设置图标显示的大小
}
//切换模式选择
void Mainwidget::selectmode_change()
{
this->close();
Selectmode *s=new Selectmode();
s->show();
}
//切换商店
void Mainwidget::store_change()
{
this->close();
Store *s=new Store();
s->show();
}
//切换排行榜
void Mainwidget::rankinglist_change()
{
this->close();
Rankinglist *r=new Rankinglist();
r->show();
}
//切换游戏帮助
void Mainwidget::gamehelp_change()
{
this->close();
Gamehelp *g=new Gamehelp();
g->show();
}
//音乐开关
void Mainwidget::musicstart()
{
musicstartbtn->setVisible(false);
musicstopbtn->setVisible(true);
}
void Mainwidget::musicstop()
{
musicstopbtn->setVisible(false);
musicstartbtn->setVisible(true);
}
//打开数据库
bool Mainwidget::openDB()
{
DB=QSqlDatabase::addDatabase("QODBC");
DB.setHostName("localhost");
DB.setPort(3306);
DB.setUserName("root");
DB.setPassword("832697.z");
DB.setDatabaseName("snake");
if(!DB.open())
{
qDebug()<<"主界面数据库打开失败!";
return false;
}
else
{
qDebug()<<"主界面数据库打开成功!";
}
//匹配头像和昵称
tx=new QLabel(this);
tx->move(50, 34);
tx->resize(50, 50);
//tx->show(); // 确保QLabel是可见的
QPixmap pixmap;
QLabel *nc=new QLabel(this);
nc->move(120,34);
nc->resize(40,40);
QFont font("微软雅黑", 16); // 创建一个14号大小的字体
nc->setFont(font);
//QSqlQuery q("select id,img,nc from player", DB);
QSqlQuery q(DB); // 直接将DB作为参数传递给QSqlQuery的构造函数
q.prepare("SELECT id,img,name FROM player WHERE id = :userid");
q.bindValue(":userid", zh);
if (q.exec())// 检查查询是否成功执行(对于prepare的查询,使用exec()才是正确的)
{
while (q.next())
{
qDebug() << "在循环查找";
QString name=q.value(2).toString();
nc->setText(name);
QVariant imgVariant = q.value(1);
if (!imgVariant.isNull())// 检查img列是否为NULL
{
QByteArray blob = imgVariant.toByteArray();
if (!blob.isEmpty())
{
pixmap.loadFromData(blob);
qDebug() << "有头像呢";
}
}
else
{
// img列为NULL 或 blob为空但不为NULL,加载默认头像
pixmap.load(":/image/1.png");
qDebug() << "没有头像呢";
}
tx->setPixmap(pixmap.scaled(tx->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
//tx->setPixmap(pixmap.scaledToWidth(tx->size().width()));
break; // 找到一个匹配的头像就退出循环
}
}
else
qDebug() << "查询失败:" << q.lastError().text();// 处理查询错误
return true;
}
//键盘事件
void Mainwidget::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape)
{
// 显示设置界面
Msetting *m = new Msetting();
m->show();
}
}
void Mainwidget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton) // 左键按下
{
m_bMove = true; // 移动标志位
reltvPos = event->pos(); // 按下瞬间获取相对窗口坐标
}
return QWidget::mousePressEvent(event);
}
void Mainwidget::mouseMoveEvent(QMouseEvent *event)
{
if(m_bMove) move(event->globalPos() - reltvPos);
return QWidget::mouseMoveEvent(event);
}
void Mainwidget::mouseReleaseEvent(QMouseEvent *event)
{
m_bMove = false; // 松开后要置为false
return QWidget::mouseReleaseEvent(event);
}
#include "hardmode.h"
#include "ui_hardmode.h"
#include"gameend.h"
#include <QMediaPlayer>
#include <QUrl>
#include<QAudioOutput>
/*优化:
1.在返回时添加一个确认窗口--QMessageBox提问对话框?
2.统计金币并比较记录到库
3.生命值变灰效果
4.撞击时的眩晕效果
5.游戏音效,失败音效,成功音效
6.音效设置--音量大小、开关
*.皮肤效果
8.调整🐍的移动速度,降低难度
按esc可以调处设置界面,省去复杂的按钮设置
*/
Hardmode::Hardmode(QWidget *parent) :
QWidget(parent),
ui(new Ui::Hardmode)
{
ui->setupUi(this);
//窗口设置
this->setFixedSize(600,400);//固定窗口大小
this->setWindowFlag(Qt::FramelessWindowHint);//隐藏当前窗口栏
setAttribute(Qt::WA_TranslucentBackground); //窗口半透明
//多媒体部分
audioOutput1 = new QAudioOutput(this);//output输出对象
player1 = new QMediaPlayer(this);//媒体播放对象
player1->setAudioOutput(audioOutput1);
// 设置音乐文件的 URL
QUrl source1(QUrl::fromLocalFile("C:\\Users\\Lenovo\\Documents\\Tencent Files\\2451479163\\FileRecv\\SNAKE(1)\\music\\fail2.mp3"));
player1->setSource(source1);
audioOutput1->setVolume(0.6);//音量为60
audioOutput2 = new QAudioOutput(this);//output输出对象
player2 = new QMediaPlayer(this);//媒体播放对象
player2->setAudioOutput(audioOutput2);
// 设置音乐文件的 URL
QUrl source2(QUrl::fromLocalFile("C:\\Users\\Lenovo\\Documents\\Tencent Files\\2451479163\\FileRecv\\SNAKE(1)\\music\\1.wav"));
player2->setSource(source2);
audioOutput2->setVolume(0.6);//音量为60
//得分情况
QLabel *l2=new QLabel(this);
l2->move(520,20);
l2->resize(40,40);
l2->setStyleSheet(
"QLabel{"
"background-color: transparent;"
"border-image: url(:/image/tuzi_shen.png);"
"}"
);
// 创建一个QFont对象并设置字体大小
QFont font = l2->font(); // 从QLabel获取当前字体作为起点(尽管在这个例子中它是默认的)
font.setPointSize(12); // 设置字体大小为12点
grade=new QLabel(this);
grade->setText("0");
grade->move(570,31);
grade->setStyleSheet(
"QLabel{"
"background-color: transparent;"
"}"
);
grade->setFont(font);
button();
//定时器
time=new QTimer();
//定时器超时就调用
connect(time,&QTimer::timeout,this,&Hardmode::timeout);
initgame();
setFocusPolicy(Qt::StrongFocus);
// connect(returnbtn,&QPushButton::clicked,this,&Simplemode::returnButton_clicked);
}
Hardmode::~Hardmode()
{
delete ui;
}
//按钮
void Hardmode::button()
{
//音乐按钮
//暂停按钮
//退出按钮
/* returnbtn=new QPushButton(this);
returnbtn->move(550, 10);
returnbtn->setText("");
returnbtn->resize(20,20);
returnbtn->setStyleSheet(
"QPushButton {"
" background-color: transparent;"
" border: none;"
"}"
);
QIcon originalIcon(":/image/fanhui-left.png");
QPixmap pixmap = originalIcon.pixmap(QSize(20, 20)); // 如果需要保持原始图片的纵横比,则可能不需要scaled方法
QIcon icon(pixmap);
returnbtn->setIcon(icon); // 设置按钮的图标
returnbtn->setIconSize(QSize(20, 20)); // 设置图标显示的大小
*/
}
void Hardmode::initgame()
{
//🐍初始化
QRectF rect(300,180,nodewidth,nodeheight);//位置、宽高
snake.append(rect);
addUp();
addUp();
//奖品初始化
addNewreward();
// 初始化三个树
for (int i = 0; i < 3; ++i) {
inittree();
}
}
//绘制
void Hardmode::paintEvent(QPaintEvent *event)
{
QPainter painter(this);//画家
QPen pen;//画笔
QBrush brush;//画刷
//设置圆角
painter.setRenderHint(QPainter::Antialiasing); //设置抗锯齿,不然边框会有明显锯齿
painter.setBrush(Qt::white);//设置刷子的颜色--内部填充色(刷子通常用于填充形状,如矩形、圆形等)
QRect rect = this->rect();//获取当前QWidget的矩形区域(即其大小和位置),并将其存储在rect变量中
painter.drawRoundedRect(rect,30,30);//绘制一个圆角矩形
// 绘制背景图片
QPixmap background;
background.load(":/image/xia.jpg");
painter.drawPixmap(0, 0, this->width(), this->height(), background);
//🐍
/*QColor Color(141, 192, 97);
//pen.setColor(Qt::gray);//设置颜色
pen.setColor(Color);//身体边框
Color.setRgb(123, 243, 9);
brush.setColor(Color);//身体
brush.setStyle(Qt::SolidPattern);//实心填充
//把画笔和画刷给画家
painter.setPen(pen);
painter.setBrush(brush);
for(int i=0; i<snake.length();i++)
{
painter.drawRect(snake[i]);
}*/
QPixmap snakeBodyPartPixmap(":/image/tuzi_shen.png");//QPixmap对象保存蛇身的图片
if (snakeBodyPartPixmap.size().width() > nodewidth || snakeBodyPartPixmap.size().height() > nodeheight)
{
snakeBodyPartPixmap = snakeBodyPartPixmap.scaled(nodewidth, nodeheight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
for(int i = 0; i < snake.length(); i++)
{
// snake[i]是一个QRectF对象,表示蛇身部分的位置和大小,下面计算偏移量以将图片居中在QRectF中
QRect destRect = snake[i].toRect();
QSize pixmapSize = snakeBodyPartPixmap.size();
int offsetX = (destRect.width() - pixmapSize.width()) / 2;
int offsetY = (destRect.height() - pixmapSize.height()) / 2;
QPoint offset(offsetX, offsetY);
// 绘制图片到snake[i]定义的位置,并应用偏移量以居中
painter.drawPixmap(snake[i].topLeft() + offset, snakeBodyPartPixmap);
}
//画奖品
/*QColor Color;
Color.setRgb(226, 252, 201);
pen.setColor(Color);//设置颜色
brush.setColor(Color);
brush.setStyle(Qt::SolidPattern);//实心填充
painter.setPen(pen);
painter.setBrush(brush);
//painter.drawEllipse(reward);//圆形
painter.drawRect(reward);//方形*/
QPixmap prizePixmap(":/image/xfood.png");
if (prizePixmap.size().width() > nodewidth || prizePixmap.size().height() > nodeheight)
{
prizePixmap = prizePixmap.scaled(nodewidth, nodeheight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
painter.drawPixmap(reward.topLeft(), prizePixmap);
//障碍物
QPixmap prizePixmap1(":/image/tree.png");
if (prizePixmap1.size().width() > nodewidth || prizePixmap1.size().height() > nodeheight)
{
prizePixmap1 = prizePixmap1.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
// 绘制树
for (const auto &tree : trees) {
painter.drawPixmap(tree.topLeft(), prizePixmap1);
}
//游戏结束提示
if(checkContact())
{
// 开始播放音乐
player1->play();
time->stop();
//间隔1s
this->close();
Gameend *c=new Gameend();
c->show();
}
QWidget::paintEvent(event);
}
//键盘事件
void Hardmode::keyPressEvent(QKeyEvent *event)
{
switch(event->key())
{
case Qt::Key_W:
{
if(moveFlag!=MyNamespace1::DOWN)//在向下走的时候不能往上走(不回头)
moveFlag=MyNamespace1::UP;
qDebug()<<"往上!";
break;
}
case Qt::Key_S:
{
if(moveFlag!=MyNamespace1::UP)
moveFlag=MyNamespace1::DOWN;
qDebug()<<"往下!";
break;
}
case Qt::Key_D:
{
if(moveFlag!=MyNamespace1::LEFT)
moveFlag=MyNamespace1::RIGHT;
qDebug()<<"往右!";
break;
}
case Qt::Key_A:
{
if(moveFlag!=MyNamespace1::RIGHT)
moveFlag=MyNamespace1::LEFT;
qDebug()<<"往左!";
break;
}
case Qt::Key_Space:
{
if(gamestart==false)
{
gamestart=true;
time->start(95);//启动定时器(100ms超时--检查一次)
qDebug()<<"启动!";
}
else
{
gamestart=false;
time->stop();//关闭定时器
qDebug()<<"结束!";
}
break;
}
defalut:break;
}
if (event->key() == Qt::Key_Escape)
{
// 显示设置界面
Setting *s = new Setting(this);
s->show();
gamestart=false;
time->stop();//关闭定时器
qDebug()<<"结束!";
}
//else
//{
// 处理其他按键事件
// QMainWindow::keyPressEvent(event);
//}
}
//吃掉
void Hardmode::timeout()
{
int count=1;
//判断重合
if(snake[0].intersects(reward))
{
player2->play();
count++;
score++;
qDebug()<<score;
grade->setText(QString::number(score));
addNewreward();
}
while(count--)
{
switch(moveFlag)
{
case MyNamespace1::UP:
addUp();//在顶部加一个小矩形
break;
case MyNamespace1::DOWN:
addDown();
break;
case MyNamespace1::RIGHT:
addRight();
break;
case MyNamespace1::LEFT:
addLeft();
break;
default:break;
}
}
deleteLast();
update();//会自动调用paintEvent进行重绘
}
void Hardmode::addUp()//往上增加一块
{
QPointF lefttop;//左上角坐标
QPointF rightdown;//右下角坐标
lefttop=QPointF(snake[0].x(),snake[0].y()-nodeheight);
rightdown=snake[0].topRight();//即原第一个方块右上角坐标
snake.insert(0,QRectF(lefttop,rightdown));//QRectF是靠左上角和右下角坐标确定位置
}
void Hardmode::addDown()
{
QPointF lefttop;//左上角坐标
QPointF rightdown;//右下角坐标
lefttop=snake[0].bottomLeft();
rightdown=snake[0].bottomRight()+QPointF(0,nodeheight);
snake.insert(0,QRectF(lefttop,rightdown));
}
void Hardmode::addLeft()
{
QPointF lefttop;//左上角坐标
QPointF rightdown;//右下角坐标
lefttop=snake[0].topLeft()-QPointF(nodewidth,0);
rightdown=lefttop+QPointF(nodewidth,nodeheight);
snake.insert(0,QRectF(lefttop,rightdown));
}
void Hardmode::addRight()
{
QPointF lefttop;//左上角坐标
QPointF rightdown;//右下角坐标
lefttop=snake[0].topRight();
rightdown=lefttop+QPoint(nodewidth,nodeheight);
snake.insert(0,QRectF(lefttop,rightdown));
}
void Hardmode::deleteLast()//删掉最后的矩形
{
snake.removeLast();
}
//增加奖品
void Hardmode::addNewreward()
{
int x = rand() % (this->width() / 20) * 20; // 生成随机的x坐标
int y = rand() % (this->height() / 20) * 20; // 生成随机的y坐标
reward = QRectF(x, y, 25, 25); // 设置reward的位置和大小
}
void Hardmode::inittree()
{
int x = rand() % (this->width() - 50) / 30 * 30; // 确保树不会超出窗口边界
int y = rand() % (this->height() - 50) / 30 * 30;
QRectF treeRect(x, y, 50, 50);
trees.append(treeRect); // 将树的位置添加到列表中
}
bool Hardmode::checkContact()
{
//判定1:身体有接触就失败
for(int i=0;i<snake.length();i++)
{
for(int j=i+1;j<snake.length();j++)
{
if(snake[0]==snake[j])
return true;
}
}
//判定2:头部撞到身体就失败
for(int j=1;j<snake.length();j++)
{
if(snake[0]==snake[j])
return true;
}
// 判定3: 头部撞到边界就失败
// 检查是否撞墙
if (snake[0].x() < 0 || snake[0].y() < 0 || snake[0].x() >= width() || snake[0].y() >= height())
return true;
//判断重合
for (const auto &tree : trees)
{
if(snake[0].intersects(tree))
{
return true;
}
}
return false;
}
//返回
void Hardmode::returnButton_clicked()
{
this->close();
Selectmode *s=new Selectmode();
s->show();
}
void Hardmode::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton) // 左键按下
{
m_bMove = true; // 移动标志位
reltvPos = event->pos(); // 按下瞬间获取相对窗口坐标
}
return QWidget::mousePressEvent(event);
}
void Hardmode::mouseMoveEvent(QMouseEvent *event)
{
if(m_bMove) move(event->globalPos() - reltvPos);
return QWidget::mouseMoveEvent(event);
}
void Hardmode::mouseReleaseEvent(QMouseEvent *event)
{
m_bMove = false; // 松开后要置为false
return QWidget::mouseReleaseEvent(event);
}
#include "setting.h"
#include "ui_setting.h"
#include"hardmode.h"
#include <QApplication>
#include"simplemode.h"
#include"selectmode.h"
#include"mainwidget.h"
Setting::Setting(QWidget *parent) :
QWidget(parent),
ui(new Ui::Setting)
{
ui->setupUi(this);
//隐藏当前窗口栏
this->setWindowFlag(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
this->setFixedSize(200, 250);
this->setStyleSheet("background-color: white;");
button();
//返回游戏
connect(returnbtn,&QPushButton::clicked,this,&Setting::close);
//退出游戏
connect(rejiembtn,&QPushButton::clicked,this,&Setting::closee);
//重新游戏
connect(regamebtn,&QPushButton::clicked,this,&Setting::regame);
}
Setting::~Setting()
{
delete ui;
}
void Setting::button()
{
regamebtn = new QPushButton(" 返回主界面", this);
regamebtn->setStyleSheet(
"QPushButton {"
" background-color: white;"
" border: 3px solid black;"
" padding: 10px;"
" font-size: 18px;"
"border-radius: 9px"
"}"
);
regamebtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); // 使得按钮在水平方向上可扩展
returnbtn = new QPushButton(" 返回游戏", this);
returnbtn->setStyleSheet(
"QPushButton {"
" background-color: white;"
" border: 3px solid black;"
" padding: 10px;"
" font-size: 18px;"
"border-radius: 9px"
"}");
returnbtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); // 使得按钮在水平方向上可扩展
rejiembtn = new QPushButton(" 退出游戏", this);
rejiembtn->setStyleSheet(
"QPushButton {"
" background-color: white;"
" border: 3px solid black;"
" padding: 10px;"
" font-size: 18px;"
"border-radius: 9px"
"}");
rejiembtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); // 使得按钮在水平方向上可扩展
// 创建垂直布局并设置到主窗口
// 创建垂直布局并设置到主窗口
QWidget *widget=new QWidget(this);
vlayut = new QVBoxLayout(widget); // 直接设置到this,即主QWidget
widget->move(32,30);
vlayut->setSpacing(15);
vlayut->addWidget(regamebtn);
vlayut->addWidget(returnbtn);
vlayut->addWidget(rejiembtn);
//关闭
/* closebtn=new QPushButton(this);
closebtn->move(160, 10);
closebtn->setText("");
closebtn->resize(20,20);
// 设置样式表使按钮透明
closebtn->setStyleSheet(
"QPushButton {"
" background-color: transparent;" // 背景透明
" border: none;" // 边框透明(如果需要的话)
"}"
);
// 加载图片
QIcon originalIcon2(":/image/close.png");
QPixmap pixmap2 = originalIcon2.pixmap(QSize(20, 20)); // 如果需要保持原始图片的纵横比,则可能不需要scaled方法
QIcon icon2(pixmap2);
closebtn->setIcon(icon2); // 设置按钮的图标
closebtn->setIconSize(QSize(20, 20)); // 设置图标显示的大小*/
}
void Setting::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); // 设置抗锯齿,不然边框会有明显锯齿
painter.setBrush(Qt::white); // 设置窗体颜色
QRect rect = this->rect();
// 绘制方式1
painter.drawRoundedRect(rect,50,50);
// 设置边框颜色、宽度和样式
QPen pen(Qt::black, borderWidth); // 边框颜色和宽度
pen.setJoinStyle(Qt::RoundJoin); // 设置连接处为圆角
painter.setPen(pen);
// 设置圆角半径,这里假设四个角的半径都相同
int cornerRadius = 50; // 可以根据需要调整
// 绘制圆角矩形边框
painter.drawRoundedRect(this->rect().adjusted(borderWidth / 2, borderWidth / 2, -borderWidth / 2, -borderWidth / 2), cornerRadius, cornerRadius);
}
void Setting::closee()
{
QApplication::quit();
}
void Setting::regame()
{
Mainwidget *m=new Mainwidget();
m->show();
}