Ubuntu 中Qt PVP五子棋注册登录系统(内含代码)

使用Qt框架在Ubuntu上创建的五子棋PVP系统,结合MySQL实现用户注册与登录功能。通过Qt与mysql数据库连接完成登录注册操作,五子棋界面利用画家和鼠标事件交互。系统包括背景设置、棋盘绘制、鼠标位置判断、黑白交替及计时器,具备输赢判断和重新开始选项。详细介绍了各个功能的实现方法,并提供了代码示例。
摘要由CSDN通过智能技术生成

刚刚入手Qt,参考网上的各种资源和课上所学,做了一个PVP五子棋注册登录系统,由于能力有限,代码十分的长,但是实现了基本的功能,先看一下界面

登录界面
注册界面
游戏界面

简单介绍一下,各功能是如何实现的。注册登录系统是Qt与mysql数据库连接,登录注册其实就是查表和写入表,五子棋界面就是画家和鼠标事件的结合。在五子棋PVP对战这里,我的思路是:
1.设置背景,界面大小
2.画棋盘,将实际坐标转换成棋盘坐标
3.捕获鼠标位置,进行判断,然后在坐标点上画圆。
4.黑白交替,加入计时器。
5.判断输赢,重新开始或退出。

确保自己的系统里安装了mysql,如果没有安装可以
参考链接: 安装mysql及常见指令.

下面是我的各个文件的代码
(gamewindow是五子棋界面,
mainwindow是登录主界面,
newuser是注册界面)

gamewindow.h

#ifndef GAMEWINDOW_H
#define GAMEWINDOW_H

#include <QMainWindow>
#include <QPainter>
#include <QMouseEvent>
#include <QMessageBox>
#include <random>
#include <QSize>
#include <qmath.h>
#include <QString>
#include <QSize>
#include <QDebug>
#include <QTimer>
#include <QTime>


namespace Ui {
   
class GameWindow;
}

class GameWindow : public QMainWindow
{
   
    Q_OBJECT
signals:
    void game_over(int winner);

protected:
    void paintEvent(QPaintEvent *);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);

private slots:
    void BTimerUpdate();
    void WTimerUpdate();
    void onRestartButtonClicked(bool);

public:
    explicit GameWindow(QWidget *parent = nullptr);
    ~GameWindow();
    void initBoard();
    //棋盘大小13*13
    static const int BOARD_WIDTH = 12;
    static const int BOARD_HEIGHT = 12;
    int board[BOARD_WIDTH][BOARD_HEIGHT];
    //棋盘起始位置 行和列
    static const QPoint ROW_NUM_START;
    static const QPoint COLUMN_NUM_START;

    //size
    static const QSize WIDGET_SIZE;
    static const QSize CELL_SIZE;

    static const QPoint START_POS;
    static const int NO_PIECE = 0;         //用于记录棋盘中的某一位置没有棋子

    //定义玩家
    static const int WHITE_PIECE = 1;      //白棋
    static const int BLACK_PIECE = 2;      //黑棋

private:
    Ui::GameWindow *ui;
    void setTrackPos(QPoint point);
    void dropPiece(int x,int y);
    QTimer *WTimer;
    QTimer *BTimer;
    int count;

    void Init();
   //判断输赢  从水平、垂直、、左对角线、右对角线
    bool isFivePiece(int x,int y);
    bool isVFivePiece(int x,int y);
    bool isHFivePiece(int x,int y);
    bool isLeftSlash(int x,int y);
    bool isRightSlash(int x,int y);
    void checkWinner();

//定义变量
    QPoint trackPos;                      //记录鼠标的位置
    bool endGame;                         //标记游戏是否结束
    bool White_player;                    //白棋玩家
    bool Black_player;                    //黑
    bool next_player;                     //下一位玩家


};

#endif // GAMEWINDOW_H

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPainter>
#include <QMovie>
#include <gamewindow.h>
#include <newuser.h>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QLayout>
#include <QCheckBox>
#include <QMessageBox>
#include <QRegExpValidator>
#include <QDate>
#include <QPainter>

#include <QSqlDatabase>
#include <QtSql>
#include <QSqlDriver>
#include <QSqlError>
#include <QSqlQuery>


QT_BEGIN_NAMESPACE
namespace Ui {
    class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
   
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    void onOkButtonClicked(bool);
    void onNewUserButtonClicked(bool);
    void onLOOKButtonClicked(bool);  //用于游客登录
    void onKeyboardButtonPressed();
    void onKeyboardButtonReleased();
    void RequestBack();


private:
    Ui::MainWindow *ui;
    GameWindow *p_t1;
    newUser *p_t2;
    void createConnection();//初始化数据库
private:
    QLineEdit *user_entry;     //用户名
    QLineEdit *passwd_entry;   //密码
    QPushButton* m_EyeButton;  //显示密码的按钮
    QCheckBox *rememberPSW;    //是否记住密码
protected:
    void paintEvent(QPaintEvent *);
};
#endif // MAINWINDOW_H

newuser.h

#ifndef NEWUSER_H
#define NEWUSER_H

#include <QWidget>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QtSql>
#include <QSqlError>
#include <QSqlQuery>
#include <gamewindow.h>

namespace Ui {
   
class newUser;
}

class newUser : public QWidget
{
   
    Q_OBJECT

public:
    explicit newUser(QWidget *parent = nullptr);
    ~newUser();

private slots:
        void on_pushButton_Clicked(bool);
        void BackMain(bool);            

private:
    Ui::newUser *ui;
    void createConnection();
     GameWindow *p_t1;

signals:
    void SignalShowMain();
};

#endif // NEWUSER_H

gamewindow.cpp

#include "gamewindow.h"
#include "ui_gamewindow.h"

GameWindow::GameWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::GameWindow)
{
   
    ui->setupUi(this);
    initBoard();
    Init();
    this->setFixedSize(WIDGET_SIZE);                  //设置界面为固定大小
    this->setAutoFillBackground(true);
    QPalette palette;
    palette.setColor(QPalette::Background,QColor("#B1723C"));
    this->setPalette(palette);
    centralWidget()->setMouseTracking(true);
    setMouseTracking(true);                     //接受鼠标移动
}

GameWindow::~GameWindow()
{
   
    delete ui;
}
const QPoint GameWindow::ROW_NUM_START(15,45);
const QPoint GameWindow::COLUMN_NUM_START(35,25);

const QSize GameWindow::WIDGET_SIZE(800,520);
const QSize GameWindow::CELL_SIZE(40,40);

const QPoint GameWindow::START_POS(40,40);
void GameWindow::Init()
{
       //初始化定时器
    count = 10;
    WTimer = new QTimer(this);
    BTimer = new QTimer(this);
    connect(WTimer,SIGNAL(timeout
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值