fix snake

This commit is contained in:
Andrei Yankovich 2018-12-01 21:06:40 +03:00
parent 36cfd6fa96
commit 849374d557
25 changed files with 262 additions and 51 deletions

View File

@ -4,8 +4,9 @@
<activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation"
android:name="org.qtproject.qt5.android.bindings.QtActivity"
android:label="-- %%INSERT_APP_NAME%% --"
android:screenOrientation="unspecified"
android:launchMode="singleTop">
android:screenOrientation="landscape"
android:launchMode="singleTop"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>

View File

@ -1,18 +1,20 @@
#include "background.h"
BackGround::BackGround(double x, double y): ItemWorld (x, y) {
this->setSize(200, 4000);
this->setTexture("qrc:/texture/box-texture");
this->setSize(200, 400);
this->setTexture("qrc:/texture/asphalt");
setBeckGroundObject(true);
}
void BackGround::render() {
if (m_x + w() < 0) {
m_x = w() * 0.5;
emit xChanged(m_x);
auto wPart = w() / 2;
if (m_x + wPart < 200) {
setX(wPart);
}
}
void BackGround::reset() {
setX(-10 - (m_x + w()));
setX(0 - w());
render();
}

View File

@ -0,0 +1,19 @@
#include "backgrounditem.h"
#include <QColor>
BackGroundItem::BackGroundItem(double x, double y):
ItemWorld (x, y) {
setBeckGroundObject(true);
reset();
}
void BackGroundItem::reset() {
auto tempColor = QColor(rand() % 255, rand() % 255, rand() % 255, 10);
setColor(tempColor.name(QColor::HexArgb));
auto radius = rand() % 200;
setSize(radius , radius);
setRadius(radius / 2);
}

15
back-end/backgrounditem.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef BACKGROUNDITEM_H
#define BACKGROUNDITEM_H
#include "itemworld.h"
class BackGroundItem : public ItemWorld
{
public:
BackGroundItem(double x, double y);
void reset() override;
};
#endif // BACKGROUNDITEM_H

View File

@ -1,9 +1,12 @@
#include "box.h"
#include <QColor>
Box::Box(double x, double y):
ItemWorld (x, y) {
this->setSize(10, 10);
this->setTexture("qrc:/texture/box-texture");
setColor(QColor(100, 100, 100).name());
}

View File

@ -7,7 +7,7 @@
Controller::Controller() {
srand(static_cast<unsigned int>(time(nullptr)));
timer = new QTimer();
timer->setInterval(33);
timer->setInterval(1);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
}

View File

@ -1,7 +1,8 @@
#include "guiobject.h"
GuiObject::GuiObject(QObject *ptr):
GuiObject::GuiObject(const QString &viewTempalte, QObject *ptr):
QObject (ptr) {
m_viewTemplate = viewTempalte;
generateId();
}
@ -73,6 +74,22 @@ void GuiObject::reset() {
setY(-1);
}
int GuiObject::radius() const {
return m_radius;
}
QString GuiObject::viewTemplate() const {
return m_viewTemplate;
}
void GuiObject::setRadius(int radius) {
if (m_radius == radius)
return;
m_radius = radius;
emit radiusChanged(m_radius);
}
void GuiObject::setW(double w) {
m_w = w;
emit wChanged(m_w);

View File

@ -14,6 +14,8 @@ class GuiObject:public QObject, public BaseClass
Q_PROPERTY(double angle READ angle NOTIFY angleChanged)
Q_PROPERTY(QString texture READ texture NOTIFY textureChanged)
Q_PROPERTY(int guiId READ guiId NOTIFY guiIdChanged)
Q_PROPERTY(QString viewTemplate READ viewTemplate NOTIFY viewTemplateChanged)
Q_PROPERTY(int radius READ radius WRITE setRadius NOTIFY radiusChanged)
Q_PROPERTY(double x READ x NOTIFY xChanged)
Q_PROPERTY(double y READ y NOTIFY yChanged)
@ -22,23 +24,25 @@ class GuiObject:public QObject, public BaseClass
private:
void generateId();
QString m_viewTemplate;
protected:
int m_guiId;
double m_angle;
QString m_texture;
QString m_color;
int m_guiId = -1;
double m_angle = 0;
QString m_texture = "";
QString m_color = "";
int m_radius = 0;
double m_x;
double m_y;
double m_w;
double m_h;
double m_x = 0;
double m_y = 0;
double m_w = 0;
double m_h = 0;
void setTexture(const QString &texture);
public:
GuiObject(QObject *ptr = nullptr);
GuiObject(const QString& viewTempalte = "GraphicItem", QObject *ptr = nullptr);
double angle() const;
QString texture() const;
@ -64,6 +68,12 @@ public:
void setH(double h);
virtual void reset();
int radius() const;
QString viewTemplate() const;
public slots:
void setRadius(int radius);
signals:
void angleChanged(double angle);
@ -74,6 +84,8 @@ signals:
void yChanged(double y);
void wChanged(double w);
void hChanged(double h);
void radiusChanged(int radius);
void viewTemplateChanged(QString viewTemplate);
};
#endif // GUIOBJECT_H

View File

@ -3,6 +3,7 @@
#include "utils.h"
#include "head.h"
#include "background.h"
#include "backgrounditem.h"
GuiObjectFactory::GuiObjectFactory() {}
@ -14,6 +15,9 @@ ItemWorld *GuiObjectFactory::generate(const QString &name) {
else if (name == "BackGround") {
obj = new BackGround(0, 0);
}
else if (name == "BackGroundItem") {
obj = new BackGroundItem(0, 0);
}
return obj;
}

View File

@ -11,6 +11,24 @@ void Head::render() {
double my = (m_y + (*speed * 0.55) * sin(m_angle * TO_RADIAN));
m_y += (my - m_y) / 1000 * tempTime;
if (*speed < 1) {
setColor(generalSpeadColor);
setRadius(static_cast<int>(m_w * 0.0));
} else if (*speed < normSpead) {
setColor(normSpeadColor);
setRadius(static_cast<int>(m_w * 0.15));
} else if (*speed < fastSpead) {
setColor(fastSpeadColor);
setRadius(static_cast<int>(m_w * 0.35));
} else if (*speed < megaFastSpead) {
setColor(megaFastSpeadColor);
setRadius(static_cast<int>(m_w * 0.5));
}
emit yChanged(m_y);
}
@ -18,7 +36,7 @@ void Head::reset() {
}
Head::Head(double x, double y, double h, double w, double *spead):
GuiObject () {
GuiObject ("SnakeItem") {
setX(x);
setY(y);
setW(w);

View File

@ -11,6 +11,15 @@ class Head : public GuiObject
private:
qint64 time;
double *speed;
const int megaFastSpead = 200;
const int fastSpead = 100;
const int normSpead = 50;
const QString generalSpeadColor = "#616a6b";
const QString normSpeadColor = "#5d6d7e";
const QString fastSpeadColor = "#eb984e";
const QString megaFastSpeadColor = "#ec7063";
public:
Head(double x , double y, double h, double w, double *speed);
void setAngle(double angle) override;

View File

@ -6,7 +6,8 @@
#define POINT 100
ItemWorld::ItemWorld(double x, double y) {
ItemWorld::ItemWorld(double x, double y, const QString& guiTemplate):
GuiObject (guiTemplate) {
setLoc(x, y);
}
@ -25,7 +26,7 @@ void ItemWorld::setLoc(double x, double y) {
}
void ItemWorld::render() {
if (m_x < 0) {
if (m_x + w() < 0) {
m_x = (rand() % 400) + 200;
m_y = rand() % 100;
emit xChanged(m_x);

View File

@ -14,7 +14,7 @@ protected:
void setBeckGroundObject(bool value);
public:
ItemWorld(double x, double y);
ItemWorld(double x, double y, const QString& GuiTemplate = "GraphicItem");
void render();
virtual bool move(const GuiObject* snakeRiger, double dx);

View File

@ -1,9 +1,11 @@
#include "lvls.h"
QList<WorldRules> lvls {
WorldRules{{"Long", 500}, {"Box", 2}, {"Spead", 10}},
WorldRules{{"Long", 1000}, {"Box", 4}, {"Spead", 12}},
WorldRules{{"Long", 2000}, {"Box", 8}, {"Spead", 14}},
WorldRules{{"Long", 4000}, {"Box", 16}, {"Spead", 18}},
WorldRules{{"Long", 8000}, {"Box", 32}, {"Spead", 26}}
WorldRules{{"Long", 500}, {"Box", 2}, {"Spead", 10}, {"BackGroundItem", 10}},
WorldRules{{"Long", 1000}, {"Box", 4}, {"Spead", 10}, {"BackGroundItem", 10}},
WorldRules{{"Long", 2000}, {"Box", 8}, {"Spead", 10}, {"BackGroundItem", 10}},
WorldRules{{"Long", 4000}, {"Box", 16}, {"Spead", 10}, {"BackGroundItem", 10}},
WorldRules{{"Long", 8000}, {"Box", 32}, {"Spead", 10}, {"BackGroundItem", 10}},
WorldRules{{"Long", 16000}, {"Box", 64}, {"Spead", 10}, {"BackGroundItem", 10}}
};

View File

@ -59,12 +59,54 @@ void Snake::setRataticonDistance(double value) {
rataticonDistance = value;
}
double Snake::sizeByLvl(double lvl , int count) const {
double maxSize = 9;
double minSize = 5;
double pos = (1 - (lvl / count));
QList<QPair<double, double>> snakeGradientSize {
{1, 4},
{0.9, 7},
{0.7, 5},
{0.5, 6},
{0.0, 3}
};
double localPos = 0;
int index = 0;
while (index + 1 < snakeGradientSize.size()
&& pos <= snakeGradientSize[index].first) {
maxSize = std::max(snakeGradientSize[index].second,
snakeGradientSize[index + 1].second);
minSize = std::min(snakeGradientSize[index].second,
snakeGradientSize[index + 1].second);
auto range = snakeGradientSize[index].first - snakeGradientSize[index + 1].first;
localPos = ( range - (snakeGradientSize[index].first - pos)) /
range;
index++;
}
return localPos * (maxSize - minSize) + minSize;
}
void Snake::changeCountObjects(int count) {
if (count > 0) {
double margin = 60.0 / count;
for ( int i = 0; i < count; ++i ) {
auto size = sizeByLvl(i, count);
auto obj = new Head(margin * (count - i),
50, 7, 7, this->speed);
50, size, size,
this->speed);
obj->setY(50 + obj->h() / 2);
items.push_back(obj);
}
@ -115,7 +157,7 @@ void Snake::resetPosotion() {
for ( int i = 0; i < items.size(); ++i ) {
items[i]->setX(margin * (items.size() - i));
items[i]->setY(50);
items[i]->setY(50 + items[i]->h() / 2);
items[i]->setAngle(0);
}
}

View File

@ -20,6 +20,7 @@ private:
bool isClick = false;
int countClick = 0;
bool dead = false;
double sizeByLvl(double lvl, int count) const;
void changeCountObjects(int count);
double checDistance(int i);

View File

@ -67,7 +67,7 @@ QMap<int, GuiObject *> World::init(WorldRules rules) {
QMap<int, GuiObject*> res;
rules["BackGround"] = 1;
// rules["BackGround"] = 1;
currentLong = -1;
for (auto i = rules.begin(); i != rules.end(); ++i) {
@ -83,7 +83,7 @@ QMap<int, GuiObject *> World::init(WorldRules rules) {
}
}
auto snakeItems = snake.init(10, &spead);
auto snakeItems = snake.init(20, &spead);
for (auto i = snakeItems.begin(); i != snakeItems.end(); ++i) {
res.insert(i.key(), i.value());

View File

@ -5,7 +5,7 @@ Rectangle {
property var model: null
property real angle: (model) ? model.angle : 0;
property string texture: (model) ? model.texture : "";
property int guiId: (model) ? model.color : -1;
property int guiId: (model) ? model.guiId : -1;
z:-1
property double devX: width / 2
@ -27,6 +27,8 @@ Rectangle {
x: (model) ? model.x * mainWindow.point - devX: 0;
y: (model) ? model.y * mainWindow.point - devY: 0;
radius: (model) ? model.radius * mainWindow.point : 0;
transform: Rotation {
origin.x: devX;
origin.y: devY;

View File

@ -28,8 +28,8 @@ Item {
spacing: 4
focusPolicy: Qt.StrongFocus
display: AbstractButton.TextBesideIcon
Layout.preferredHeight: 75
Layout.preferredWidth: 500
Layout.preferredHeight: item1.height / 5
Layout.preferredWidth: item1.height * 0.8
onClicked: {
paly();
@ -43,8 +43,8 @@ Item {
text: qsTr("Select level")
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
font.pixelSize: 20
Layout.preferredWidth: 500
Layout.preferredHeight: 75
Layout.preferredHeight: item1.height / 5
Layout.preferredWidth: item1.height * 0.8
spacing: 2
}
@ -53,8 +53,8 @@ Item {
text: qsTr("Exit")
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
font.pixelSize: 20
Layout.preferredHeight: 75
Layout.preferredWidth: 500
Layout.preferredHeight: item1.height / 5
Layout.preferredWidth: item1.height * 0.8
onClicked: {
Qt.quit();

View File

@ -6,6 +6,23 @@ import QtQuick.Layouts 1.3
Item {
id: scene;
z: -2
Rectangle {
id: background;
color: "#ffffff"
anchors.fill: parent;
Behavior on color {
ColorAnimation {
duration: 5000
}
}
z: -3
}
property var model: (contr)? contr: null;
property var arrayObjects: []
property bool showMenu: false
@ -14,16 +31,23 @@ Item {
console.log("create object fail")
return;
}
var objModel = model.getGameObject(cppObjId);
var temp = Qt.createComponent("GraphicItem.qml")
if (!objModel) {
console.log("object model not found");
return;
}
var viewTemplate = objModel.viewTemplate;
var temp = Qt.createComponent( viewTemplate + ".qml")
if (temp.status === Component.Ready) {
var obj = temp.createObject(parent) // parent - это обьект на который будет помещен соззданный элемент
obj.model = model.getGameObject(cppObjId);
if (!obj.model) {
console.log("object model not found");
return;
}
obj.z = -1;
arrayObjects.push(obj)
} else {
console.log("wrong viewTemplate in model");
}
}
@ -49,6 +73,21 @@ Item {
autoTimer.running = auto && model;
}
function updateBackgroundColor(lvl) {
switch(lvl % 7) {
case 0: background.color = "#d6eaf8"; break;
case 1: background.color = "#d0ece7"; break;
case 2: background.color = "#d4efdf"; break;
case 3: background.color = "#fcf3cf"; break;
case 4: background.color = "#f6ddcc"; break;
case 5: background.color = "#f2d7d5"; break;
case 6: background.color = "#ebdef0"; break;
case 7: background.color = "#fbfcfc"; break;
}
}
Timer {
id :autoTimer;
repeat: true;
@ -83,9 +122,11 @@ Item {
}
onFinished: {
var isVictory = victory;
var gameLvl = lvl + 1;
var dist = distance;
updateBackgroundColor(gameLvl);
if (isVictory ) {
@ -103,6 +144,10 @@ Item {
}
}
Component.onCompleted: {
updateBackgroundColor(0);
}
MouseArea {
anchors.fill: parent;

17
front-end/SnakeItem.qml Normal file
View File

@ -0,0 +1,17 @@
import QtQuick 2.9
GraphicItem {
Behavior on color {
ColorAnimation {
duration: 2000
}
}
Behavior on radius {
NumberAnimation {
duration: 2000
}
}
}

BIN
img/asphalt.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 186 KiB

View File

@ -7,10 +7,9 @@
<file>front-end/Scene.qml</file>
<file>front-end/Image.qml</file>
<file>front-end/NotificationForm.qml</file>
<file>front-end/SnakeItem.qml</file>
</qresource>
<qresource prefix="/texture">
<file alias="box-texture">img/box-texture.png</file>
<file alias="up">img/up.svg</file>
<file alias="settings">img/Settings-icon.png</file>
</qresource>
</RCC>

View File

@ -6,8 +6,8 @@
Style=Material
[Material]
Theme=Dark
Theme=Light
#Accent=#5840FF
#Primary=#FFF600
#Foreground=#142ECC
#Background=#61CC14
Background=#eeeeee

View File

@ -26,7 +26,8 @@ SOURCES += \
back-end/guiobjectfactory.cpp \
back-end/utils.cpp \
back-end/diff.cpp \
back-end/background.cpp
back-end/background.cpp \
back-end/backgrounditem.cpp
RESOURCES += qml.qrc
@ -54,7 +55,8 @@ HEADERS += \
back-end/lvls.h \
back-end/guiobjectfactory.h \
back-end/diff.h \
back-end/background.h
back-end/background.h \
back-end/backgrounditem.h
DISTFILES += \
doc/calassdiagramm.qmodel \