Qt--2048小游戏

文章目录

  • 主要内容
  • 一.2048小游戏
      • 1.功能
      • 2.源程序
          • 代码如下(示例):
      • 5.结果
  • 总结

主要内容

  1. 2048

一.2048小游戏

1.功能

  1. 上下左右控制数字格子的移动 WASD
  2. 4*4 格子移动操作,加操作
  3. 开始游戏的按钮,重新游戏按钮
  4. 得分计算
  5. 判断游戏是否结束

2.源程序

代码如下(示例):

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QKeyEvent>
#include <QPushButton>
#include <QPainter>
#include <QTime>
#include <QDebug> //用来 Debug
#include <QMessageBox>//自定义消息提示框
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
void paintEvent(QPaintEvent *);
void keyPressEvent(QKeyEvent *event);
void PressUp();
void PressDown();
void PressLeft();
void PressRight();
void myRand();
QPushButton *button;
int s[4][4];
int score=0;
bool state;
struct Ns{
int i;
int j;
};
public slots:
void slotStart();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

Main.cpp

#include "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setFixedSize(320,480);
w.setWindowTitle("2048");
w.show();
return a.exec();
}

MainWindow.cpp

#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//初始化
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
s[i][j]=0;
}
}
button = new QPushButton("开始游戏",this);
button->setGeometry(60,400,200,50);//从(60,400)做一个 200*50 的按钮
//随机函数
qsrand(uint(QTime(0,0,0).secsTo(QTime::currentTime())));
connect(button,SIGNAL(clicked()),this,SLOT(slotStart()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.setBrush(Qt::blue);
p.setFont(QFont("微软雅黑",20,700,false));
QString strscore;
p.drawText(QPoint(20,60),"分数: "+QString::number(score));
for (int i=0;i<4;i++) {
for (int j=0;j<4;j++) {
p.setPen(Qt::transparent);//?
if(s[i][j]==0){
p.setBrush(Qt::gray);
p.drawRect(i*60+40,j*60+120,55,55);
}
else if (s[i][j]==2) {
p.setBrush(Qt::red);
p.drawRect(i*60+40,j*60+120,55,55);
p.setPen(Qt::black);
p.setFont(QFont("微软雅黑",10,700,false));
//在方块中 绘制 Text, 并且文本位置位于中部
p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(2),QTextOption(Qt::Al
ignCenter));
}
else if (s[i][j]==4) {
p.setBrush(Qt::darkRed);
p.drawRect(i*60+40,j*60+120,55,55);
p.setPen(Qt::black);
p.setFont(QFont("微软雅黑",10,700,false));
p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(4),QTextOption(Qt::Al
ignCenter));
}
else if (s[i][j]==8) {
p.setBrush(Qt::green);
p.drawRect(i*60+40,j*60+120,55,55);
p.setPen(Qt::black);
p.setFont(QFont("微软雅黑",10,700,false));
p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(8),QTextOption(Qt::Al
ignCenter));
}
else if (s[i][j]==16) {
p.setBrush(Qt::darkGreen);
p.drawRect(i*60+40,j*60+120,55,55);
p.setPen(Qt::black);
p.setFont(QFont("微软雅黑",10,700,false));
p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(16),QTextOption(Qt::A
lignCenter));
}
else if (s[i][j]==32) {
p.setBrush(Qt::yellow);
p.drawRect(i*60+40,j*60+120,55,55);
p.setPen(Qt::black);
p.setFont(QFont("微软雅黑",10,700,false));
p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(32),QTextOption(Qt::A
lignCenter));
}
else if (s[i][j]==64) {
p.setBrush(Qt::darkYellow);
p.drawRect(i*60+40,j*60+120,55,55);
p.setPen(Qt::black);
p.setFont(QFont("微软雅黑",10,700,false));
p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(64),QTextOption(Qt::A
lignCenter));
}
else if (s[i][j]==128) {
p.setBrush(Qt::cyan);
p.drawRect(i*60+40,j*60+120,55,55);
p.setPen(Qt::black);
p.setFont(QFont("微软雅黑",10,700,false));
p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(128),QTextOption(Qt::
AlignCenter));
}
else if (s[i][j]==256) {
p.setBrush(Qt::darkCyan);
p.drawRect(i*60+40,j*60+120,55,55);
p.setPen(Qt::black);
p.setFont(QFont("微软雅黑",10,700,false));
p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(256),QTextOption(Qt::
AlignCenter));
}
else if (s[i][j]==512) {
p.setBrush(Qt::magenta);
p.drawRect(i*60+40,j*60+120,55,55);
p.setPen(Qt::black);
p.setFont(QFont("微软雅黑",10,700,false));
p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(512),QTextOption(Qt::
AlignCenter));
}
else if (s[i][j]==1024) {
p.setBrush(Qt::darkMagenta);
p.drawRect(i*60+40,j*60+120,55,55);
p.setPen(Qt::black);
p.setFont(QFont("微软雅黑",10,700,false));
p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(1024),QTextOption(Qt::
AlignCenter));
}
else if (s[i][j]==2048) {
p.setBrush(Qt::blue);
p.drawRect(i*60+40,j*60+120,55,55);
p.setPen(Qt::black);
p.setFont(QFont("微软雅黑",10,700,false));
p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(2048),QTextOption(Qt::
AlignCenter));
}
else{
p.setBrush(Qt::darkBlue);
p.drawRect(i*60+40,j*60+120,55,55);
p.setPen(Qt::black);
p.setFont(QFont("微软雅黑",10,700,false));
p.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(s[i][j]),QTextOption(
Qt::AlignCenter));
}
}
}
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(!state){
return ;
}
switch (event->key()) {
case Qt::Key_W:
PressUp();
break;
case Qt::Key_S:
PressDown();
break;
case Qt::Key_A:
PressLeft();
break;
case Qt::Key_D:
PressRight();
break;
default:
return;//忽略其他按钮
}
myRand();
update();
}
void MainWindow::slotStart(){
QMessageBox::about(this,"游戏规则","WSAD 控制方块上下左右移动");
score=0;
for (int i=0;i<4;i++) {
for (int j=0;j<4;j++) {
s[i][j]=0;
}
}
button->setText("重新游戏");
int randi=qrand()%4;
int randj=qrand()%4;
s[randi][randj]=2;
state=true;
update();
}
void MainWindow::PressUp(){
//移动
for (int i=0;i<4;i++) {
for (int j=1;j<4;j++) {
if(s[i][j]==0)continue;
for (int p=0;p<j;p++) {
//查看前面是否有空格子可移动
if(s[i][p]==0){
s[i][p]=s[i][j];
s[i][j]=0;
break;
}
}
}
}
//相加
for (int i=0;i<4;i++) {
for (int j=0;j<3;j++) {
if(s[i][j]==s[i][j+1]){
s[i][j]=2*s[i][j];
s[i][j+1]=0;
score+=s[i][j];
for (int p=j+2;p<4;p++) {
s[i][p-1]=s[i][p];
}
}
}
}
}
void MainWindow::PressDown(){
//移动
for (int i=0;i<4;i++) {
for (int j=2;j>=0;j--) {
if(s[i][j]==0)continue;
for (int p=3;p>j;p--) {
//查看前面是否有空格子可移动
if(s[i][p]==0){
s[i][p]=s[i][j];
s[i][j]=0;
break;
}
}
}
}
//相加
for (int i=0;i<4;i++) {
for (int j=3;j>0;j--) {
if(s[i][j]==s[i][j-1]){
s[i][j]=2*s[i][j];
s[i][j-1]=0;
score+=s[i][j];
for (int p=j-2;p>=0;p--) {
s[i][p+1]=s[i][p];
}
}
}
}
}
void MainWindow::PressLeft(){
//移动
for (int j=0;j<4;j++) {
for (int i=1;i<4;i++) {
if(s[i][j]==0){
continue;
}
for (int p=0;p<i;p++) {
//查看前面是否有空格可移入
if(s[p][j] == 0){
s[p][j] = s[i][j];
s[i][j] = 0;
break;
}
}
}
}
//相加
for (int j=0;j<4;j++) {
for (int i=0;i<3;i++) {
if(s[i][j]==s[i+1][j]){
s[i][j]=s[i][j]*2;
score+=s[i][j];
s[i+1][j]=0;
for(int p=i+2;p<4;p++){
s[p-1][j] = s[p][j];
}
}
}
}
}
void MainWindow::PressRight(){
//移动
for (int j=0;j<4;j++) {
for (int i=2;i>=0;i--) {
if(s[i][j]==0){
continue;
}
for (int p=3;p>i;p--) {
//查看前面是否有空格可移入
if(s[p][j] == 0){
s[p][j] = s[i][j];
s[i][j] = 0;
break;
}
}
}
}
//相加
for (int j=0;j<4;j++) {
for (int i=3;i>=0;i--) {
if(s[i][j]==s[i-1][j]){
s[i][j]=s[i][j]*2;
s[i-1][j]=0;
score+=s[i][j];
for(int p=i-2;p>=0;p--){
s[p+1][j] = s[p][j];
}
}
}
}
}
void MainWindow::myRand(){
int i=0,j=0;
//找出格子
struct Ns n[15];
int ni=0;
for (i=0;i<4;i++) {
for (j=0;j<4;j++) {
if(s[i][j]==0){
n[ni].i=i;
n[ni].j=j;
ni++;
}
}
}
//判断游戏是否结束
if (ni==0) {
for (i=0;i<4;i++) {
for (j=0;j<3;j++) {
if(s[i][j]==s[i][j+1]){
return;
}
}
}
for (j=0;j<4;j++) {
for (i=0;i<3;i++) {
if(s[i][j]==s[i+1][j]){
return;
}
}
}
QMessageBox::about(this,"游戏失败","分数为:"+QString::number(score)+"
");
return;
}
int rand=qrand()%ni;
s[n[rand].i][n[rand].j]=2;
}

在这里插入图片描述

5.结果

在这里插入图片描述


总结

以上是今天要讲的内容,练习了2048小游戏。

  • 14
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
抱歉,我是一名语言模型AI,不能实际编写代码。但是,我可以给您提供以下的示例代码,您可以参考此代码来编写一个基于QT框架的2048小游戏: ```c++ #include <QApplication> #include <QKeyEvent> #include <QLabel> #include <QPushButton> #include <QVBoxLayout> #include <QDebug> class Game : public QWidget { public: Game(QWidget* parent = nullptr) : QWidget(parent) { createBoard(); setLayout(new QVBoxLayout()); layout()->addWidget(m_board); layout()->addWidget(m_button); setFocusPolicy(Qt::StrongFocus); } private: enum { BOARD_SIZE = 4 }; enum { CELL_SIZE = 100 }; struct Position { Position(int x_, int y_) : x(x_), y(y_) {} int x; int y; }; QLabel* cell(int x, int y) const { return qobject_cast<QLabel*>(m_board->itemAtPosition(y, x)->widget()); } bool valid(int x, int y) const { return x >= 0 && y >= 0 && x < BOARD_SIZE && y < BOARD_SIZE; } Position findEmptyCell() const { while (true) { int x = qrand() % BOARD_SIZE; int y = qrand() % BOARD_SIZE; if (cell(x, y)->text().isEmpty()) return Position(x, y); } } void createBoard() { m_board = new QGridLayout(); for (int y = 0; y < BOARD_SIZE; ++y) { for (int x = 0; x < BOARD_SIZE; ++x) { QLabel* label = new QLabel(); label->setFixedSize(CELL_SIZE, CELL_SIZE); label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); label->setStyleSheet("background-color: #fbb03b; font-size: 36px; font-weight: bold; color: #776e65; border-radius: 5px"); m_board->addWidget(label, y, x); } } cell(0, 0)->setText("2"); cell(1, 0)->setText("2"); } bool merge(Position from, Position to) { if (!valid(to.x, to.y) || !valid(from.x, from.y)) return false; QLabel* fromLabel = cell(from.x, from.y); QLabel* toLabel = cell(to.x, to.y); if (toLabel->text().isEmpty()) { toLabel->setText(fromLabel->text()); fromLabel->setText(""); return true; } if (toLabel->text() == fromLabel->text()) { int value = toLabel->text().toInt() * 2; toLabel->setText(QString::number(value)); fromLabel->setText(""); return true; } return false; } void restart() { for (int y = 0; y < BOARD_SIZE; ++y) { for (int x = 0; x < BOARD_SIZE; ++x) { cell(x, y)->setText(""); } } int x1 = qrand() % BOARD_SIZE; int y1 = qrand() % BOARD_SIZE; int x2 = qrand() % BOARD_SIZE; int y2 = qrand() % BOARD_SIZE; cell(x1, y1)->setText("2"); cell(x2, y2)->setText("2"); } bool isGameOver() const { for (int y = 0; y < BOARD_SIZE; ++y) { for (int x = 0; x < BOARD_SIZE; ++x) { if (cell(x, y)->text().isEmpty()) return false; if (valid(x + 1, y) && cell(x + 1, y)->text() == cell(x, y)->text()) return false; if (valid(x, y + 1) && cell(x, y + 1)->text() == cell(x, y)->text()) return false; } } return true; } protected: void keyPressEvent(QKeyEvent* event) override { if (event->key() == Qt::Key_Escape) { return qApp->quit(); } if (isGameOver()) { return; } bool changed = false; switch (event->key()) { case Qt::Key_Left: for (int y = 0; y < BOARD_SIZE; ++y) { for (int x = 0; x < BOARD_SIZE; ++x) { for (int x2 = x + 1; x2 < BOARD_SIZE; ++x2) { if (merge(Position(x2, y), Position(x, y))) changed = true; } } } break; case Qt::Key_Right: for (int y = 0; y < BOARD_SIZE; ++y) { for (int x = BOARD_SIZE - 1; x >= 0; --x) { for (int x2 = x - 1; x2 >= 0; --x2) { if (merge(Position(x2, y), Position(x, y))) changed = true; } } } break; case Qt::Key_Up: for (int y = 0; y < BOARD_SIZE; ++y) { for (int x = 0; x < BOARD_SIZE; ++x) { for (int y2 = y + 1; y2 < BOARD_SIZE; ++y2) { if (merge(Position(x, y2), Position(x, y))) changed = true; } } } break; case Qt::Key_Down: for (int y = BOARD_SIZE - 1; y >= 0; --y) { for (int x = 0; x < BOARD_SIZE; ++x) { for (int y2 = y - 1; y2 >= 0; --y2) { if (merge(Position(x, y2), Position(x, y))) changed = true; } } } break; default: break; } if (changed) { insertRandom(); if (isGameOver()) { qDebug() << "Game over!"; } } } void insertRandom() { Position emptyCell = findEmptyCell(); int value = (qrand() % 10 == 0) ? 4 : 2; cell(emptyCell.x, emptyCell.y)->setText(QString::number(value)); } QLabel* m_button = new QLabel("Press Arrow keys to play."); QGridLayout* m_board; }; int main(int argc, char** argv) { QApplication app(argc, argv); app.setApplicationName("2048 Game by Qt"); Game game; game.setGeometry(200, 200, 500, 500); game.show(); return app.exec(); } ``` 希望这可以帮助您开始编写自己的2048小游戏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

K要努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值