forked from believer2/Terris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.hpp
More file actions
136 lines (117 loc) · 2.98 KB
/
Block.hpp
File metadata and controls
136 lines (117 loc) · 2.98 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
#include<bits/stdc++.h>
using namespace std;
#include<graphics.h>
struct Point {
int row;
int col;
};
class Block
{
private:
int blockType;
Point blockLocation[4];
IMAGE *img;
static IMAGE* imgs[7];
static int size;
public:
Block(/* args */);
~Block();
void drop();//下降
void move_leftorright(int offset);//左右移动
void rotate();//旋转
void draw(int left,int top);
static IMAGE** getImages();
Block& operator=(const Block& other);
bool blockInMap(const vector<vector<int>> &map);
void solidify(vector<vector<int>> &map);//“固化”
int getBlockType();
};
Block& Block::operator=(const Block& other)
{
if (this == &other) return *this;
this->blockType = other.blockType;
for (int i = 0; i < 4; i++) {
this->blockLocation[i] = other.blockLocation[i];
}
return *this;
}
IMAGE* Block::imgs[7] = {NULL};
int Block::size = 36;
Block::Block(){
if (imgs[0] == NULL) {
IMAGE imgTmp;
loadimage(&imgTmp, "res/tiles.png");
SetWorkingImage(&imgTmp);
for (int i = 0; i < 7; i++) {
imgs[i] = new IMAGE;
getimage(imgs[i], i * size, 0, size, size);
}
SetWorkingImage();//恢复工作区
srand(time(NULL));
}
int blocks[7][4] = {
1,3,5,7, // I
2,4,5,7, // Z 1型
3,5,4,6, // Z 2型
3,5,4,7, // T
2,3,5,7, // L
3,5,7,6, // J
2,3,4,5, // 田
};
blockType = rand()%7+1;
for(int i = 0;i<4;i++){
blockLocation[i].row = blocks[blockType-1][i]/2;
blockLocation[i].col = blocks[blockType-1][i]%2;
}
img = imgs[blockType-1];
}
void Block::drop(){
for(int i = 0;i<4;i++){
blockLocation[i].row++;
}
}
void Block::draw(int left,int top){
for(int i = 0;i<4;i++){
int x = left + blockLocation[i].col*size;
int y = top + blockLocation[i].row*size;
putimage(x,y,img);
}
}
bool Block::blockInMap(const vector<vector<int>> &map){
int rows = map.size();
int cols = map[0].size();
for(int i = 0;i<4;i++){
if(blockLocation[i].col<0 || blockLocation[i].col>=cols ||
blockLocation[i].row<0 || blockLocation[i].row>=rows ||
map[blockLocation[i].row][blockLocation[i].col]){
return false;
}
}
return true;
}
void Block::solidify(vector<vector<int>> &map){
for(int i =0;i<4;i++){
map[blockLocation[i].row][blockLocation[i].col] = blockType;
}
}
void Block::move_leftorright(int offset){ //左右移动
for (int i = 0; i < 4; i++) {
blockLocation[i].col += offset;
}
}
void Block::rotate(){//旋转
Point p = blockLocation[1];
for(int i = 0;i<4;i++){
Point temp = blockLocation[i];
blockLocation[i].row = p.row + temp.col - p.col;
blockLocation[i].col = p.col - temp.row + p.row;
}
}
int Block::getBlockType(){
return blockType;
}
IMAGE** Block::getImages(){
return imgs;
}
Block::~Block(){
}