本文主要是介绍【自用14.12】C++俄罗斯方块-清除方块,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
该系列文章会根据项目的编写步骤来出
由于设备问题,暂时出的代码是未进行运行检验的,后期会补上运行后的版本
清除方块-clearBlock函数编写
#include <stdio.h>//C语言形式的输入输出
#include <graphics.h>//图形库的头文件
#include <time.h>
#include <conio.h>//kbhit()int score = 0;//总分
int rank = 0;//等级#define BLOCK_COUNT 5
#define BLOCK_WIDTH 5
#define BLOCK_HEIGHT 5
#define UNIT_SIZE 20#define START_X 130
#define START_Y 30#define KEY_UP 72
#define KEY_RIGHT 77
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_SPACE 32int speed = 500;
int minX=30;
int minY=30;typedef enum{//方块的朝向BLOCK_UP,BLOCK_RIGHT,BLOCK_DOWN,BLOCK_LEFT
}block_dir_t;typedef enum{//移动的方向MOVE_DOWN,MOVE_LEFT,MOVE_RIGHT
}move_dir_t;int NextIndex=-1;//下一个方块的种类
int BlockIndex=-1;//当前方块的种类int visit[30][15]; // visit[i][j] == 1 表示该位置有方块int color[BLOCK_COUNT]={GREEN,CYAN,MAGENTA,BROWN,YELLOW
};
int block [BLOCK_COUNT*4][BLOCK_HEIGHT][BLOCK_WIDTH]={// | 形方块{ 0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0 },// L 形方块{ 0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0 },// 田 形方块{ 0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0 },// T 形方块{ 0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,1,0,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0 },// Z 形方块{ 0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0 },{ 0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0 },
};//实现欢迎界面
void welcome(void);//初始化游戏场景
void initGameScene(void);//将右上角区域的方块清除
void clearBlock(void);void clearBlock(int x,int k,block_dir_t dir);//在右上角区域中,绘制下一个方块
void drawBlock();//
void nextblock(void);int moveable(int x,int y,move_dir_t moveDir,block_dir_t blockDir);//检测游戏是否结束
void failCheck(void);void wait(int interval);//方块降落
void move(void);void newblock(void);int main(void){welcome();initGameScene();//产生新方块nextblock();// 清空访问数组Sleep(500);memset(visit, 0, sizeof(visit));while(1){newblock();}system("pause");colsegraph();return 0;
}void welcome(void){//初始化画布initgraph(550,660);//设置窗口标题HWND window=GetHWnd();//获取窗口SetWindowText(window,_T("俄罗斯方块 .远_"))//设置窗口标题//设置文本的字体样式setfont(40,0,_T("微软雅黑"));//0代表自适应宽度setcolor(WHITE);outtextxy(205,200,_T("俄罗斯方块"));//在指定位置输出文本setfont(20,0,_T("楷体"));//0代表自适应宽度outtextxy(175,300,_T("编程,从俄罗斯方块开始!"));//在指定位置输出文本Sleep(3000);//暂停3秒钟
}void initGameScene(void){char str[16];//清除屏幕cleardevice();rectangle(27,27,336,635);rectangle(29,29,334,633);rectangle(370,50,515,195);setfont(24,0,_T("楷体"));setcolor(LIGHTGRAY);outtextxy(405,215,_T("下一个"));setcolor(RED);outtextxy(405,280,_T("分数"));sprintf(str,"%d",score);outtextxy(415,310,str);//这里需要修改项目属性,操作方法如下//右击项目名称-》选择属性-》配置属性-》字符集-》使用多字节字符集outtextxy(405,375,_T("等级"));sprintf(str,"%d",rank);outtextxy(425,405,rank);//操作说明setcolor(LIGHTBLUE);outtextxy(390,475,_T("操作说明"));outtextxy(390,500,_T("↑:旋转"));outtextxy(390,525,_T("↓:下降"));outtextxy(390,550,_T("←:左移"));outtextxy(390,575,_T("→:右移"));outtextxy(390,600,_T("空格:暂停"));
}void clearBlock(void){setcolor(BLACK);setfont(23,0,"楷体");for(int i=0;i<BLOCK_HEIGHT;i++){for(int j=0;j<BLOCK_WIDTH;j++){//画黑色小方块int x=391+j*UNIT_SIZE;int y=71+i*UNIT_SIZE;outtextxy(x,y,"■");}}
}//清除指定位置指定方向的方块
//参数x:方框的左上角的x坐标
//参数y:方块的左上角在游戏区域内的坐标,距离游戏区域顶部的距离
void clearBlock(int x,int y,block_dir_t dir){setcolor(BLACK);int id=BlockcIndex*4+dir;//方块种类*4+方块方向=具体的某一个方块的编号y += START_Y;for(int i=0;i<5;i++){for(int j=0;j<5;j++){if(block[id][i][j]==1){//擦除该方块的第i行的第j列outtextxy(x+j*20,y+i*20,"■");}}}
}void drawBlock(int x,int y){setcolor(color[NextIndex]);//不同形态的方块对应不同的颜色setfont(23,0,"楷体");for(int i=0;i<BLOCK_HEIGHT;i++){for(int j=0;j<BLOCK_WIDTH;j++){if(block[NextIndex*4][i][j]==1){//画小方块int x=391+j*UNIT_SIZE;int y=71+i*UNIT_SIZE;outtextxy(x,y,"■");}}}
}void drawBlock(int x,int y,int blockIndex,block_dir_t dir){}void nextblock(void){clearBlock();//随机选择一种方块srand(time(NULL));//使用时间函数的返回值,来作为随机种子NextIndex=rand()%BLOCK_COUNT;drawBlock(NextIndex);
}//如果在指定位置可以在指定方向移动,就返回1,否则就返回0
int moveable(int x0,int y0,move_dir_t moveDir,block_dir_t blockDir){//计算当前方块的左上角在30*15的游戏区中的位置(第多少行,第多少列)int x=(y0-minY)/UNIT_SIZE;int y=(x0-minX)/UNIT_SIZE;int id=BlockIndex*4+blockDir;int ret=1;if(moveDir==MOVE_DOWN){for(int i=0;i<5;i++){for(int j=0;j<5;j++){//if(block[id][i][j]==1 && (x+i+1>30 || (x+i>=0 && x+i<30 && y+j>=0 && y+j<15 && visit[x+i+1][y+j]==1))){if(block[id][i][j]==1 && (x+i+1>30 || visit[x+i+1][y+j]==1)){ret=0;}}}}else if(moveDir==MOVE_LEFT){for(int i=0;i<5;i++){for(int j=0;j<5;j++){//if(block[id][i][j]==1 && (y+j==0 || (x+i>=0 && x+i<30 && y+j-1>=0 && y+j-1<15 && visit[x+i][y+j-1]==1))){if(block[id][i][j]==1 && (y+j==0 || visit[x+i][y+j-1]==1)){ret=0;}}}}else if(moveDir==NOVE_RIGHT){for(int i=0;i<5;i++){for(int j=0;j<5;j++){//if(block[id][i][j]==1 && (y+j+1>=15 || (y+j+1>=15 || (x+i>=0 && x+i<30 && y+i+1>=0 && y+j+1<15 && visit[x+i][y+j+1]==1)))){if(block[id][i][j]==1 && (y+j+1>=15 || (y+j+1>=15 || visit[x+i][y+j+1]==1))){ret=0;}}}}return ret;
}void failCheck(void){if(!moveable(START_X,START_Y,MOVE_DOWN,BLOCK_UP)){setcolor(WHITE);setfont(45,0,_T("隶体"));outtextxy(75,300,_T("GAME OVER!"));Sleep(1000);system("pause");closegraph();exit(0);}
}void wait(int interval){}void move(void){int x=START_X;int y=START_Y;int k=0;//方块二维数组左上角相对于方块下落起始高度的纵向偏移量block_dir_t blockDir=BLOCK_UP;int curSpeed = speed;//检测游戏是否结束failCheck();//持续向下降落while(1){if(kbhit()){int key=getch();if(key==KEY_SPACE){getch();//暂停}}//清除当前方块clearBlock(x,k,blockDir);if(kbhit()){int key=getch();if(key==KEY_UP){//to do}else if(key==KEY_DOWN){//to do}else if(key==KEY_LEFT){//to do}else if(key==KEY_RIGHT){//to do}}//绘制当前方块drawBlock();wait(curSpeed);//方块的固化处理//to do}
}void newblock(void){//确定即将使用的方块的类别BlockIndex=NextIndex;//绘制刚从顶部下降的方块drawBlock(START_X,START_Y);//让新出现的方块暂停一下,让用户识别到Sleep(100);//0.1秒//在右上角区域,绘制一个下一个方块nextblock();//方块降落move();
}
这篇关于【自用14.12】C++俄罗斯方块-清除方块的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!