该系列文章会根据项目的编写步骤来出
新方块的实现
由于设备问题,暂时出的代码是未进行运行检验的,后期会补上运行后的版本
#include <stdio.h>//C语言形式的输入输出
#include <graphics.h>//图形库的头文件
#include <time.h>
int score = 0;//总分
int rank = 0;//等级
#define BLOCK_COUNT 5
#define BLOCK_WIDTH 5
#define BLOCK_HEIGHT 5
#define UNIT_SIZE 20
int NextIndex=-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 main(void){
welcome();
initGameScene();
//产生新方块
nextblock();
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,"■");
}
}
}
void drawBlock(){
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 nextblock(void){
clearBlock();
//随机选择一种方块
srand(time(NULL));//使用时间函数的返回值,来作为随机种子
NextIndex=rand()%BLOCK_COUNT;
drawBlock(NextIndex);
}