#include<stdio.h>#include <graphics.h>#include <conio.h>#include <time.h>
// 定义常量、枚举量、结构体、全局变量
#defineWIDTH10// 游戏区宽度#defineHEIGHT22// 游戏区高度#defineSIZE20// 每个游戏区单位的实际像素int mark=0;char Mark[8];int level=1;char Level[10];int LEVEL[10]={500,400,300,200,150,120,100,80,60,50};//设置关卡时间
// 定义操作类型enum CMD{CMD_ROTATE,// 方块旋转CMD_LEFT, CMD_RIGHT, CMD_DOWN,// 方块左、右、下移动CMD_SINK,// 方块沉底CMD_QUIT,// 退出游戏CMD_Q//暂停游戏};
// 定义绘制方块的方法enum DRAW{SHOW,// 显示方块HIDE,// 隐藏方块FIX// 固定方块};
// 定义七种俄罗斯方块struct BLOCK{short dir[4];// 方块的四个旋转状态int color;// 方块的颜色}g_Blocks[7] = {{0x2260, 0x0E20, 0x0644, 0x0470, RED},// 反L{0x0660, 0x0660, 0x0660, 0x0660, BLUE},// 口{0x4460, 0x02E0, 0x0622, 0x0740, MAGENTA},// L{0x0F00, 0x4444, 0x0F00, 0x4444, YELLOW},// I{0x0C60, 0x2640, 0x0C60, 0x2640, CYAN},// Z {0x0360, 0x4620, 0x0360, 0x4620, GREEN},// 反Z{0x4E00, 0x4C40, 0x0E40, 0x4640, BROWN}};// T
// 定义当前方块、下一个方块的信息struct BLOCKINFO{unsigned char id;// 方块 IDint x, y;// 方块在游戏区中的坐标unsigned char dir:2;// 方向}g_CurBlock, g_NextBlock;
// 定义游戏区BYTE g_World[WIDTH][HEIGHT] = {0};
/////////////////////////////////////////////// 函数声明/////////////////////////////////////////////
void Init();// 初始化游戏void Quit();// 退出游戏void NewGame();// 开始新游戏void GameOver();// 结束游戏CMD GetCmd();// 获取控制命令void DispatchCmd(CMD _cmd);// 分发控制命令void NewBlock();// 生成新的方块bool CheckBlock(BLOCKINFO _block);// 检测指定方块是否可以放下void DrawBlock(BLOCKINFO _block, DRAW _draw = SHOW);// 画方块void OnRotate();// 旋转方块void OnLeft();// 左移方块void OnRight();// 右移方块void OnDown();// 下移方块void OnSink();// 沉底方块void Welcome();//欢迎界面void QUIT();//暂停游戏
/////////////////////////////////////////////// 函数定义/////////////////////////////////////////////
// 主函数void main(){Welcome();Init();CMD c;while(true){c = GetCmd();DispatchCmd(c);// 按退出时,显示对话框咨询用户是否退出if (c == CMD_QUIT){HWND wnd = GetHWnd();if (MessageBox(wnd, "您要退出游戏吗?", "提醒", MB_OKCANCEL | MB_ICONQUESTION) == IDOK)Quit();}}}
//设置欢迎界面void Welcome(){int b,x=0,y=0,i,j,x1=0,y1=0;initgraph(640,480);//调制背景for(i=0;i<200;i++){setcolor(RGB(0,0,200-i));rectangle(160-i,120-i,480+i,360+i);}bar(160,120,480,360);//绘制方块setorigin(160,120);for(j=0;j<7;j++){b=g_Blocks[j].dir[2];setfillstyle(g_Blocks[j].color);for(i=0;i<16;i++){x=x1+i%4;y=y1+i/4;if(b&0x8000)bar3d(x*SIZE,y*SIZE+2,(x+1)*SIZE-2,(y+1)*SIZE,6,1);b<<=1;}x1+=5;if(j==2){x1=5;y1+=4;}if(j==3){x1=0;y1+=4;}}setfont(30,0,"楷体");setbkcolor(BLUE);setcolor(LIGHTMAGENTA);outtextxy(70,-40,"俄罗斯方块");setfont(20,0,"宋体");//显示动态提示while(1){setcolor(LIGHTCYAN);outtextxy(-170+x,-119,"亲,按任意键进入游戏!");Sleep(30);setcolor(BLACK);outtextxy(-170+x,-119,"亲,按任意键进入游戏!");x+=2;if(x>640)x=-160;if(kbhit())break;}getch();closegraph();}
// 初始化游戏void Init(){initgraph(640, 480);srand((unsigned)time(NULL));// 显示操作说明setfont(35,0,"楷体");setcolor(MAGENTA);outtextxy(20,20,"俄罗斯方块");setfont(15, 0, "园体");setcolor(CYAN);outtextxy(20, 310,"操作说明");outtextxy(20, 330,"Q:暂停游戏(任意键开始)");outtextxy(20, 350,"上:旋转");outtextxy(20, 370,"左:左移");outtextxy(20, 390,"右:右移");outtextxy(20, 410,"下:下移");outtextxy(20, 430,"空格:沉底");outtextxy(20, 450,"ESC:退出");setcolor(WHITE);// 设置坐标原点setorigin(220, 20);// 绘制游戏区边界setlinestyle(0,0,3);rectangle(-1, -1, WIDTH * SIZE, HEIGHT * SIZE);rectangle((WIDTH + 1) * SIZE - 1, -1, (WIDTH + 5) * SIZE, 4 * SIZE);setlinestyle(0,0,1); rectangle((WIDTH+1)*SIZE,8* SIZE,(WIDTH+9)*SIZE,10*SIZE);outtextxy((WIDTH+1)*SIZE+5,9* SIZE,"Mark:");sprintf(Mark,"%08d",mark);outtextxy((WIDTH+4)*SIZE,9* SIZE,Mark);rectangle((WIDTH+1)*SIZE,14* SIZE,(WIDTH+9)*SIZE,16*SIZE); outtextxy((WIDTH+1)*SIZE+5,15* SIZE,"Level:");sprintf(Level,"%2d",level);outtextxy((WIDTH+4)*SIZE,15* SIZE,Level);// 开始新游戏NewGame();}
//暂停游戏void QUIT(){while(1){if(kbhit())return;}}
// 退出游戏void Quit(){closegraph();exit(0);}
// 开始新游戏void NewGame(){// 清空游戏区setfillstyle(BLACK);bar(0, 0, WIDTH * SIZE - 1, HEIGHT * SIZE - 1);ZeroMemory(g_World, WIDTH* HEIGHT);// 生成下一个方块g_NextBlock.id = rand() % 7;g_NextBlock.dir = rand() % 4;g_NextBlock.x = WIDTH+1 ;g_NextBlock.y = HEIGHT-1;// 获取新方块NewBlock();}
// 结束游戏void GameOver(){HWND wnd = GetHWnd();if (MessageBox(wnd, "游戏结束。\n您想重新来一局吗?", "游戏结束", MB_YESNO | MB_ICONQUESTION) == IDYES)NewGame();elseQuit();}
// 定义常量、枚举量、结构体、全局变量
#defineWIDTH10// 游戏区宽度#defineHEIGHT22// 游戏区高度#defineSIZE20// 每个游戏区单位的实际像素int mark=0;char Mark[8];int level=1;char Level[10];int LEVEL[10]={500,400,300,200,150,120,100,80,60,50};//设置关卡时间
// 定义操作类型enum CMD{CMD_ROTATE,// 方块旋转CMD_LEFT, CMD_RIGHT, CMD_DOWN,// 方块左、右、下移动CMD_SINK,// 方块沉底CMD_QUIT,// 退出游戏CMD_Q//暂停游戏};
// 定义绘制方块的方法enum DRAW{SHOW,// 显示方块HIDE,// 隐藏方块FIX// 固定方块};
// 定义七种俄罗斯方块struct BLOCK{short dir[4];// 方块的四个旋转状态int color;// 方块的颜色}g_Blocks[7] = {{0x2260, 0x0E20, 0x0644, 0x0470, RED},// 反L{0x0660, 0x0660, 0x0660, 0x0660, BLUE},// 口{0x4460, 0x02E0, 0x0622, 0x0740, MAGENTA},// L{0x0F00, 0x4444, 0x0F00, 0x4444, YELLOW},// I{0x0C60, 0x2640, 0x0C60, 0x2640, CYAN},// Z {0x0360, 0x4620, 0x0360, 0x4620, GREEN},// 反Z{0x4E00, 0x4C40, 0x0E40, 0x4640, BROWN}};// T
// 定义当前方块、下一个方块的信息struct BLOCKINFO{unsigned char id;// 方块 IDint x, y;// 方块在游戏区中的坐标unsigned char dir:2;// 方向}g_CurBlock, g_NextBlock;
// 定义游戏区BYTE g_World[WIDTH][HEIGHT] = {0};
/////////////////////////////////////////////// 函数声明/////////////////////////////////////////////
void Init();// 初始化游戏void Quit();// 退出游戏void NewGame();// 开始新游戏void GameOver();// 结束游戏CMD GetCmd();// 获取控制命令void DispatchCmd(CMD _cmd);// 分发控制命令void NewBlock();// 生成新的方块bool CheckBlock(BLOCKINFO _block);// 检测指定方块是否可以放下void DrawBlock(BLOCKINFO _block, DRAW _draw = SHOW);// 画方块void OnRotate();// 旋转方块void OnLeft();// 左移方块void OnRight();// 右移方块void OnDown();// 下移方块void OnSink();// 沉底方块void Welcome();//欢迎界面void QUIT();//暂停游戏
/////////////////////////////////////////////// 函数定义/////////////////////////////////////////////
// 主函数void main(){Welcome();Init();CMD c;while(true){c = GetCmd();DispatchCmd(c);// 按退出时,显示对话框咨询用户是否退出if (c == CMD_QUIT){HWND wnd = GetHWnd();if (MessageBox(wnd, "您要退出游戏吗?", "提醒", MB_OKCANCEL | MB_ICONQUESTION) == IDOK)Quit();}}}
//设置欢迎界面void Welcome(){int b,x=0,y=0,i,j,x1=0,y1=0;initgraph(640,480);//调制背景for(i=0;i<200;i++){setcolor(RGB(0,0,200-i));rectangle(160-i,120-i,480+i,360+i);}bar(160,120,480,360);//绘制方块setorigin(160,120);for(j=0;j<7;j++){b=g_Blocks[j].dir[2];setfillstyle(g_Blocks[j].color);for(i=0;i<16;i++){x=x1+i%4;y=y1+i/4;if(b&0x8000)bar3d(x*SIZE,y*SIZE+2,(x+1)*SIZE-2,(y+1)*SIZE,6,1);b<<=1;}x1+=5;if(j==2){x1=5;y1+=4;}if(j==3){x1=0;y1+=4;}}setfont(30,0,"楷体");setbkcolor(BLUE);setcolor(LIGHTMAGENTA);outtextxy(70,-40,"俄罗斯方块");setfont(20,0,"宋体");//显示动态提示while(1){setcolor(LIGHTCYAN);outtextxy(-170+x,-119,"亲,按任意键进入游戏!");Sleep(30);setcolor(BLACK);outtextxy(-170+x,-119,"亲,按任意键进入游戏!");x+=2;if(x>640)x=-160;if(kbhit())break;}getch();closegraph();}
// 初始化游戏void Init(){initgraph(640, 480);srand((unsigned)time(NULL));// 显示操作说明setfont(35,0,"楷体");setcolor(MAGENTA);outtextxy(20,20,"俄罗斯方块");setfont(15, 0, "园体");setcolor(CYAN);outtextxy(20, 310,"操作说明");outtextxy(20, 330,"Q:暂停游戏(任意键开始)");outtextxy(20, 350,"上:旋转");outtextxy(20, 370,"左:左移");outtextxy(20, 390,"右:右移");outtextxy(20, 410,"下:下移");outtextxy(20, 430,"空格:沉底");outtextxy(20, 450,"ESC:退出");setcolor(WHITE);// 设置坐标原点setorigin(220, 20);// 绘制游戏区边界setlinestyle(0,0,3);rectangle(-1, -1, WIDTH * SIZE, HEIGHT * SIZE);rectangle((WIDTH + 1) * SIZE - 1, -1, (WIDTH + 5) * SIZE, 4 * SIZE);setlinestyle(0,0,1); rectangle((WIDTH+1)*SIZE,8* SIZE,(WIDTH+9)*SIZE,10*SIZE);outtextxy((WIDTH+1)*SIZE+5,9* SIZE,"Mark:");sprintf(Mark,"%08d",mark);outtextxy((WIDTH+4)*SIZE,9* SIZE,Mark);rectangle((WIDTH+1)*SIZE,14* SIZE,(WIDTH+9)*SIZE,16*SIZE); outtextxy((WIDTH+1)*SIZE+5,15* SIZE,"Level:");sprintf(Level,"%2d",level);outtextxy((WIDTH+4)*SIZE,15* SIZE,Level);// 开始新游戏NewGame();}
//暂停游戏void QUIT(){while(1){if(kbhit())return;}}
// 退出游戏void Quit(){closegraph();exit(0);}
// 开始新游戏void NewGame(){// 清空游戏区setfillstyle(BLACK);bar(0, 0, WIDTH * SIZE - 1, HEIGHT * SIZE - 1);ZeroMemory(g_World, WIDTH* HEIGHT);// 生成下一个方块g_NextBlock.id = rand() % 7;g_NextBlock.dir = rand() % 4;g_NextBlock.x = WIDTH+1 ;g_NextBlock.y = HEIGHT-1;// 获取新方块NewBlock();}
// 结束游戏void GameOver(){HWND wnd = GetHWnd();if (MessageBox(wnd, "游戏结束。\n您想重新来一局吗?", "游戏结束", MB_YESNO | MB_ICONQUESTION) == IDYES)NewGame();elseQuit();}