2048前言
总体的执行流程是 初始化背景->随机生成数字->绘制->滑动-》归零,计算,归零->随机生成数字->绘制。
绘制是根据一个二位数组来存储的数值来进行绘制的,为了方便逻辑计算,我用的是4*4的数组,从1,1,开始计数。
二维数组存储的是每个格子应该显示的数值,初始化的时候都为0.
首先,我们先通俗易懂说一下这个小游戏这个游戏的核心实现思路:
1,归零:我们将出现在格子里面的非零的数字都移动到手滑动的方向去。
2,计算:移动到一起之后,非零数字之间没有空隙了,我们对他们
相邻的两个数字相同的进行相加计算。将计算的结果保存在离手指滑动方向较远的那个位置,那么,被消除的那个位置就要重置数值为0;
3,归零:上面第二步操作之后,非零数字中间又出现了0,所以我们又要执行第一步,归零。这样,一个基本的游戏逻辑运算就完成了。
操作流程:
操作:分别通过上下左右方向键模拟左下上右各个方向。
判断输赢:棋盘满了输,没有满就无限继续
实现的原理:
游戏的工作原理,实际上可以将游戏描述为四个带有方向的同一操作:
1、将所有数字向一个方向移动至中间没有空位
2、将相邻的两个相同的数字加和然后放在更靠近移动方向前部的一个位置上
另外需要判断一下玩家当前输入的内容是否可以执行,如果不可以执行等待用户下一条记录。
同时需要对游戏的进程进行控制,如果可以继续游戏,那么运行玩家继续输入下一条指令,而如果不可以进行,那么提示无法继续游戏的提示。
通过重写键盘事件,在输入上下左右时 分别调用对应的函数进行游戏操作。
关键的来了,上核心代码:
weight.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QKeyEvent>
#include "movemap.h"
#include "form.h"
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public slots:
void Resetgame();
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void setMap();
void setScore();
void setval(int p,int val);
void setclear(int p);
private:
Ui::Widget *ui;
moveMap mm;
int map[4][4];
int score;
Form *fm;
protected:
virtual void keyPressEvent(QKeyEvent* event);
};
#endif // WIDGET_H
weight.cpp
#include "widget.h"
#include "ui_widget.h"
#include "gamestartend.h"
#include <cstdlib>
#include <cstring>
#include <QDebug>
#include <QTime>
#include <QtGlobal>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
memset(map,0,sizeof(map));
score = 0;
/* map[0][0] = 4; map[0][1] = 4; map[0][2] = 4; map[0][3] = 4;
map[1][0] = 16; map[1][1] = 4; map[1][2] = 4; map[1][3] = 8;
map[2][0] = 4; map[2][1] = 0; map[2][2] = 4; map[2][3] = 4;
map[3][0] = 0; map[3][1] = 4; map[3][2] = 8; map[3][3] = 2;*/
resetMap(map); //初始化地图
setMap(); //设置地图
setScore(); //设置得分
fm = new Form;
fm->hide();
QObject::connect(fm,SIGNAL(Resetgame()),this,SLOT(Resetgame()));
QObject::connect(fm,SIGNAL(Exit()),this,SLOT(close()));
}
Widget::~Widget()
{
delete ui;
}
/*
* 函数名:setMap
* 参数:无
* 返回值:空
* 函数功能:将私有成员map中的数据 插入对应的标签中
*/
void Widget::setMap()
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(map[i][j])
{
setval(i*10+j,map[i][j]);
}
else
{
setclear(i*10+j);
}
}
}
}
/*
* 函数名:setval
* 参数:p为要插入的位置,val为插入的值
* 返回值:空
* 函数功能:将val插入p位置的标签
*/
void Widget::setval(int p,int val)
{
QString text = QString::number(val,10);
// qDebug()<<text;
switch(p){
case 0:ui->label00->setText(text);break;
case 1:ui->label01->setText(text);break;
case 2:ui->label02->setText(text);break;
case 3:ui->label03->setText(text);break;
case 10:ui->label10->setText(text);break;
case 11:ui->label11->setText(text);break;
case 12:ui->label12->setText(text);break;
case 13:ui->label13->setText(text);break;
case 20:ui->label20->setText(text);break;
case 21:ui->label21->setText(text);break;
case 22:ui->label22->setText(text);break;
case 23:ui->label23->setText(text);break;
case 30:ui->label30->setText(text);break;
case 31:ui->label31->setText(text);break;
case 32:ui->label32->setText(text);break;
case 33:ui->label33->setText(text);break;
default:qDebug()<<"setval error";
}
}
/*
* 函数名:setclear
* 参数:p为要清空数据的标签位置
* 返回值:空
* 函数功能:将p位置的标签内容清空
*/
void Widget::setclear(int p)
{
switch(p)
{
case 0:ui->label00->clear();break;
case 1:ui->label01->clear();break;
case 2:ui->label02->clear();break;
case 3:ui->label03->clear();break;
case 10:ui->label10->clear();break;
case 11:ui->label11->clear();break;
case 12:ui->label12->clear();break;
case 13:ui->label13->clear();break;
case 20:ui->label20->clear();break;
case 21:ui->label21->clear();break;
case 22:ui->label22->clear();break;
case 23:ui->label23->clear();break;
case 30:ui->label30->clear();break;
case 31:ui->label31->clear();break;
case 32:ui->label32->clear();break;
case 33:ui->label33->clear();break;
}
}
/*
* 函数名:keyPressevent
* 参数:键盘事件
* 返回值:空
* 函数功能:通过重写键盘事件 当输入上下左右时 分别调用对应的函数更新map的值
*/
void Widget::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Up)
{
score+=mm.upMap(map);
if(addval(map) == false) fm->show();
setMap();
setScore();
}
if(event->key() == Qt::Key_Down)
{
score+=mm.downMap(map);
if(addval(map) == false) fm->show();
setMap();
setScore();
}
if(event->key() == Qt::Key_Left)
{
score+=mm.leftMap(map);
if(addval(map) == false) fm->show();
setMap();
setScore();
}
if(event->key() == Qt::Key_Right)
{
score += mm.rightMap(map);
if(addval(map) == false) fm->show();
setMap();
setScore();
}
}
/*
* 函数名:Resetgame
* 参数:无
* 返回值:空
* 函数功能:重置map 重新开始游戏
*/
void Widget::Resetgame()
{
memset(map,0,sizeof(map));
score = 0;
resetMap(map);
setMap();
setScore();
}
/*
* 函数名:setScore
* 参数:无
* 返回值:空
* 函数功能:将私有成员score的数据输出到分数标签中
*/
void Widget::setScore()
{
QString text = QString::number(score,10);
ui->labelscore->setText(text);
}
weight.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>329</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>70</x>
<y>70</y>
<width>238</width>
<height>232</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label00">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 255, 127);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label01">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 85, 255);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label02">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 255, 127);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label03">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 85, 255);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label10">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 85, 255);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label11">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 255, 127);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label12">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 85, 255);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label13">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 255, 127);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label20">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 255, 127);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label21">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 85, 255);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label22">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 255, 127);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label23">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 85, 255);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label30">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 85, 255);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label31">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 255, 127);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label32">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 85, 255);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label33">
<property name="minimumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>54</width>
<height>51</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(0, 255, 127);</string>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>100</x>
<y>30</y>
<width>151</width>
<height>16</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string> 目前得分:</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labelscore">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
代码比较多,就不一一在这贴了,详情,请去下载:
下载地址:
网络比较慢,在评论区查找~