-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
150 lines (124 loc) · 3.53 KB
/
Copy pathgame.cpp
File metadata and controls
150 lines (124 loc) · 3.53 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "game.h"
#include <QQmlContext>
#include <QTimer>
Game::Game() : QObject(), started(0), points(0), bestpoints(0), deaths(0)
{
connect(this, SIGNAL(start()), this, SLOT(init()));
connect(this, SIGNAL(close()), this, SLOT(shutdown()));
fish = new Fish(300,300, 1, 10);
fish->setDestPoint(300,150);
worm = new Worm(0,0,4,3);
booster = new Booster(0,0,0);
engine = new QQmlApplicationEngine(this);
}
Game::~Game()
{
delete fish;
delete worm;
delete booster;
delete engine;
}
void Game::startGame()
{
init();
}
void Game::init()
{
engine->rootContext()->setContextProperty("fish", fish);
engine->rootContext()->setContextProperty("worm", worm);
engine->rootContext()->setContextProperty("booster", booster);
engine->rootContext()->setContextProperty("game", this);
engine->load(QUrl(QStringLiteral("qrc:/main.qml")));
//TODO: connect signals
connect(engine->rootObjects()[0], SIGNAL(mouseClicked(double,double)), this, SLOT(onMouseClicked(double,double)));
connect(engine->rootObjects()[0], SIGNAL(updated(double,double)), this, SLOT(onUpdated(double,double)));
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(render()));
timer->start(30);
QTimer *timer2 = new QTimer(this);
connect(timer2, SIGNAL(timeout()), this, SLOT(update()));
timer2->start(1000);
}
void Game::render()
{
if(started)
{
if(fish->check())
{
started = 0;
points = 0;
deaths++;
worm->toDefault();
booster->toDefault();
fish->toDefault();
emit statChanged();
}
else
{
if(worm->check(booster->getX(), booster->getY()))
{
worm->setBoost(worm->getBoost() + booster->getValue());
worm->setTime(15);
booster->toDefault();
booster->setXY(rand() % 600, rand() % 600);
booster->setValue(1);
booster->reset();
points += 100;
if(points >= bestpoints)
bestpoints += 100;
}
if(worm->check())
{
}
else
{
worm->move();
fish->setDestPoint(worm->getX(), worm->getY());
}
fish->move();
emit statChanged();
}
}
else
{
if(fish->check())
fish->setDestPoint(rand() % 600, rand() % 600);
else
fish->move();
}
}
void Game::shutdown()
{
}
void Game::update()
{
if(started)
{
points += fish->getVelocity() * 10;
if(points >= bestpoints)
bestpoints += fish->getVelocity() * 10;
fish->setVelocity(fish->getVelocity() + 0.1);
if(worm->getTime() > 0)
worm->setTime(worm->getTime() - 1);
else
worm->setBoost(0);
}
}
void Game::onMouseClicked(double x, double y)
{
if(!started)
{
started = true;
worm->setXY(x,y);
worm->setDestPoint(x,y);
worm->reset();
booster->setXY(rand() % 600, rand() % 600);
booster->setValue(1);
booster->reset();
fish->setDestPoint(worm->getX(), worm->getY());
}
}
void Game::onUpdated(double x, double y)
{
worm->setDestPoint(x, y);
}