-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.cpp
142 lines (130 loc) · 3.36 KB
/
box.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
#include "box.h"
#include "iostream"
#include "position.h"
#include "shapes.cpp"
using namespace std;
box::box(){};
box::box(int screenwidth, int screenheight) : colors{BLACK, ORANGE, PINK, BLUE, YELLOW, RED, PURPLE, GREEN, PINK, LIME, BROWN, DARKGRAY, LIGHTGRAY, MAGENTA, MAROON}, shapes{Ishape(), Jshape(), Lshape(), Oshape(), Sshape(), Tshape(), Zshape()}
{
screenheight = screenheight;
screenwidth = screenwidth;
l_x = screenwidth - 2 * padding;
l_y = screenheight - 2 * padding;
n_x = 10;
n_y = 20;
block_w = l_x / n_x;
block_h = l_y / n_y;
origin = position{padding, screenheight - padding};
for (int i = 0; i < n_x; i++)
for (int j = 0; j < n_y; j++)
arr[i][j] = 0;
spawn_falling();
}
box::~box()
{
}
void box::spawn_falling()
{
// std::cout << "spawning falling_shape ";
falling_shape = shapes[rand() % 7];
// std::cout << "spawning falling_shape ";
// falling_shape = shape{
// 1,
// // rand() % n_shapes,
// position{rand() % n_x, n_y},
// 0,
// colors[rand() % 13]};
}
void box::draw_block(position origin, position pos, Color color)
{
DrawRectangle(origin.x + block_w * pos.x, origin.y - block_h * (pos.y + 1) + 1, block_w - 1, block_h - 1, color);
}
void box::draw()
{
Color linecolor = RED;
Color gridcolor = RED;
Color boxcolor = BLACK;
DrawRectangle(origin.x, origin.y - l_y, l_x, l_y, linecolor);
DrawLine(origin.x, origin.y, origin.x + l_x, origin.y, linecolor);
DrawLine(origin.x, origin.y, origin.x, origin.y - l_y, linecolor);
DrawLine(origin.x + l_x, origin.y, origin.x + l_x, origin.y - l_y, linecolor);
DrawLine(origin.x, origin.y - l_y, origin.x + l_x, origin.y - l_y, linecolor);
// cout << origin.x << "; " << origin.y << "; " << origin.x + l_x << "; " << origin.y + l_y;
// cout << "drawing grids";
for (int i = 0; i < n_x; i++)
for (int j = 0; j < n_y; j++)
// cout << arr[i][j];
draw_block(origin, position{i, j}, colors[arr[i][j]]);
// cout << "drawing grids";
// DrawRectangle(origin.x + block_w * i , origin.y - block_h * (j + 1) + 1, block_w - 1, block_h - 1, boxcolor);
// for (auto i : arr)
// {
// // draw_block()
// i.draw(*this);
// }
}
void box ::draw_falling()
{
// cout << "trying to draw fall";
falling_shape.draw(*this);
};
void box::collision_horizontal()
{
for (position i : falling_shape.cells[falling_shape.rotation])
{
int x = falling_shape.pos.x + i.x;
int y = falling_shape.pos.y + i.y;
if (0 > x)
{
falling_shape.pos.x = falling_shape.pos.x + 1;
}
else if (x > n_x)
{
falling_shape.pos.x = falling_shape.pos.x - 1;
}
}
}
bool box::collision_vertical()
{
for (position i : falling_shape.cells[falling_shape.rotation])
{
int x = falling_shape.pos.x + i.x;
int y = falling_shape.pos.y + i.y;
if (arr[x][y] || 0 > y || y > n_y)
{
return true;
}
}
return false;
}
void box::fall()
{
falling_shape.fall();
if (collision_vertical())
{
falling_shape.pos = falling_shape.pos + position{0, 1};
add_to_arr();
spawn_falling();
}
};
void box::add_to_arr()
{
for (position i : falling_shape.cells[falling_shape.rotation])
{
arr[i.x + falling_shape.pos.x][i.y + falling_shape.pos.y] = falling_shape.color_id;
}
}
void box::rotate()
{
falling_shape.rotate();
}
void box::move_left()
{
falling_shape.pos = falling_shape.pos + position{-1, 0};
collision_horizontal();
}
void box::move_right()
{
falling_shape.pos = falling_shape.pos + position{1, 0};
collision_horizontal();
}