【自用14.10】C++俄罗斯方块-移动测试

该系列文章会根据项目的编写步骤来出

由于设备问题,暂时出的代码是未进行运行检验的,后期会补上运行后的版本

移动测试

#include //C语言形式的输入输出
#include //图形库的头文件
#include 
#include //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 32

int 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 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;i30 || (x+i>=0 && x+i<30 && y+j>=0 && y+j<15 && 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))){
                    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)))){
                    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;
    block_dir_t blockDir;
    int curSpeed = speed;

    //检测游戏是否结束
    failCheck();

    //持续向下降落
    while(1){
        if(kbhit()){
            int key=getch();
            if(key==KEY_SPACE){
                getch();//暂停
            }
        }

        //清除当前方块
        clearBlock(x,y,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();
}

你可能感兴趣的:(C++,Pro,学习笔记,c++,算法,数据结构)