- # include <stdio.h>
- # include <windows.h>
- # include <conio.h>
- # include <time.h>
- # include <stdlib.h>
- # define Map 20
- # define FF for(i=0;i<fdl;i++)
- # define FFF for(j=0;j<fdl;j++)
- void gotoxy(int x, int y){
- COORD pos = {x,y};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
- }
- short target[Map][Map];
- short fdx[9],fdy[9],fdl;
- int score,speed,shape,cnt,shapes;
- void food(){
- shape = shapes;
- switch(shape)
- {
- case 1: fdx[0] = 10,fdy[0] = 2;//-
- fdl = 1;break;
- case 2: fdx[0] = 10,fdy[0] = 2;//--\\n --
- fdx[1] = 9,fdy[1] = 2;
- fdx[2] = 10,fdy[2] = 3;
- fdx[3] = 11,fdy[3] = 3;
- fdl = 4;break;
- case 3: fdx[0] = 10,fdy[0] = 2;//---
- fdx[1] = 9,fdy[1] = 2;
- fdx[2] = 11,fdy[2] = 2;
- fdl = 3;break;
- case 4: fdx[0] = 10,fdy[0] = 2;//--\\n--
- fdx[1] = 11,fdy[1] = 2;
- fdx[2] = 10,fdy[2] = 3;
- fdx[3] = 11,fdy[3] = 3;
- fdl = 4;break;
- case 5: fdx[0] = 10,fdy[0] = 3;//angle
- fdx[1] = 10,fdy[1] = 2;
- fdx[2] = 9,fdy[2] = 3;
- fdx[3] = 11,fdy[3] = 3;
- fdl = 4;break;
- case 6:
- case 7: fdx[0] = 10,fdy[0] = 2;
- fdx[1] = 9,fdy[1] = 2;
- fdx[2] = 11,fdy[2] = 2;
- fdx[3] = 11,fdy[3] = 3;
- if(shape == 7) fdx[3]-=2;
- fdl = 4;break;
- default:break;
- }
- cnt = -1;
- }
- void fod(){
- shapes = rand()%7+1;
- gotoxy(57,3),printf(" "),gotoxy(57,4),printf(" "),gotoxy(57,5),printf(" ");
- switch(shapes){
- case 1:gotoxy(59,4),printf("■");break;
- case 2:gotoxy(59,3),printf("■"),gotoxy(59,4),printf("■");
- gotoxy(57,3),printf("■"),gotoxy(61,4),printf("■");break;
- case 3:gotoxy(57,4),printf("■"),gotoxy(59,4),printf("■");
- gotoxy(61,4),printf("■");break;
- case 4:gotoxy(58,3),printf("■"),gotoxy(58,4),printf("■");
- gotoxy(60,3),printf("■"),gotoxy(60,4),printf("■");break;
- case 5:gotoxy(59,3),printf("■"),gotoxy(59,4),printf("■");
- gotoxy(57,4),printf("■"),gotoxy(61,4),printf("■");break;
- case 6:gotoxy(57,4),printf("■"),gotoxy(59,4),printf("■");
- gotoxy(61,4),printf("■");gotoxy(61,5),printf("■");break;
- case 7:gotoxy(57,4),printf("■"),gotoxy(59,4),printf("■");
- gotoxy(61,4),printf("■");gotoxy(57,5),printf("■");break;
- default:break;
- }
- }
- void show(){
- short i,j;
- for(i=0;i<Map;i++)
- for(j=0;j<Map;j++)
- if(target[i][j] == 2) gotoxy(i*2,j),puts("■");
- else if(target[i][j] == 1) gotoxy(i*2,j),puts("□");
- else gotoxy(i*2,j),puts(" ");
- gotoxy(0,3),puts("△"),gotoxy(Map*2-2,3),puts("△");
- gotoxy(58,8),printf("%d",score);
- }
- void init(){
- SetConsoleTitle("俄罗斯方块V1.0 by 流星不死");
- short i,j;
- for(i=0;i<Map;i++)
- for(j=0;j<Map;j++)
- if(i==0||i==Map-1||j==Map-1) target[i][j] = 1;
- else target[i][j] = 0;
- score = 0,speed = 210;
- fod(),food(),fod(),show();
- gotoxy(55,1),puts("下一图形");
- gotoxy(55,7),puts("你的得分");
- }
- int main(){
- short i,j,k,w,r,t;
- char ch;
- init();
- while(1){
- FF gotoxy(fdx[i]*2,fdy[i]),puts("■");
- Sleep(speed);
- FF gotoxy(fdx[i]*2,fdy[i]),puts(" ");
- k1:
- if(kbhit()){
- ch = getch();
- switch(ch){
- case 'a':FF if(target[fdx[i]-1][fdy[i]]!=0) goto k1;
- FFF fdx[j]--;break;
- case 'd':FF if(target[fdx[i]+1][fdy[i]]!=0) goto k1;
- FFF fdx[j]++;break;
- case 'w':if(1==fdx[0] || Map-2==fdx[0]) break;
- cnt++;
- switch(shape){
- case 2:switch(cnt%2){
- case 0:fdy[1]=fdy[2],fdx[1]=fdx[2];fdy[2]=fdy[0];
- fdx[2]=fdx[0]+1;fdy[3]=fdy[0]-1,fdx[3]=fdx[0]+1;break;
- case 1:fdy[2]=fdy[1],fdx[2]=fdx[1];fdy[1]=fdy[0];
- fdx[1]=fdx[0]-1;fdy[3]=fdy[0]+1,fdx[3]=fdx[0]+1;break;
- }break;
- case 3:switch(cnt%2){
- case 0:fdy[1]=fdy[0]-1,fdx[1]=fdx[0];fdy[2]=fdy[0]+1,fdx[2]=fdx[0];break;
- case 1:fdy[1]=fdy[0],fdx[1]=fdx[0]+1;fdy[2]=fdy[0],fdx[2]=fdx[0]-1;break;
- }break;
- case 5:fdy[3]=fdy[1],fdx[3]=fdx[1];fdy[1]=fdy[2],fdx[1]=fdx[2];
- switch(cnt%4){
- case 0:fdy[2]=fdy[0]+1,fdx[2]=fdx[0];break;
- case 1:fdy[2]=fdy[0],fdx[2]=fdx[0]+1;break;
- case 2:fdy[2]=fdy[0]-1,fdx[2]=fdx[0];break;
- case 3:fdy[2]=fdy[0],fdx[2]=fdx[0]-1;break;
- }break;
- case 6:switch(cnt%4){
- case 0:fdy[1]=fdy[0]-1,fdx[1]=fdx[0];fdy[2]=fdy[0]+1,fdx[2]=fdx[0];fdy[3]=fdy[0]+1,fdx[3]=fdx[0]-1;break;
- case 1:fdy[1]=fdy[0],fdx[1]=fdx[0]+1;fdy[2]=fdy[0],fdx[2]=fdx[0]-1;fdy[3]=fdy[0]-1,fdx[3]=fdx[0]-1;break;
- case 2:fdy[1]=fdy[0]+1,fdx[1]=fdx[0];fdy[2]=fdy[0]-1,fdx[2]=fdx[0];fdy[3]=fdy[0]-1,fdx[3]=fdx[0]+1;break;
- case 3:fdy[1]=fdy[0],fdx[1]=fdx[0]-1;fdy[2]=fdy[0],fdx[2]=fdx[0]+1;fdy[3]=fdy[0]+1,fdx[3]=fdx[0]+1;break;
- }break;
- default:switch(cnt%4){
- case 0:fdy[1]=fdy[0]-1,fdx[1]=fdx[0];fdy[2]=fdy[0]+1,fdx[2]=fdx[0];fdy[3]=fdy[0]-1,fdx[3]=fdx[0]-1;break;
- case 1:fdy[1]=fdy[0],fdx[1]=fdx[0]+1;fdy[2]=fdy[0],fdx[2]=fdx[0]-1;fdy[3]=fdy[0]-1,fdx[3]=fdx[0]+1;break;
- case 2:fdy[1]=fdy[0]+1,fdx[1]=fdx[0];fdy[2]=fdy[0]-1,fdx[2]=fdx[0];fdy[3]=fdy[0]+1,fdx[3]=fdx[0]+1;break;
- case 3:fdy[1]=fdy[0],fdx[1]=fdx[0]-1;fdy[2]=fdy[0],fdx[2]=fdx[0]+1;fdy[3]=fdy[0]+1,fdx[3]=fdx[0]-1;break;
- }break;
- }break;
- case 's':while(1){
- FF if(target[fdx[i]][fdy[i]+1]!=0) goto k2;
- FF fdy[i]++;
- }break;
- default:break;
- }continue;
- }
- FF
- if(target[fdx[i]][fdy[i]+1]!=0){
- k2:
- FFF
- target[fdx[j]][fdy[j]] = 2,gotoxy(fdx[j]*2,fdy[j]),puts("■");
- for(k=Map-2;k>=2;k--){
- for(t=1;t<Map-1;t++)
- if(target[t][k]==0)
- break;
- if(t == Map-1){
- for(r=k;r>0;r--)
- for(w=1;w<Map-1;w++)
- target[w][r] = target[w][r-1];
- k++,score+=20,show();
- if(speed-20>70) speed-=20;
- }
- }
- for(w=1,r=3;w<Map-1;w++){
- if(target[w][r]!=0){
- gotoxy(0,Map),puts("你输了!");
- Sleep(5000);
- exit(0);
- }
- }
- food(),fod();
- break;
- }
- FF fdy[i]++;
- }
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1510201513829.html
来源: http://www.codesnippet.cn/detail/1510201513829.html