版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_24880087/article/details/52433687
代码包在这里:http://download.csdn.net/detail/sinat_24880087/9621471
1.打字训练游戏
typeGame QDialog
1-1 字母 / 砖块 绘制
paintEvent() ---- update()
QPainter 类
资源清单: 26个字母 砖块 背景
数据处理:
struct _STCH
QString szPath
QPoint cPos
QSize 砖块大小
QPoint 桌上角第一块砖的位置
const int max_ch = 5
最多5个字母同时出现
1.绘制砖头 40*40
800/40 = 20 col
640/40 = 16 row
13 14 15
2.处理数据********
3.随机出现字母
字母图片和位置
4.捕获用户按键
KeyPressEvent
5.计分
首先建一个文件叫typeGame 选择 基类 QDialog
名字设为dialog
1.dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include <QMessageBox>
// 声明它是一个外部变量
extern int gScore;
void Dialog::on_startBtn_clicked()
{
initImgList(); // 1.初始化图片路径
initPosList(); // 2.初始化图片位置
// initWallList(); // 3.初始化砖头列表
checkCrash(); // 4.检查字母是否撞倒砖头
// 3.定义控制字母下移的定时器
QTimer *pDownTimer = new QTimer;
connect(pDownTimer,SIGNAL(timeout()),
this,SLOT(updateMap()));
pDownTimer->start(1000);
// 4.定义产生字母的定时器
QTimer *pCharTimer = new QTimer;
connect(pCharTimer,SIGNAL(timeout()),
this,SLOT(createChar()));
pCharTimer->start(3000);
// 5.定义一个刷新显示分数的定时器
QTimer *pScoreTimer = new QTimer;
connect(pScoreTimer,SIGNAL(timeout()),
this,SLOT(updateScore()));
pScoreTimer->start(500);
// 6.定义一个刷新计时的定时器
QTimer *pTimeTimer = new QTimer;
connect(pTimeTimer,SIGNAL(timeout()),
this,SLOT(updateTime()));
pTimeTimer->start(1000);
}
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog),
m_iTime(60)
{
ui->setupUi(this);
initWallList();
checkCrash();
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::paintEvent(QPaintEvent * event)
{
QPainter p(this);
// 1.绘制背景
p.drawPixmap(0,0,800,640,QPixmap(":image/back.jpg"));
// 2.绘制砖头
for(int i = 0; i < m_WallList.size(); ++i)
{
int x = m_WallList.at(i).x();
int y = m_WallList.at(i).y();
p.drawPixmap(x,y,40,40,QPixmap(":image/wall.jpg"));
}
// 3.绘制随机产生的字母
for(int i = 0; i < m_CharList.size(); ++ i)
{
int x = m_CharList.at(i).x;
int y = m_CharList.at(i).y;
int iIndex = m_CharList.at(i).iIndex;
p.drawPixmap(x,y,40,40,QPixmap(m_ImgList.at(iIndex)));
}
}
// 按键事件
void Dialog::keyPressEvent(QKeyEvent * event)
{
m_key = event->key();
m_key += 32;
for(int i = 0; i < m_CharList.size(); ++ i)
if( m_key == m_CharList.at(i).value)
{
m_CharList.removeAt(i);
gScore += 10;
break;
}
update();
return ;
}
// 更新地图
void Dialog::updateMap()
{
// 1.改变字母的位置
for(int i = 0; i < m_CharList.size(); ++ i)
m_CharList[i].y += 40;
// 2.检查字母是否撞倒砖头
checkCrash();
// 3.重绘地图
update();
return ;
}
// 产生字母
void Dialog::createChar()
{
if( m_CharList.size() > 4)
return ;
STCHAR stChar;
stChar.iIndex = rand()%m_ImgList.size();
stChar.x = m_PosList.at(stChar.iIndex%20).x();
stChar.y = m_PosList.at(stChar.iIndex%20).y();
stChar.value = stChar.iIndex + 'a';
m_CharList.append(stChar);
return ;
}
// 1.初始化图片路径
void Dialog::initImgList()
{
for(char i = 'a'; i<='z'; ++i)
{
QString szPath = QString(":image/%1.jpg").arg(i);
m_ImgList.append(szPath);
}
return ;
}
// 2.初始化图片位置
void Dialog::initPosList()
{
for(int iCol = 0; iCol < 20; ++iCol)
m_PosList.append(QPoint(iCol*40,0));
return ;
}
// 3.初始化砖头列表
void Dialog::initWallList()
{
for(int iRow = 13; iRow < 16; ++ iRow)
for(int iCol = 0; iCol < 20; ++iCol)
m_WallList.append(QPoint(iCol*40,iRow*40));
return ;
}
// 4.检查字母是否撞倒砖头
void Dialog::checkCrash()
{
for(int i = 0; i < m_CharList.size(); ++i)
for(int j = 0; j < m_WallList.size(); ++j)
{
int x1 = m_CharList.at(i).x;
int y1 = m_CharList.at(i).y;
int x2 = m_WallList.at(j).x();
int y2 = m_WallList.at(j).y();
if ( x1 == x2 && y1 == y2)
{
m_CharList.removeAt(i);
m_WallList.removeAt(j);
gScore -=5;
return ;
}
}
}
// 5.定义一个刷新显示分数
void Dialog::updateScore()
{
ui->scoreLcd->display(gScore);
return ;
}
// 5.定义一个刷新显示时间
void Dialog::updateTime()
{
ui->timeLcd->display(m_iTime--);
if( m_iTime < 0 )
close();
return ;
}
2.dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QPainter>
#include <QTimer>
#include <QKeyEvent>
// 字符信息结构体
typedef struct _STCHAR
{
int iIndex; // 字母索引值
int x; // 字母X坐标
int y; // 字母Y坐标
int value; // 字母值
}STCHAR;
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
void initImgList(); // 1.初始化图片路径
void initPosList(); // 2.初始化图片位置
void initWallList(); // 3.初始化砖头列表
void checkCrash(); // 4.检查字母是否撞倒砖头
protected:
void paintEvent(QPaintEvent * event); // 绘图事件
void keyPressEvent(QKeyEvent * event); // 按键事件
public slots:
void updateMap(); // 更新地图
void createChar(); // 创建字符
void updateScore(); // 更新分数
void updateTime(); // 更新时间
private slots:
void on_startBtn_clicked();
private:
Ui::Dialog *ui;
QList<QString> m_ImgList; // 字母图片路径
QList<QPoint> m_PosList; // 字母初始位置
QList<STCHAR> m_CharList; // 字符链表
QList<QPoint> m_WallList; // 砖头列表
int m_key; // 用户输入Key
int m_iTime; // 计时时间
int gScore = 0;
};
#endif // DIALOG_H
3。dialog.ui
4。 main.cpp
cpp中的代码建完文件就有 一般不需要改什么,
这里用到时间需要添加QTime
#include "dialog.h"
#include <QApplication>
#include <QTime>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 设置当前系统时间(微秒)为随机数种子
srand(QTime::currentTime().msec());
Dialog w;
w.show();
return a.exec();
}
5 。添加image图片文件
网上随便找26个字母图片 和 背景 图片 以及 砖块的图片
并且按照‘a’‘b’…等方式命名
6 。测试运行