juego de entrada qt
typeGame QDialog
1-1 letra/dibujo de ladrillo
paintEvent() ---- update()
Clase QPainter
Lista de recursos: 26 letras Bricks Background
Procesamiento de datos:
struct _STCH
QString szPath
p>QPoint cPos
Tamaño del ladrillo QSize
Posición QPoint del primer ladrillo en la esquina del escritorio
const int max_ch = 5 p >
Muestra hasta 5 letras al mismo tiempo
1. Dibujar ladrillos 40*40
800/40 = 20 col
640/ 40 = 16 filas
13 14 15
2. Procesa los datos*********
3.
Imágenes y posiciones de las letras
4. Capturar claves de usuario
KeyPressEvent
5 Puntuación
1<. /p>
2
3
4
5
6
7
8
9
10
11
1.dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include lt;QMessageBoxgt;
//Declararlo como una variable externa
extern int gScore;
void Dialog::on_startBtn_clicked()
{
initImgList() // 1.1.Inicializar la ruta de la imagen<; /p>
initPosList(); // 2.Inicializa la posición de la imagen
// initWallList() // 3.Inicializa la lista de ladrillos
checkCrash(); ; // 4. Comprueba si la letra choca contra los ladrillos
// 3. Define el temporizador que controla el movimiento hacia abajo de la letra
QTimer *pDownTimer = new QTimer
connect(pDownTimer, SEÑAL (timeout()),
this, SLOT(updateMap()));
pDownTimer-gt;
// 4. Defina el temporizador para generar letras
QTimer *pCharTimer = new QTimer;
connect(pCharTimer, SIGNAL(timeout()),
this, SLOT(createChar())) ;
pCharTimer-gt.start(3000);
// 5. Defina el temporizador para actualizar la partitura mostrada
QTimer *pPuntuaciónTi
mer = nuevo QTimer;
connect(pScoreTimer, SIGNAL(timeout()),
this, SLOT(updateScore()));
pScoreTimer-gt. ;start(500);
// 6. Defina un temporizador para actualizar el tiempo
QTimer *pTimeTimer = new QTimer;
connect( pTimeTimer, SIGNAL ( tiempo de espera()),
this, SLOT(updateTime()));
pTimeTimer-gt;
Dialog::Dialog(QWidget *padre):
QDialog(padre),
ui(nueva Ui::Dialog),
m_iTime ( 60)
{
uigt; setupUi(this);
initWallList()
checkCrash();
}
Diálogo::~Dialog()
{
eliminar interfaz de usuario
}
< p; >void Dialog::paintEvent(QPaintEvent * event){
QPainter p(this);
// 1. Dibuja el fondo
p.drawPixmap(0, 0, 800, 640, QPixmap(":image/back.jpg"));
// 2. Dibujar ladrillos
para ( int i = 0; i lt; 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")); p>
}
for(int i = 0; i lt; m_CharList.size(); i)
{
int x = m_CharList.
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));
}
}
//Evento clave
void Dialog::keyPressEvent(QKeyEvent * evento)
{
m_key = event-gt;key();
m_key = 32;
for(int i = 0; i lt; m_CharList.size(); i)
si(
m_key == m_CharList.at(i).value)
{
m_CharList.removeAt(i);
gScore = 10;
romper;
}
actualizar();
regresar
}
// actualizar Mapa
void Dialog:: updateMap()
{
/// 1. Cambia la posición de las letras
for(int i = 0; i lt; m_CharList.size(); i)
m_CharList[i].y = 40;
// 2. Comprueba si las letras golpean los ladrillos
checkCrash();
// 3. Redibujar el mapa
update();
return;
}
//Generar letras
void Dialog::createChar ()
{
if( m_CharList.size() gt 4 )
retorno;
STCHAR stChar;
stChar.iIndex = rand()m_ImgList.size();
stChar .x = m_PosList.at(stChar.iIndex20).x();
stChar.y = m_PosList.at(stChar.iIndex20).y();
stChar.value = stChar .iIndex 'a';
m_CharList.append(stChar);
retorno
}
// 1. Inicialice la ruta de la imagen
void Dialog::initImgList()
{
for( char i = 'a'; ilt; = 'z'; i ) p>
{
QString szPath = QString(":image/1.jpg").arg(i);
m_ImgList.append(szPath);
}
return;
}
// 2. Inicializa la posición de la imagen
void Dialog::initPosList()
{
for(int iCol = 0; iCol lt; 20; iCol)
m_PosList .append(QPoint(iCol*40, 0));
return;
}
// 3. Inicializa la lista de ladrillos
void Dialog::initWallList()
{
for(int iRow = 13; iRow lt; 16; iRow)
for(int iCol = 0; iCol lt; 20; iCol)
m_WallList .append(QPoint(iCol*40,iRow*40));
return;
<p>}
// 4. Comprueba si la letra estrella el ladrillo
void Dialog::checkCrash()
{
for(int i = 0; i lt; m_CharList.size(); i)
for(int j = 0; j lt; m_ WallList.size(); j) p>
{
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 amp; ;amp; y1 == y2)
{
m_CharList.removeAt(i);
m_WallList.removeAt(j);
gScore -=5;
retorno;
}
}
}
}
}
}
}
// 5. Defina la actualización para mostrar la puntuación
void Dialog::updateScore( )
{
uigt; puntuaciónLcd-gt; mostrar(gScore)
retorno; >// 5. Definir el tiempo de visualización de actualización
void Dialog:: updateTime()
{
ui-gt; timeLcd-gt; -);
if( m_iTime lt; 0)
cerrar();
regresar;
}
1
2
3
4
5
6
7
8
9
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
42
43
44
45
46
47
48