資源簡介
qt下簡單的2048源碼,參考網(wǎng)上的代碼進行一些修改,
可以很好的運行
代碼片段和文件信息
#include?
#include?“GameWidget.h“
//?顏色數(shù)組?存儲每個數(shù)字對應(yīng)的背景色
QColor?digitBkg[11]?=?{QColor::fromRgb(0xEE?0xE5?0xDB)?QColor::fromRgb(0xEC?0xE0?0xC8)
????????????????????????????QColor::fromRgb(0xF2?0xAF?0x78)?QColor::fromRgb(0xEE?0x8A?0x54)
????????????????????????????QColor::fromRgb(0xFE?0x76?0x5E)?QColor::fromRgb(0xE7?0x58?0xC)
????????????????????????????QColor::fromRgb(0xFF?0x66?0x66)?QColor::fromRgb(0xF1?0xCF?0x48)
????????????????????????????QColor::fromRgb(0xCC?0x33?0x33)?QColor::fromRgb(0xE3?0xB9?0x11)
????????????????????????????QColor::fromRgb(0xFF?0x00?0x00)};
//?每個方向位置的增量
QPointF?dPos[5]?=?{QPointF(-10?0)?QPointF(10?0)?QPointF(0?-10)?QPointF(0?10)?QPointF(-2?-2)};
GameWidget::GameWidget(QWidget?*parent)?:
????QWidget(parent)
{
????//?連接手勢移動信號和相應(yīng)的槽函數(shù)
????connect(this?SIGNAL(GestureMove(GestureDirect))?SLOT(onGestureMove(GestureDirect)));
????//?初始化board數(shù)組
????memset(board?0?sizeof(board));
????//?隨機填入兩個2
????board[rand()?%?4][rand()?%?4]?=?2;
????while(true)?{
????????int?x?=?rand()?%?4;
????????int?y?=?rand()?%?4;
????????if?(board[x][y]?!=?0)?{
????????????continue;
????????}
????????else?{
????????????board[x][y]?=?2;
????????????break;
????????}
????}
????//?分?jǐn)?shù)初始化為0
????score?=?0;
????//?數(shù)碼個數(shù)初始化為2
????digitCount?=?2;
????isAnimating?=?false;
????cacheImg?=?NULL;
}
void?GameWidget::keyPressEvent(QKeyEvent?*event)
{
????if?(isAnimating)
????????return;
????switch?(event->key())?{
????????case?Qt::Key_Left:
????????????emit?GestureMove(LEFT);
????????break;
????????case?Qt::Key_Right:
????????????emit?GestureMove(RIGHT);
????????break;
????????case?Qt::Key_Down:
????????????emit?GestureMove(DOWN);
????????break;
????????case?Qt::Key_Up:
????????????emit?GestureMove(UP);
????????break;
????????default:
????????break;
????}
????QWidget::keyPressEvent(event);
}
void?GameWidget::mousePressEvent(QMouseEvent?*e)
{
????//?獲取起點坐標(biāo)
????startPos?=?e->pos();
}
void?GameWidget::mouseReleaseEvent(QMouseEvent?*e)
{
????//?如果在播放動畫效果則直接退出防止重復(fù)產(chǎn)生手勢事件
????if?(isAnimating)
????????return;
????//?根據(jù)終點坐標(biāo)和起點坐標(biāo)計算XY坐標(biāo)的增量
????float?dX?=?(float)(e->pos().x()?-?startPos.x());
????float?dY?=?(float)(e->pos().y()?-?startPos.y());
????//?確定手勢方向
????if?(abs(dX)?>?abs(dY))
????{
????????if?(dX?0)
????????????emit?GestureMove(LEFT);
????????else
????????????emit?GestureMove(RIGHT);
????}
????else
????{
????????if?(dY?0)
????????????emit?GestureMove(UP);
????????else
????????????emit?GestureMove(DOWN);
????}
}
void?GameWidget::onGestureMove(GestureDirect?direct)
{
????int?i?j?k;
????Animation?a;
????//?是否合并過方格
????bool?combine?=?false;
????//?處理不同方向
????switch?(direct)
????{
????//?向左
????case?LEFT:
????????//?循環(huán)每一行
????????for?(i?=?0;?i?4;?i++)
????????{
????????????/*?初始化j?k為0
?????????????*?這里j表示要交換的數(shù)字列號
?????????????*?k表示交換到的位置的列號
?????????????*?*/
????????????j?=?0?k?=?0?combine?=?false;
??
- 上一篇:Qt下tcp和udp的編程
- 下一篇:QZxing解碼二維碼
評論
共有 條評論