Qt打字小游戏

1.打字训练游戏

typeGame QDialog

1-1字母/砖图

paintEvent() - update()

QPainter类

资源列表:26个字母的砖块背景

数据处理:

结构_STCH

QString szPath

QPoint cPos

QSize砖块大小

QPoint表角落第一块砖的位置。

const int max_ch = 5

最多同时出现5个字母。

1.画砖40*40

800/40 = 20 col

640/40 = 16行

13 14 15

2.正在处理数据* * * * * * * *

3.随机字母

字母图片和位置

4.捕获用户击键

按键事件

得分

1

2

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

首先,构建一个名为typeGame的文件来选择基类QDialog。

将名称设置为对话框。

1.dialog.cpp

#include "dialog.h "

#include "ui_dialog.h "

# include & ltQMessageBox & gt

//声明它是一个外部变量

extern int gScore

void Dialog::on _ start BTN _ clicked()

{

initImgList();// 1.初始化图像路径

initPosList();// 2.初始化图像位置

//initWallList();// 3.初始化砖块列表

check crash();// 4.检查信件是否撞倒了砖块。

// 3.定义一个定时器来控制字母向下移动。

QTimer * pDownTimer = new QTimer

connect(pDownTimer,SIGNAL(timeout()),

this,SLOT(update map());

pDownTimer-& gt;开始(1000);

// 4.定义生成信件的计时器。

QTimer * pCharTimer = new QTimer

connect(pCharTimer,SIGNAL(timeout()),

this,SLOT(create char());

pCharTimer-& gt;开始(3000);

// 5.定义一个计时器来刷新显示的分数。

QTimer * pScoreTimer = new QTimer

connect(pScoreTimer,SIGNAL(timeout()),

this,SLOT(update score()));

pScoreTimer-& gt;开始(500);

// 6.为刷新计时定义一个定时器。

QTimer * pTimeTimer = new QTimer

connect(pTimeTimer,SIGNAL(timeout()),

this,SLOT(update time());

pTimeTimer-& gt;开始(1000);

}

Dialog::Dialog(QWidget *parent):

QDialog(父级),

ui(新Ui::对话框),

时间(60)

{

ui-& gt;setupUi(这个);

initWallList();

check crash();

}

对话框::~对话框()

{

删除ui;

}

void对话框::paintEvent(QPaintEvent * event)

{

QPainter p(这个);

// 1.绘制背景

p.drawPixmap(0,0,800,640,QPixmap(":image/back . jpg "));

// 2.画砖

for(int I = 0;我& ltm _ wall list . 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;我& ltm _ 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 _ img list . at(iIndex)));

}

}

//击键事件

void Dialog::keypress event(QKeyEvent * event)

{

m _ key = event-& gt;key();

m _ key+= 32;

for(int I = 0;我& ltm _ charlist . size();++ i)

if( m_key == m_CharList.at(i)。值)

{

m _ charlist . remove at(I);

GS core+= 10;

打破;

}

update();

返回;

}

//更新地图

void Dialog::updateMap()

{

// 1.改变字母的位置

for(int I = 0;我& ltm _ charlist . size();++ i)

m_CharList[i]。y+= 40;

// 2.检查信件是否撞倒了砖块。

check crash();

// 3.重画地图

update();

返回;

}

//生成信件

void对话框::createChar()

{

if(m _ charlist . size()& gt;4)

返回;

STCHAR stChar

stchar . iindex = rand()% m _ img list . size();

stchar . x = m _ pos list . at(stchar . iindex % 20)。x();

stchar . y = m _ pos list . at(stchar . iindex % 20)。y();

stchar . value = stchar . iindex+' a ';

m _ charlist . append(stChar);

返回;

}

// 1.初始化图像路径

void Dialog::initImgList()

{

for(char I = ' a ';我& lt= ' z++i)

{

QString SZ path = QString(":image/% 1 . jpg ")。arg(I);

m _ img list . append(SZ path);

}

返回;

}

// 2.初始化图像位置

void Dialog::initPosList()

{

for(int iCol = 0;iCol & lt20;++iCol)

m _ pos list . append(q point(iCol * 40,0));

返回;

}

// 3.初始化砖块列表

void Dialog::initWallList()

{

for(int iRow = 13;iRow & lt16;++ iRow)

for(int iCol = 0;iCol & lt20;++iCol)

m _ wall list . append(q point(iCol * 40,iRow * 40));

返回;

}

// 4.检查信件是否撞倒了砖块。

void对话框::checkCrash()

{

for(int I = 0;我& ltm _ charlist . size();++i)

for(int j = 0;j & ltm _ wall list . 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(x 1 = = x2 & amp;& ampy1 == y2)

{

m _ charlist . remove at(I);

m _ walllist . remove at(j);

gScore-= 5;

返回;

}

}

}

// 5.定义刷新显示分数。

void对话框::updateScore()

{

ui-& gt;score LCD-& gt;显示器(gScore);

返回;

}

// 5.定义刷新显示时间。

void Dialog::updateTime()

{

ui-& gt;time LCD-& gt;display(m _ iTime-);

if(m _ iTime & lt;0 )

close();

返回;

}

1

2

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

四十二个

43

四十四

45

46

47

48

四十九个

50

51

五十二个

53

54

55

五十六岁

57

58

59

60

61

62

63

64

65

66

67

六十八

六十九

70

71

七十二个

73

74

75

76

77

七十八

79

80

81

82

83

84

八十五

86

87

88

八十九

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

2 .对话框

#ifndef DIALOG_H

#定义对话框_H

# include & ltQDialog & gt

# include & ltQPainter & gt

# include & ltQTimer & gt

# include & ltQKeyEvent & gt

//字符信息结构

typedef struct _STCHAR

{

int iIndex//字母索引值

int x;//字母x坐标

int y;//字母y坐标

int值;//字母值

} STCHAR

命名空间Ui {

课堂对话;

}

类对话框:公共QDialog

{

q _对象

公共:

显式对话框(q widget * parent = 0);

~ Dialog();

void initImgList();// 1.初始化图像路径

void initPosList();// 2.初始化图像位置

void initWallList();// 3.初始化砖块列表

void check crash();// 4.检查信件是否撞倒了砖块。

受保护:

void paint event(QPaintEvent * event);//绘图事件

void keypress event(QKeyEvent * event);//击键事件

公共插槽:

void update map();//更新地图

void create char();//创建字符

void update score();//更新分数

void update time();//更新时间

专用插槽:

void on _ start BTN _ clicked();

私人:

ui::Dialog * ui;

QList & ltQString & gtm _ ImgList//字母图片路径

QList & ltQPoint & gtm _ PosList//字母的初始位置

QList & ltSTCHAR & gtm _ CharList//字符链表

QList & ltQPoint & gtm _ WallList//砖块列表

int m _ key//用户输入密钥。

int m _ iTime//计时时间

int GS core = 0;

};

#endif // DIALOG_H

-

作者:王木木杰

资料来源:CSDN

原文:/sinat _ 24880087/article/details/52433687

版权声明:本文为博主原创文章,转载请附上博客链接!