制作俄罗斯方块小游戏(一)
分析:
printf函数高级用法
'\033[':表示转义序列的开始
'm':表示转义序列的结束
'0':重置所有属性
'1':设置粗体或高亮
'30-37':设置字体色 30: 黑 31: 红 32: 绿 33: 黄 34: 蓝 35: 紫 36: 深绿 37: 白色
'40-47':设置背景色 40: 黑 41: 红 42: 绿 43: 黄 44: 蓝 45: 紫 46: 深绿 47: 白色
/033[0m 关闭所有属性
/033[1m 设置高亮度
/033[4m 下划线
/033[5m 闪烁
/033[7m 反显
/033[8m 消隐
/033[30m -- /033[37m 设置前景色
/033[40m -- /033[47m 设置背景色
/033[nA 光标上移n行
/033[nB 光标下移n行
/033[nC 光标右移n行
/033[nD 光标左移n行
/033[y;xH设置光标位置
/033[2J 清屏
/033[K 清除从光标到行尾的内容
/033[s 保存光标位置
/033[u 恢复光标位置
/033[?25l 隐藏光标
/033[?25h 显示光标
#include <stdio.h>
#include <unistd.h>
#include <signal.h>extern int InitBoarder(void);
extern int SetBoarder(void);
extern int ShowMenu(void);
extern int InitBox(void);
extern int CreateBox(int BoxNo);
extern int MoveBox(int y, int x);
extern int ClearBox(int y, int x);
extern int CanMoveBox(int y, int x);int cury = 1;
int curx = 16;void handler(int signo)
{ClearBox(cury, curx);if (CanMoveBox(cury+1, curx)){MoveBox(cury+1, curx);cury = cury+1;ShowMenu();}else {MoveBox(cury, curx);ShowMenu();}alarm(1);return;
}int main(void)
{int i = 0;signal(SIGALRM, handler);InitBoarder();SetBoarder();CreateBox(13);MoveBox(cury, curx);ShowMenu();alarm(1);while (1){}return 0;
}
/************************************************************ * 文件名:boarder.c * 功能: 游戏界面相关功能* ************************************************************/#include <stdio.h>/* 游戏界面的宽度 */
#define BOARDER_WIDTH 60/* 游戏界面的高度 */
#define BOARDER_HEIGHT 30/* 游戏界面的数据内容 */
unsigned char gBoarder[BOARDER_HEIGHT][BOARDER_WIDTH];/* 初始化游戏界面数组内容 */
int InitBoarder(void)
{int i = 0;int j = 0;//1.对所有的元素都赋值为' 'for (j = 0; j < BOARDER_HEIGHT; j++){for (i = 0; i < BOARDER_WIDTH; i++){gBoarder[j][i] = ' ';}}return 0;
}/* 游戏界面加入边框 */
int SetBoarder(void)
{int i = 0;int j = 0;//1.第一行赋值为'#'for (i = 0; i < BOARDER_WIDTH; i++){gBoarder[0][i] = '#';}//2.最后一行赋值为'#'for (i = 0; i < BOARDER_WIDTH; i++){gBoarder[BOARDER_HEIGHT-1][i] = '#';}//3.第一列赋值为'#'for (j = 0; j < BOARDER_HEIGHT; j++){gBoarder[j][0] = '#';gBoarder[j][1] = '#';}//4.最后一列赋值为'#'for (j = 0; j < BOARDER_HEIGHT; j++){gBoarder[j][BOARDER_WIDTH-1] = '#';gBoarder[j][BOARDER_WIDTH-2] = '#';}//5.界面中间选择一列赋值为'#',作为游戏区域和其他功能区域的划分for (j = 0; j < BOARDER_HEIGHT; j++){gBoarder[j][BOARDER_WIDTH/3*2-1] = '#';gBoarder[j][BOARDER_WIDTH/3*2] = '#';}//6.游戏界面有方一行的一部分赋值为'#'for (i = BOARDER_WIDTH/3*2; i < BOARDER_WIDTH; i++){gBoarder[BOARDER_HEIGHT/2][i] = '#';}return 0;
}/* 游戏界面的显示 */
int ShowMenu(void)
{int i = 0;int j = 0;printf("\033[2J");for (j = 0; j < BOARDER_HEIGHT; j++){for (i = 0; i < BOARDER_WIDTH; i++){if ('#' == gBoarder[j][i]){printf("\033[41;31m%c\033[0m", gBoarder[j][i]);}else if ('[' == gBoarder[j][i] || ']' == gBoarder[j][i]){printf("\033[40;30m%c\033[0m", gBoarder[j][i]);}else{printf("%c", gBoarder[j][i]);}}printf("\n");}return 0;
}
/************************************************************ * 文件名:block.c * 功能: 俄罗斯方块功能* ************************************************************/#include <stdio.h>#define BOARDER_WIDTH 60#define BOARDER_HEIGHT 30extern unsigned char gBoarder[BOARDER_HEIGHT][BOARDER_WIDTH];/* 俄罗斯方块数据 */
unsigned char gBox[4][8];/* 初始化俄罗斯方块内容 */
int InitBox(void)
{int i = 0;int j = 0;for (j = 0; j < 4; j++){for (i = 0; i < 8; i++){gBox[j][i] = ' ';}}return 0;
}/* 创建俄罗斯方块图形 */
int CreateBox(int BoxNo)
{int i = 0;int j = 0;InitBox();if (0 == BoxNo){//[][][][]//////for (i = 0; i < 8; i++){gBox[0][i] = (0 == i % 2 ? '[' : ']');}}else if (1 == BoxNo){//[]//[]//[]//[]for (j = 0; j < 4; j++){gBox[j][0] = '[';gBox[j][1] = ']';}}else if (2 == BoxNo){//[][]//[][]////for (j = 0; j < 2; j++){for (i = 0; i < 4; i++){gBox[j][i] = (i % 2 == 0 ? '[' : ']');}}}else if (3 == BoxNo){//[][]//[]//[]//for (j = 0; j < 3; j++){gBox[j][0] = '[';gBox[j][1] = ']';}gBox[0][2] = '[';gBox[0][3] = ']';}else if (4 == BoxNo){//[][][] // []////for (i = 0; i < 6; i++){gBox[0][i] = (0 == i % 2 ? '[' : ']');}gBox[1][4] = '[';gBox[1][5] = ']';}else if (5 == BoxNo){// []// []//[][]//for (j = 0; j < 3; j++){gBox[j][2] = '[';gBox[j][3] = ']';}gBox[2][0] = '[';gBox[2][1] = ']';}else if (6 == BoxNo){//[]//[][][]////for (i = 0; i < 6; i++){gBox[1][i] = (0 == i % 2 ? '[' : ']');}gBox[0][0] = '[';gBox[0][1] = ']';}else if (7 == BoxNo){//[][]// []// []//for (j = 0; j < 3; j++){gBox[j][2] = '[';gBox[j][3] = ']';}gBox[0][0] = '[';gBox[0][1] = ']';}else if (8 == BoxNo){// []//[][][] ////for (i = 0; i < 6; i++){gBox[1][i] = (0 == i % 2 ? '[' : ']');}gBox[0][4] = '[';gBox[0][5] = ']';}else if (9 == BoxNo){//[]//[]//[][]//for (j = 0; j < 3; j++){gBox[j][0] = '[';gBox[j][1] = ']';}gBox[2][2] = '[';gBox[2][3] = ']';}else if (10 == BoxNo){//[][][]//[]////for (i = 0; i < 6; i++){gBox[0][i] = (0 == i % 2 ? '[' : ']');}gBox[1][0] = '[';gBox[1][1] = ']';}else if (11 == BoxNo){// []//[][][]////for (i = 0; i < 6; i++){gBox[1][i] = (0 == i % 2 ? '[' : ']');}gBox[0][2] = '[';gBox[0][3] = ']';}else if (12 == BoxNo){//[]//[][]//[]//for (j = 0; j < 3; j++){gBox[j][0] = '[';gBox[j][1] = ']';}gBox[1][2] = '[';gBox[1][3] = ']';}else if (13 == BoxNo){//[][][]// []////for (i = 0; i < 6; i++){gBox[0][i] = (0 == i % 2 ? '[' : ']');}gBox[1][2] = '[';gBox[1][3] = ']';}else if (14 == BoxNo){// []//[][]// []//for (j = 0; j < 3; j++){gBox[j][2] = '[';gBox[j][3] = ']';}gBox[1][0] = '[';gBox[1][1] = ']';}else if (15 == BoxNo){//[]//[][]// []//for (j = 0; j < 2; j++){gBox[j][0] = '[';gBox[j][1] = ']';}for (j = 1; j < 3; j++){gBox[j][2] = '[';gBox[j][3] = ']';}}else if (16 == BoxNo){// [][]//[][]////for (i = 2; i < 6; i++){gBox[0][i] = (0 == i % 2 ? '[' : ']');}for (i = 0; i < 4; i++){gBox[1][i] = (0 == i % 2 ? '[' : ']');}}else if (17 == BoxNo){// []//[][]//[]//for (j = 1; j < 3; j++){gBox[j][0] = '[';gBox[j][1] = ']';}for (j = 0; j < 2; j++){gBox[j][2] = '[';gBox[j][3] = ']';}}else if (18 == BoxNo){//[][]// [][]////for (i = 0; i < 4; i++){gBox[0][i] = (0 == i % 2 ? '[' : ']');}for (i = 2; i < 6; i++){gBox[1][i] = (0 == i % 2 ? '[' : ']');}}return 0;
}/* 判断俄罗斯方块是否能够移动到指定游戏界面位置 */
int CanMoveBox(int y, int x)
{int i = 0;int j = 0;for (j = 0; j < 4; j++){for (i = 0; i < 8; i++){if ((gBox[j][i] == '[' || gBox[j][i] == ']') && (gBoarder[j+y][i+x] != ' ')){return 0;}}}return 1;
}/* 将俄罗斯方块移动到游戏界面中 */
int MoveBox(int y, int x)
{int i = 0;int j = 0;for (j = 0; j < 4; j++){for (i = 0; i < 8; i++){if (gBox[j][i] == '[' || gBox[j][i] == ']'){gBoarder[j+y][i+x] = gBox[j][i];}}}return 0;
}/* 将俄罗斯方块从游戏界面中清除 */
int ClearBox(int y, int x)
{int i = 0;int j = 0;for (j = 0; j < 4; j++){for (i = 0; i < 8; i++){if (gBox[j][i] == '[' || gBox[j][i] == ']'){gBoarder[j+y][i+x] = ' ';}}}return 0;
}