一、环境介绍
期末三级项目选择了象棋,共花费两晚找教程学习写基础棋盘和吃子并将本学期所学整合加入。
由于做了好多个选题时间不足,目前仅支持红棋子移动。
操作系统:Win10
Qt版本:5.9.8
二、游戏效果


三、核心代码
logo.h代码
#ifndef LOGO_H
#define LOGO_H
#include <QWidget>
#include <QMediaPlayer>
#include <QPoint>
namespace Ui {
class logo;
}
class logo : public QWidget
{
Q_OBJECT
public:
explicit logo(QWidget *parent = nullptr);
~logo();
private slots:
void mouseMoveEvent(QMouseEvent *e);
void mousePressEvent(QMouseEvent *e);
void on_pushButton_2_clicked();
void on_pushButton_clicked();
private:
Ui::logo *ui;
QPoint last;
};
#endif // LOGO_H
logo.cpp代码
#include "logo.h"
#include "ui_logo.h"
#include "widget.h"
#include <QTimer>
#include <QWidget>
logo::logo(QWidget *parent) :
QWidget(parent),
ui(new Ui::logo)
{
ui->setupUi(this);
}
logo::~logo()
{
delete ui;
}
void logo::on_pushButton_2_clicked()
{
Widget *w=new Widget;
w->setWindowFlag(Qt::FramelessWindowHint);
w->show();
}
void logo::on_pushButton_clicked()
{
this->close();
}
void logo::mousePressEvent(QMouseEvent *e)//鼠标按下事件
{
last = e->globalPos();
}
void logo::mouseMoveEvent(QMouseEvent *e)//鼠标移动事件
{
int dx = e->globalPos().x() - last.x();
int dy = e->globalPos().y() - last.y();
move(x()+dx,y()+dy);
last = e->globalPos();
}
stone.h代码
#ifndef STONE_H
#define STONE_H
#include <QString>
class Stone
{
public:
Stone();
~Stone();
int _row;
int _col;
int _id;
bool _red;
bool _death;
enum Type{jiang,shi,jv,pao,bing,ma,xiang};//定义枚举类型
Type _type;
void init(int id){
struct{
int row,col;
Stone::Type type;
}pos[16]={
{0,0,Stone::jv},
{0,1,Stone::ma},
{0,2,Stone::xiang},
{0,3,Stone::shi},
{0,4,Stone::jiang},
{0,5,Stone::shi},
{0,6,Stone::xiang},
{0,7,Stone::ma},
{0,8,Stone::jv},
{2,1,Stone::pao},
{2,7,Stone::pao},
{3,0,Stone::bing},
{3,2,Stone::bing},
{3,4,Stone::bing},
{3,6,Stone::bing},
{3,8,Stone::bing},
};
if(id<16){
_row=9-pos[id].row;
_col=8-pos[id].col;
_type=pos[id].type;
}
else{
_row=pos[id-16].row;
_col=pos[id-16].col;
_type=pos[id-16].type;
}
_id=id;
_death=false;
_red=id<16;
}
QString getText(){
switch(this->_type){
case jv:
return "车";
case shi:
return "士";
case jiang:
return "将";
case pao:
return "炮";
case bing:
return "兵";
case ma:
return "马";
case xiang:
return "象";
}
return "错误";
}
};
#endif // STONE_H
stone.cpp


1426





