俄羅斯方塊(c++)
阿新 • • 發佈:2018-12-10
1 void Diamond::create()
2 {
3 Frame F;
4 Tool T;
5 int key;
6 int base_point;
7 int rotate_time=0;
8 srand(time(NULL));
9 base_point = ( F.RIGHT-F.LEFT) / 2;
10 key = rand() % 5; // 隨機出形狀
11 rotate_time = rand() % 9; //隨機旋轉原始形狀,已達到豐富的目的
12 arrays.clear(); //為了降低難度,每個圖形出現的比例應該不一樣
13 switch(key)
14 {
15 case 0:
16 arrays.clear();
17 for (int i = 0; i < 4; ++i)
18 {
19 Unit unit(i + F.LEFT+base_point+1, F.UP + 1); //****型
20 arrays.push_back(unit);
21 }
22 for (int i = 0; i < rotate_time; ++i)
23 {
24 rotate();
25 }
26 break;
27 case 1:case 5:case 6:
28 arrays.clear();
29 for (int i = 0; i < 2; ++i)
30 {
31 for (int j = 0; j < 2; ++j)
32 {
33 Unit unit(i + F.LEFT + base_point + 1, F.UP + 1 + j); //正方形
34 arrays.push_back(unit);
35 }
36 }
37 break;
38 case 2:case 7: //槍形
39 arrays.clear();
40 arrays.push_back(Unit(F.LEFT + base_point + 1, F.UP + 1));
41 arrays.push_back(Unit (F.LEFT + base_point+2, F.UP + 1));
42 arrays.push_back(Unit (F.LEFT + base_point + 3, F.UP + 1));
43 arrays.push_back(Unit(F.LEFT + base_point + 3, F.UP + 2));
44 for (int i = 0; i < rotate_time; ++i)
45 {
46 rotate();
47 }
48 break;
49 case 3: //梯形
50 arrays.clear();
51 arrays.push_back(Unit(F.LEFT + base_point + 1, F.UP + 1));
52 arrays.push_back(Unit(F.LEFT + base_point+1, F.UP + 2));
53 arrays.push_back(Unit(F.LEFT + base_point + 2, F.UP + 2));
54 arrays.push_back(Unit(F.LEFT + base_point + 2, F.UP + 3));
55 for (int i = 0; i < rotate_time; ++i)
56 {
57 rotate();
58 }
59 break;
60 case 4: case 8:
61 arrays.clear(); //城牆形
62 arrays.push_back(Unit(F.LEFT + base_point + 1, F.UP + 1));
63 arrays.push_back(Unit(F.LEFT + base_point + 2, F.UP + 1));
64 arrays.push_back(Unit(F.LEFT + base_point + 3, F.UP + 1));
65 arrays.push_back(Unit(F.LEFT + base_point + 2, F.UP + 2));
66 for (int i = 0; i < rotate_time; ++i)
67 {
68 rotate();
69 }
70 break;
71 }
72 //應該先將小螢幕清空。
73 for (int i = 0; i < F.RIGHT2 - F.RIGHT - 1; ++i)
74 {
75 for (int j = 0; j < F.MIDDLE1 - F.UP - 1; ++j)
76 {
77 T.gotoxy(F.RIGHT + 1 + i, F.UP + j + 1);
78 cout << " ";
79 }
80 }
81 for (int i = 0; i < 4; ++i)
82 {
83 Unit unit(F.RIGHT + 7 + arrays[i].get_x() - arrays[0].get_x(), F.UP + 4 + arrays[i].get_y() - arrays[0].get_y());
84 unit.show(); //按照實際的俄羅斯方塊,小螢幕放映的應該是下一個出現的方塊形狀,這裡先將功能簡單化
85 }
86 for (int i = 0; i < 4; ++i)
87 {
88 grid[arrays[i].get_x() - 1 - F.LEFT][arrays[i].get_y()-1-F.UP] = true;
89 }
90 }