forked from Pomidorry/arkanoid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
248 lines (225 loc) · 4.36 KB
/
game.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <string>
#include <iostream>
#include <cmath>
#include <vector>
#include "Framework.h"
/* Test Framework realization */
struct Point {
Point() {};
Point(int x1, int y1) {
x = x1;
y = y1;
};
double x;
double y;
};
struct Vector {
double xV;
double yV;
Vector(Point B, Point A, int l) {
xV = (B.x - A.x)/l;
yV = (B.y - A.y)/l;
}
};
class Ball {
public:
Ball() {
position.x = 380;
position.y = 530;
};
void SetPath(const char* path) {
texture = createSprite(path);
};
void DrawBall() {
setSpriteSize(texture, width, height);
drawSprite(texture, position.x, position.y);
};
void Destroy() {
destroySprite(texture);
};
void setIdle(bool on) {
isIdle = on;
}
bool getIdle() {
return isIdle;
}
void SetVelocityDirection(int xMouse, int yMouse) {
if (xMouse > 400) {
yVelocity = -2;
xVelocity = 2;
}
else {
yVelocity = -2;
xVelocity = -2;
}
};
void SetVelocity(int dx, int dy) {
xVelocity *= dx;
yVelocity *= dy;
}
void SetPosition(Point p) {
position = p;
}
void Move() {
position.x += xVelocity;
position.y += yVelocity;
if (position.x >= 800 || position.x <= 0) {
xVelocity *= -1;
}
if (position.y <= 0) {
yVelocity *= -1;
}
};
Point GetPosition() {
return position;
};
private:
int width = 20;
int height = 20;
Point position;
Sprite* texture;
double xVelocity=0;
double yVelocity = 0;
bool isIdle = false;
};
class Colider {
public:
Colider(int w, int h, Point p) {
width = w;
height = h;
position = p;
}
bool isColide(Point p) {
if (p.x > position.x-20 && p.x < position.x + 100+20 && p.y==position.y) {
return 1;
}
else {
return 0;
}
}
Vector Colide(Point curr,Point prev) {
int a = abs(prev.x - curr.x);
int b = abs(prev.y - curr.y);
double c = sqrt(a * a + b * b);
Point A (prev.x, 600-50);
int pA = abs(prev.y-600+50);
int k = pA * c / b;
double BA = sqrt(k * k + pA ^ 2);
Point P(prev.x + 2 * BA, prev.y);
Point B(A.x + BA, A.y);
Vector norm(P, B, k);
return norm;
}
void ColiderUpdate(Point p) {
position = p;
}
private:
int width;
int height;
Point position;
};
class Platform {
public:
Platform() {
position.x = 350;
position.y = 550;
}
void SetPath(const char* path) {
texture = createSprite(path);
};
void DrawPlatform() {
setSpriteSize(texture, width, height);
drawSprite(texture, position.x, position.y);
}
void MovePlatform(FRKey k) {
if (k == FRKey::RIGHT) {
position.x += 50;
}
if (k == FRKey::LEFT) {
position.x -= 50;
}
}
void Collision() {
if (position.x < 0) {
position.x = 0;
}
if (position.x > 700) {
position.x = 700;
}
}
void Destroy() {
destroySprite(texture);
};
void PlatformUpdate() {
SetPath("C:\\17-Breakout-Tiles.png");
Collision();
DrawPlatform();
Destroy();
};
Point GetPosition() {
return position;
}
Colider* myColider = new Colider(100, 50, GetPosition());
private:
int width = 100;
int height = 50;
Point position;
Sprite* texture;
};
class MyFramework : public Framework {
public:
virtual void PreInit(int& width, int& height, bool& fullscreen)
{
width = 800;
height = 600;
fullscreen = false;
}
virtual bool Init() {
return true;
}
virtual void Close() {
}
virtual void onKeyPressed(FRKey k) {
platform->MovePlatform(k);
}
virtual bool Tick() {
platform->PlatformUpdate();
platform->myColider->ColiderUpdate(platform->GetPosition());
ball->SetPath("C:\\13-Breakout-Tiles.png");
Point prev = ball->GetPosition();
ball->Move();
Point curr = ball->GetPosition();
if (platform->myColider->isColide(curr)) {
std::cout << 1;
ball->SetPosition(prev);
ball->SetVelocity(platform->myColider->Colide(curr, prev).xV, platform->myColider->Colide(curr, prev).yV);
}
ball->DrawBall();
ball->Destroy();
return false;
}
virtual void onMouseMove(int x, int y, int xrelative, int yrelative) {
if (ball->getIdle()) {
ball->SetVelocityDirection(x, y);
ball->setIdle(false);
}
}
virtual void onMouseButtonClick(FRMouseButton button, bool isReleased) {
if (button == FRMouseButton::LEFT && isReleased) {
ball->setIdle(isReleased);
}
}
virtual void onKeyReleased(FRKey k) {
}
virtual const char* GetTitle() override
{
return "Arcanoid";
}
private:
Platform* platform=new Platform();
Ball* ball = new Ball();
};
int main(int argc, char *argv[])
{
return run(new MyFramework);
}