本文实例为大家分享了C++俄罗斯方块游戏的具体实现代码,供大家参考,具体内容如下、
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
|
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
#define MOD 28
#define SIZE_N 19
#define SIZE_M 12
int cur_x,cur_y;
int score,mark,next,map[SIZE_N][SIZE_M],Gamespeed=300;
int shape[28][6]={
{0,-1,0,-2,1,0}, {0,1,1,0,2,0}, {-1,0,0,1,0,2}, {0,-1,-1,0,-2,0},
{0,-1,0,1,-1,0}, {0,1,1,0,-1,0}, {1,0,0,-1,0,1}, {1,0,-1,0,0,-1},
{-1,1,0,1,1,0}, {0,-1,1,0,1,1}, {-1,0,0,-1,1,-1}, {-1,-1,-1,0,0,1},
{-1,0,0,1,1,1}, {0,1,1,-1,1,0}, {-1,0,0,1,1,1}, {0,1,1,-1,1,0},
{-1,0,0,-1,0,-2}, {-1,0,-2,0,0,1}, {0,1,0,2,1,0}, {0,-1,1,0,2,0},
{0,1,1,0,1,1}, {0,-1,1,0,1,-1}, {-1,0,0,-1,-1,-1}, {-1,0,-1,1,0,1},
{0,1,0,2,0,3}, {1,0,2,0,3,0}, {0,-1,0,-2,0,-3}, {-1,0,-2,0,-3,0}
};
void gotoxy( int x, int y){
COORD c;
c.X=x-1; c.Y=y-1;
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void Gameover(){
int i,j,flag=0;
for (j=1;j<SIZE_M-1;j++){
if (map[1][j]!=0){
flag=1; break ;
}
}
if (flag==1){
for (i=1;i<SIZE_N-1;i++){
gotoxy(2,i+1);
for (j=1;j<SIZE_M-1;j++){
printf ( "□" );
} puts ( "" );
}
gotoxy(7,9);
printf ( "GAME OVER!" );
gotoxy(1,SIZE_N+1);
exit (0);
}
}
void ShowMap( int id){
int i,j;
gotoxy(1,1);
if (id!=-1){
for (i=0;i<SIZE_N;i++){
for (j=0;j<SIZE_M;j++){
if (i==0&&j==0 || i==0&&j==SIZE_M-1 || j==0&&i==SIZE_N-1 || j==SIZE_M-1&&i==SIZE_N-1) printf ( " " );
else if (i==0 || i==SIZE_N-1) printf ( "--" );
else if (j==0 || j==SIZE_M-1) printf ( "|" );
else if (map[i][j]==2) printf ( "■" );
else if (i==cur_x+shape[id][0] && j==cur_y+shape[id][1] ||
i==cur_x+shape[id][2] && j==cur_y+shape[id][3] ||
i==cur_x+shape[id][4] && j==cur_y+shape[id][5] ||
i==cur_x && j==cur_y)
printf ( "■" );
else if (map[i][j]==0) printf ( " " );
}
if (i==1) printf ( " 下一个 :" );
if (i==11) printf ( " 等分 : %d" ,score);
if (i==14) printf ( " 速度 : %d" ,score/100+1);
puts ( "" );
}
}
else {
mark=1;
for (i=0;i<SIZE_N;i++){
for (j=0;j<SIZE_M;j++){
if (i==0&&j==0 || i==0&&j==SIZE_M-1 || j==0&&i==SIZE_N-1 || j==SIZE_M-1&&i==SIZE_N-1) printf ( " " );
else if (i==0 || i==SIZE_N-1) printf ( "--" );
else if (j==0 || j==SIZE_M-1) printf ( "|" );
else if (map[i][j]==2) printf ( "■" );
else if (map[i][j]==0) printf ( " " );
}
if (i==1) printf ( " next:" );
if (i==11) printf ( " score : %d" ,score);
if (i==14) printf ( " speed : %d" ,score/100+1);
puts ( "" );
}
}
gotoxy(30,6); printf ( " " );
for (i=0;i<6;i=i+2){
gotoxy(30+2*shape[id][i+1],6+shape[id][i]); printf ( " " );
}
gotoxy(30,6); printf ( "■" );
for (i=0;i<6;i=i+2){
gotoxy(30+2*shape[next][i+1],6+shape[next][i]); printf ( "■" );
}
Sleep(Gamespeed);
}
void init( int id){
int i,j;
memset (map,0, sizeof (map));
for (i=0;i<SIZE_N;i++){
for (j=0;j<SIZE_M;j++)
if (i==SIZE_N-1 || j==0 || j==SIZE_M-1)
map[i][j]=-1;
}
cur_x=0; cur_y=5;
ShowMap(id);
}
int judge_in( int x, int y, int id){
int i;
if (map[x][y]!=0) return 0;
for (i=0;i<6;i=i+2){
if (map[ x+shape[id][i] ][ y+shape[id][i+1] ]!=0) return 0;
} return 1;
}
void fun_score(){
int i,j,ii,jj;
for (i=1;i<SIZE_N-1;i++){
int flag=0;
for (j=1;j<SIZE_M-1;j++){
if (map[i][j]!=2){ flag=1; break ; }
}
if (flag==0){
int k=3;
while (k--){
gotoxy(2,i+1);
for (ii=1;ii<SIZE_M-1;ii++){
if (map[i][ii]==2){
if (k%2==1) printf ( " " );
else printf ( "■" );
}
}Sleep(100);
}
for (ii=i;ii>1;ii--){
for (jj=1;jj<SIZE_M-1;jj++) map[ii][jj]=map[ii-1][jj];
}
ShowMap(-1);
score+=10;
if (score%100==0 && score!=0)Gamespeed-=50;
}
}
}
int main(){
int i,id,set=1;
srand ( time (NULL));
id= rand ()%MOD; id=(id+MOD)%MOD;
next= rand ()%MOD; next=(next+MOD)%MOD;
init(id);
while (1){
Here: mark=0;
if (set==0){
id=next;
next= rand ()%MOD; next=(next+MOD)%MOD;
cur_x=0;cur_y=5;
set=1;
}
while (!kbhit()){
Gameover();
if (judge_in(cur_x+1,cur_y,id)==1) cur_x++;
else {
map[cur_x][cur_y]=2;
for (i=0;i<6;i=i+2)
map[ cur_x+shape[id][i] ][ cur_y+shape[id][i+1] ]=2;
fun_score();
set=0;
}
if (mark!=1)ShowMap(id);
goto Here;
}
char key;
key=getch();
if (key==72){
int tmp=id;
id++;
if ( id%4==0 && id!=0 )id=id-4;
if (judge_in(cur_x,cur_y,id)!=1)id=tmp;
}
else if (key==80 && judge_in(cur_x+1,cur_y,id)==1)cur_x++;
else if (key==75 && judge_in(cur_x,cur_y-1,id)==1)cur_y--;
else if (key==77 && judge_in(cur_x,cur_y+1,id)==1)cur_y++;
else if (key==27){gotoxy(1,SIZE_N+1); exit (0);}
}
getch();
return 0;
}
|
以上就是本文的全部内容,希望对大家学习C++程序设计有所帮助。