俄罗斯方块
2012-06-03 09:02
162 查看
看看,试试,玩玩,呵呵。。。。!!
#include<stdio.h> #include<stdlib.h> #include<memory.h> #include<windows.h> #include<conio.h> #include<time.h> #define PLUS(x) (x == 1 ? 1 : -1) #define ZFROM_W 21//游戏框的宽 #define ZFROM_H 21//游戏框的高 #define FFROM_W 9 #define MODE 7 #define NORN 3 #define NORM 4 #define STARTX 7 #define STARTY -1 #define SANG 100 int map[ZFROM_W + 5][ZFROM_H + 5]; int Srow[ZFROM_H + 5]; int TIME; int type,idx,next; int score; int norm,stage; int x,y; int fig[MODE][4] = {183141,183141,183141,183141,215916,187177,215849,186789,249132,162213,249132,162213, 220012,182885,220012,182885,191333,203561,191333,203561,215845,191340,204076,190885,220005,182693,220460,182890};//# , T ,|,Z反,Z正,F正, HANDLE g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE g_hInput = GetStdHandle(STD_INPUT_HANDLE); int dir[4][2] = {0,-1,2,0,0,1,-2,0};//up , right , down , left void SetCursor(int x,int y)//设置光标位置 { COORD cd = {x , y}; SetConsoleCursorPosition(g_hOutput , cd); } void Draw_from(int w,int h,int x,int y)//打印方框 { int i,j; SetCursor(x,y); printf(" "); for(i = 1;i < w;i ++) { printf("-"); } puts(" "); for(i = 1;i < h;i ++) { SetCursor(x,i); for(j = 0;j <= w;j ++) { if(j == 0 || j == w) printf("|"); else printf(" "); } puts(""); } SetCursor(x,h); printf(" "); for(i = 1;i < w;i ++) { printf("-"); } SetCursor(23 , 2); printf("NEXT:"); SetCursor(23 , 10); printf("SPEED:"); SetCursor(23,15); printf("SCORE:"); } void Print_fig(int x,int y,int type,int flag)//打印,擦除,记录点 { int i; int tx = x , ty = y; int tmp = fig[type][idx]; SetCursor(tx , ty); for(i = 1;i <= 4;i ++) { if(ty > 0){ if(flag == 1)printf("■"); else if(flag == 0) printf(" "); else if(flag == 2) { map[ty][tx] = 1; Srow[ty] += 2; } } tx = x + (PLUS((tmp & NORM) / 4) * (tmp & NORN) * 2); tmp >>= 3; ty = y + (PLUS((tmp & NORM) / 4) * (tmp & NORN)); tmp >>= 3; SetCursor(tx , ty); } SetCursor(30,21); return ; } void Inite_game()//初始化游戏界面 { memset(map,0,sizeof(map)); memset(Srow,0,sizeof(Srow)); srand(time(NULL)); type = rand() % MODE; next = rand() % MODE; Print_fig(24 , 4 ,next, 1); idx = 0; score = 0; x = STARTX; y = STARTY; TIME = 400; norm = 50; stage = 1; Print_fig(x , y , type , 1); SetCursor(24 , 11); printf("%c",2); SetCursor(24 , 16); printf("%d",score); } int Judge(int id,int x,int y)//判断动作是否可以进行 { int i; int tx,ty; bool flag = false; int tmp = fig[type][idx]; if(id == 0) { tmp = fig[type][(idx+1)%4]; tx = x , ty = y; } else { tx = x += dir[id][0]; ty = y += dir[id][1]; } for(i = 1;i <= 4;i ++) { if(ty < 1) flag = true; if(tx < 1 || tx >= ZFROM_W - 1 || ty >= ZFROM_H) { if(flag) return 2; return 0; } if(map[ty][tx]){ if(flag) return 2; return 0; } tx = x + (PLUS((tmp & NORM) / 4) * (tmp & NORN) * 2); tmp >>= 3; ty = y + (PLUS((tmp & NORM) / 4) * (tmp & NORN)); tmp >>= 3; } return 1; } void Deal_judge()//处理消除一行 { int i,j,k,n; int gde = 5; while(true){ for(i = 1;i < ZFROM_H;i ++) { if(Srow[i] == ZFROM_W - 1) break; } n = i; if(i == ZFROM_H){ SetCursor(24 , 16); printf("%d",score); if(score > norm) { norm *= 50 * stage; stage ++; TIME -= TIME * 0.1; SetCursor(24,11); for(k = 1;k <= stage;k ++) printf("%c",2); } return ; } score += gde; gde *= 2; for(k = 1;k <= 3;k ++){ SetCursor(1 , i); for(j = 1;j <= ZFROM_W / 2;j ++) { if(k & 1) printf(" "); else printf("■"); } Sleep(SANG); } for(i ;i > 1;i --) { Srow[i] = Srow[i-1]; for(j = 1;j < ZFROM_W;j ++) { map[i][j] = map[i-1][j]; } } for(i = 1;i <= n;i ++) { for(j = 1;j < ZFROM_W;j += 2) { SetCursor(j , i); if(map[i][j]) {printf("■");} else printf(" "); } } } return ; } int Move_fig(int id)//改变,移动方块 { int tans = Judge(id,x,y); if(tans == 0) {//如果移动不了了 if(id == 2){ Print_fig(x , y , type , 2); Deal_judge(); type = next; idx = 0; next = rand() % MODE; Print_fig(24 , 4 , type , 0); Print_fig(24 , 4 , next , 1); x = STARTX; y = STARTY; } return 1; } if(tans == 2) return 0; Print_fig(x , y , type , 0); if(id == 0){ idx = (idx + 1) % 4; } else { x += dir[id][0]; y += dir[id][1]; } Print_fig(x , y , type , 1); return 1; } void Deal_gameover()//游戏结束 { int i,j,k; Print_fig(24 , 4 , next , 0); SetCursor(24,11); SetCursor(24 , 16); printf(" "); for(k = 1;k <= stage;k ++){ printf(" "); } for(i = 1;i <= 5;i ++) { SetCursor(0 , 1); for(j = 1;j < ZFROM_H;j ++) { for(k = 0;k <= ZFROM_W / 2;k ++) { if(!k) {printf("|");continue;} if(i & 1)printf(" "); else printf("■"); } puts(""); } Sleep(SANG); if(i == 4) { SetCursor(5 , 10); printf("GAME OVER"); Sleep(2000); SetCursor(5 , 10); printf("按 ESC 退出"); Sleep(2000); if(GetAsyncKeyState(VK_ESCAPE)) { exit(0); } } } return ; } int main() { int pre,cur; Draw_from(ZFROM_W,ZFROM_H,0,0); Draw_from(FFROM_W,ZFROM_H,ZFROM_W + 1 , 0); while(true) { Inite_game(); while(true) { cur = pre = GetTickCount(); while(cur - pre < TIME) { if(GetAsyncKeyState(VK_UP)) { //printf("%c",7); Move_fig(0); } if(GetAsyncKeyState(VK_DOWN)) { if(Move_fig(2) == 0) break; } if(GetAsyncKeyState(VK_LEFT)) { Move_fig(3); } if(GetAsyncKeyState(VK_RIGHT)) { Move_fig(1); } if(GetAsyncKeyState(VK_SPACE)) { while(true) { Sleep(1000); if(GetAsyncKeyState(VK_SPACE)) { break; } } } if(GetAsyncKeyState(VK_ESCAPE)) { exit(0); } Sleep(100); cur = GetTickCount(); } if(Move_fig(2) == 0) break; } Deal_gameover(); } return 0; }
相关文章推荐
- 俄罗斯方块 tc版
- Tc2.0 编写俄罗斯方块游戏
- csdn下载的java applet俄罗斯方块
- 一个基于Yahoo UI的Ajax俄罗斯方块游戏
- c++写的俄罗斯方块源码
- 俄罗斯方块新增行算法:不拘一格编程序之二
- 零打碎敲学Android(三)—俄罗斯,你为什么是方块的?
- 【Python】用Python实现一个俄罗斯方块游戏
- aasddsasda的JS俄罗斯方块全解释
- 俄罗斯方块源码解析(带下载)[2]
- 俄罗斯方块游戏笔记(二)——参数配置窗体
- shell脚本制作俄罗斯方块游戏
- 发布一个Java写的俄罗斯方块源码 算法简单(300行) 注释详细
- 多库俄罗斯方块之一(Delphi)
- 牛×,用批处理写的俄罗斯方块游戏
- 俄罗斯方块 -- 暑假集训
- <Unity3D>使用Unity来制作俄罗斯方块游戏
- 1568俄罗斯方块
- 史上最短小精悍的javascript编写的俄罗斯方块游戏,仅仅60行代码
- 史上最短小精悍的javascript编写的俄罗斯方块游戏,仅仅60行代码