文章目录
1 启动页面
1.1 启动页面分析
启动页面:
启动页面分析:
启动界面的大小为:550 X 660(像素)。
1.2 启动界面代码实现
# include <stdio.h>
# include <graphics.h>
void welcome(void) {
initgraph(550, 660);
// 设置窗口标题
HWND hwnd = GetHWnd();
SetWindowText(hwnd, "俄罗斯方块");
//Sleep(2000);
// 游戏标题
setfont(40, 0, "微软雅黑");
setcolor(WHITE);
outtextxy(205, 200, "俄罗斯方块!");
// 游戏副标题
setfont(22, 0, "楷体");
outtextxy(175, 300, "编程,从俄罗斯方块开始!");
Sleep(3000);
}
int main()
{
welcome();
closegraph();
return 0;
}
2 初始化游戏环境
2.1 界面效果及分析
效果:
分析:
2.2 代码实现
int score = 0; // 总分
int rank = 0; //等级
void initGameScene()
{
char str[16];
cleardevice();
setcolor(WHITE);
rectangle(29, 29, 334, 633);
rectangle(27, 27, 336, 635);
rectangle(370, 50, 515, 195);
setfont(24, 0, "楷体");
setcolor(LIGHTGRAY);
outtextxy(405, 215, "下一个:");
setcolor(RED);
outtextxy(405, 280, "分数:");
sprintf(str, "%d", score);
outtextxy(415, 310, str);
outtextxy(405, 375, "等级:");
sprintf(str, "%d", rank);
outtextxy(425, 405, str);
setfont(22, 0, "楷体");
setcolor(LIGHTBLUE);
outtextxy(390, 475, "操作说明:");
outtextxy(390, 500, "↑: 旋转");
outtextxy(390, 525, "↓: 下降");
outtextxy(390, 550, "←: 左移");
outtextxy(390, 575, "→: 右移");
outtextxy(390, 600, "空格: 暂停");
}
int main()
{
welcome();
initGameScene();
system("pause");
closegraph();
return 0;
}
3 新方块
3.1 显示效果
3.2 分析
以L型方块为例:
每个方块有4种形态:4个方向,所以使用4个二维数组来表示1种方块。
{
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 }
3.3 代码实现
#define BLOCK_COUNT 5
#define BLOCK_WIDTH 5
#define BLOCK_HEIGHT 5
#define UNIT_SIZE 20 //小砖块的宽度和高度
int color[BLOCK_COUNT] = {
GREEN,CYAN,MAGENTA,BROWN,YELLOW
};
int NextIndex = -1;
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