mirror of
https://github.com/QuasarApp/Snake.git
synced 2025-05-10 00:19:45 +00:00
added texture
This commit is contained in:
parent
e2dfdb82b9
commit
36cfd6fa96
18
back-end/background.cpp
Normal file
18
back-end/background.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
#include "background.h"
|
||||
|
||||
BackGround::BackGround(double x, double y): ItemWorld (x, y) {
|
||||
this->setSize(200, 4000);
|
||||
this->setTexture("qrc:/texture/box-texture");
|
||||
setBeckGroundObject(true);
|
||||
}
|
||||
|
||||
void BackGround::render() {
|
||||
if (m_x + w() < 0) {
|
||||
m_x = w() * 0.5;
|
||||
emit xChanged(m_x);
|
||||
}
|
||||
}
|
||||
|
||||
void BackGround::reset() {
|
||||
setX(-10 - (m_x + w()));
|
||||
}
|
16
back-end/background.h
Normal file
16
back-end/background.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef BACKGROUND_H
|
||||
#define BACKGROUND_H
|
||||
|
||||
#include "itemworld.h"
|
||||
|
||||
|
||||
class BackGround : public ItemWorld
|
||||
{
|
||||
public:
|
||||
BackGround(double x, double y);
|
||||
|
||||
void render() override;
|
||||
void reset() override;
|
||||
};
|
||||
|
||||
#endif // BACKGROUND_H
|
@ -46,6 +46,10 @@ void Controller::generateDiff(const QMap<int, GuiObject *>& objs) {
|
||||
}
|
||||
|
||||
void Controller::update() {
|
||||
if (pause) {
|
||||
return;
|
||||
}
|
||||
|
||||
world.render();
|
||||
if(world.isDefiat()) {
|
||||
stopTimer();
|
||||
@ -90,3 +94,7 @@ void Controller::buttonPress() {
|
||||
world.reversClick();
|
||||
}
|
||||
|
||||
void Controller::setPause(bool p){
|
||||
pause = p;
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,7 @@ class Controller : public QObject
|
||||
private:
|
||||
World world;
|
||||
QTimer *timer;
|
||||
bool pause = false;
|
||||
int lvl = 0;
|
||||
QMap<int, GuiObject *> objectsContainer;
|
||||
|
||||
@ -30,6 +31,9 @@ public:
|
||||
|
||||
public slots:
|
||||
void buttonPress();
|
||||
|
||||
void setPause(bool);
|
||||
|
||||
void update();
|
||||
|
||||
/**
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "box.h"
|
||||
#include "utils.h"
|
||||
#include "head.h"
|
||||
#include "background.h"
|
||||
|
||||
GuiObjectFactory::GuiObjectFactory() {}
|
||||
|
||||
@ -10,6 +11,9 @@ ItemWorld *GuiObjectFactory::generate(const QString &name) {
|
||||
if (name == "Box") {
|
||||
obj = new Box(rand() % 400, 0);
|
||||
}
|
||||
else if (name == "BackGround") {
|
||||
obj = new BackGround(0, 0);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ void Head::render() {
|
||||
qint64 tempTime = QDateTime::currentMSecsSinceEpoch() - time;
|
||||
time = QDateTime::currentMSecsSinceEpoch();
|
||||
|
||||
double my = (m_y + (*speed * 0.75) * sin(m_angle * TO_RADIAN));
|
||||
double my = (m_y + (*speed * 0.55) * sin(m_angle * TO_RADIAN));
|
||||
m_y += (my - m_y) / 1000 * tempTime;
|
||||
|
||||
emit yChanged(m_y);
|
||||
|
@ -17,7 +17,7 @@ public:
|
||||
ItemWorld(double x, double y);
|
||||
|
||||
void render();
|
||||
bool move(const GuiObject* snakeRiger, double dx);
|
||||
virtual bool move(const GuiObject* snakeRiger, double dx);
|
||||
bool isBeckGroundObject();
|
||||
|
||||
~ItemWorld();
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "snake.h"
|
||||
#include "guiobject.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QMap>
|
||||
#include <QRectF>
|
||||
#include <cmath>
|
||||
@ -13,8 +14,14 @@ const QVector<Head *> &Snake::getItems() const {
|
||||
}
|
||||
|
||||
void Snake::render() {
|
||||
|
||||
for (int i = items.length() - 1; i >= 0; --i) {
|
||||
|
||||
if (dead) {
|
||||
items[i]->render();
|
||||
continue;
|
||||
}
|
||||
|
||||
if(i == 0) {
|
||||
if(isClick){
|
||||
if(countClick & 1){
|
||||
@ -86,6 +93,9 @@ QMap<int, GuiObject*> Snake::init(int size, double *speed) {
|
||||
for (auto i : items) {
|
||||
res[i->guiId()] = i;
|
||||
}
|
||||
|
||||
dead = false;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -110,6 +120,14 @@ void Snake::resetPosotion() {
|
||||
}
|
||||
}
|
||||
|
||||
void Snake::kill() {
|
||||
dead = true;
|
||||
}
|
||||
|
||||
bool Snake::isDead() const {
|
||||
return dead;
|
||||
}
|
||||
|
||||
Snake::~Snake() {
|
||||
clearItems();
|
||||
}
|
||||
|
@ -9,6 +9,8 @@
|
||||
|
||||
class GuiObject;
|
||||
|
||||
|
||||
|
||||
class Snake : public BaseClass
|
||||
{
|
||||
private:
|
||||
@ -17,6 +19,7 @@ private:
|
||||
double *speed = nullptr;
|
||||
bool isClick = false;
|
||||
int countClick = 0;
|
||||
bool dead = false;
|
||||
void changeCountObjects(int count);
|
||||
|
||||
double checDistance(int i);
|
||||
@ -29,6 +32,9 @@ public:
|
||||
void clear();
|
||||
void resetPosotion();
|
||||
|
||||
void kill();
|
||||
bool isDead() const;
|
||||
|
||||
void reverse();
|
||||
void render() override;
|
||||
QMap<int, GuiObject *> init(int size, double *speed);
|
||||
@ -37,6 +43,8 @@ public:
|
||||
double getMovedLong() const;
|
||||
double getRataticonDistance() const;
|
||||
void setRataticonDistance(double value);
|
||||
int getDeadTimer() const;
|
||||
void setDeadTimer(int value);
|
||||
};
|
||||
|
||||
#endif // SNAKE_H
|
||||
|
@ -1,3 +1,4 @@
|
||||
#include "background.h"
|
||||
#include "world.h"
|
||||
|
||||
#include <QMap>
|
||||
@ -21,6 +22,11 @@ double World::getCurrentLong() const {
|
||||
return currentLong;
|
||||
}
|
||||
|
||||
QMultiMap<QString, ItemWorld *> World::getItems() const
|
||||
{
|
||||
return items;
|
||||
}
|
||||
|
||||
void World::clearItems() {
|
||||
for (auto i : items) {
|
||||
delete i;
|
||||
@ -43,22 +49,26 @@ void World::changeCountObjects(const QString &name, int count) {
|
||||
break;
|
||||
}
|
||||
|
||||
items.push_back(obj);
|
||||
items.insertMulti(name, obj);
|
||||
}
|
||||
|
||||
} else {
|
||||
for ( int i = count; i < 0; ++i ) {
|
||||
auto obj = items.first();
|
||||
items.removeFirst();
|
||||
auto obj = items.value(name);
|
||||
if (1 != items.remove(name, obj)) {
|
||||
qWarning() << "World::changeCountObjects error delete object!";
|
||||
}
|
||||
delete obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QMap<int, GuiObject *> World::init(const WorldRules &rules) {
|
||||
QMap<int, GuiObject *> World::init(WorldRules rules) {
|
||||
|
||||
QMap<int, GuiObject*> res;
|
||||
|
||||
rules["BackGround"] = 1;
|
||||
|
||||
currentLong = -1;
|
||||
for (auto i = rules.begin(); i != rules.end(); ++i) {
|
||||
if (i.key() == "Long") {
|
||||
@ -87,7 +97,6 @@ QMap<int, GuiObject *> World::init(const WorldRules &rules) {
|
||||
oldRules = rules;
|
||||
time = QDateTime::currentMSecsSinceEpoch();
|
||||
defiat = false;
|
||||
spead = 0;
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -109,11 +118,16 @@ void World::render() {
|
||||
snake.render();
|
||||
auto rig = snake.getItems().first();
|
||||
|
||||
for (int i = items.length() - 1; i >= 0; --i) {
|
||||
defiat |= items[i]->move(rig, dx);
|
||||
items[i]->render();
|
||||
for (auto i = items.begin(); i != items.end(); ++i) {
|
||||
defiat |= (*i)->move(rig, dx);
|
||||
(*i)->render();
|
||||
}
|
||||
|
||||
defiat |= (rig->y()< 0 || rig->y() > 100);
|
||||
|
||||
if (!snake.isDead() && defiat) {
|
||||
snake.kill();
|
||||
}
|
||||
|
||||
currentLong += dx;
|
||||
}
|
||||
@ -122,6 +136,8 @@ void World::resetPosition() {
|
||||
for (auto i : items) {
|
||||
i->reset();
|
||||
}
|
||||
spead = 0;
|
||||
|
||||
snake.resetPosotion();
|
||||
}
|
||||
|
||||
@ -134,7 +150,7 @@ bool World::isEnd() {
|
||||
}
|
||||
|
||||
bool World::isDefiat() const {
|
||||
return defiat;
|
||||
return defiat && !static_cast<bool>(spead);
|
||||
}
|
||||
|
||||
WorldRules World::currentRules() const {
|
||||
@ -142,10 +158,11 @@ WorldRules World::currentRules() const {
|
||||
}
|
||||
|
||||
void World::reversClick() {
|
||||
if (snake.isDead()) {
|
||||
spead = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
snake.reverse();
|
||||
spead += d_spead;
|
||||
}
|
||||
|
||||
const QVector<ItemWorld *> &World::getItems() const {
|
||||
return items;
|
||||
}
|
||||
|
@ -14,7 +14,8 @@ class World : public BaseClass
|
||||
private:
|
||||
|
||||
Snake snake;
|
||||
QVector<ItemWorld*> items;
|
||||
// QVector<ItemWorld*> items;
|
||||
QMultiMap<QString, ItemWorld*> items;
|
||||
double currentLong = 0;
|
||||
int endLong;
|
||||
double spead = 0, d_spead = 0;
|
||||
@ -30,17 +31,17 @@ private:
|
||||
public:
|
||||
World();
|
||||
void clear();
|
||||
QMap<int, GuiObject*> init(const WorldRules &rules);
|
||||
QMap<int, GuiObject*> init(WorldRules rules);
|
||||
~World() override;
|
||||
void render() override;
|
||||
void resetPosition();
|
||||
bool move();
|
||||
bool isEnd();
|
||||
const QVector<ItemWorld*>& getItems() const;
|
||||
bool isDefiat() const;
|
||||
WorldRules currentRules() const;
|
||||
void reversClick();
|
||||
double getCurrentLong() const;
|
||||
QMultiMap<QString, ItemWorld *> getItems() const;
|
||||
};
|
||||
|
||||
#endif // WORLD_H
|
||||
|
@ -7,9 +7,17 @@ Rectangle {
|
||||
property string texture: (model) ? model.texture : "";
|
||||
property int guiId: (model) ? model.color : -1;
|
||||
|
||||
z:-1
|
||||
property double devX: width / 2
|
||||
property double devY: height / 2
|
||||
|
||||
Image {
|
||||
id: name
|
||||
visible: texture.length
|
||||
source: texture
|
||||
|
||||
anchors.fill: parent;
|
||||
}
|
||||
|
||||
color: (model) ? model.color : "#11ff32";
|
||||
|
||||
|
@ -132,5 +132,7 @@ Popup {
|
||||
}
|
||||
}
|
||||
}
|
||||
closePolicy: Popup.NoAutoClose
|
||||
|
||||
|
||||
}
|
||||
|
@ -139,6 +139,7 @@ Item {
|
||||
|
||||
anchors.top: parent.top
|
||||
anchors.topMargin: point
|
||||
z: 1
|
||||
|
||||
visible: !showMenu
|
||||
}
|
||||
@ -153,6 +154,7 @@ Item {
|
||||
|
||||
anchors.top: parent.top
|
||||
anchors.topMargin: point
|
||||
z: 1
|
||||
|
||||
visible: !showMenu
|
||||
|
||||
@ -178,6 +180,7 @@ Item {
|
||||
|
||||
anchors.top: parent.top
|
||||
anchors.topMargin: point
|
||||
z: 1
|
||||
|
||||
visible: !showMenu
|
||||
|
||||
|
@ -6,8 +6,8 @@
|
||||
Style=Material
|
||||
|
||||
[Material]
|
||||
Theme=Light
|
||||
Accent=#5840FF
|
||||
Primary=#FFF600
|
||||
Foreground=#142ECC
|
||||
Background=#61CC14
|
||||
Theme=Dark
|
||||
#Accent=#5840FF
|
||||
#Primary=#FFF600
|
||||
#Foreground=#142ECC
|
||||
#Background=#61CC14
|
||||
|
@ -25,7 +25,8 @@ SOURCES += \
|
||||
back-end/lvls.cpp \
|
||||
back-end/guiobjectfactory.cpp \
|
||||
back-end/utils.cpp \
|
||||
back-end/diff.cpp
|
||||
back-end/diff.cpp \
|
||||
back-end/background.cpp
|
||||
|
||||
RESOURCES += qml.qrc
|
||||
|
||||
@ -52,7 +53,8 @@ HEADERS += \
|
||||
back-end/utils.h \
|
||||
back-end/lvls.h \
|
||||
back-end/guiobjectfactory.h \
|
||||
back-end/diff.h
|
||||
back-end/diff.h \
|
||||
back-end/background.h
|
||||
|
||||
DISTFILES += \
|
||||
doc/calassdiagramm.qmodel \
|
||||
|
Loading…
x
Reference in New Issue
Block a user