文章目录
你的点赞评论就是对博主最大的鼓励
当然喜欢的小伙伴可以:点赞+关注+评论+收藏(一键四连)哦~
🍊自我介绍
Hello,大家好,我是小珑也要变强(也是小珑),我是易编程·终身成长社群的一名“创始团队·嘉宾” 和“内容共创官” ,现在我来为大家介绍一下有关物联网-嵌入式方面的内容。
🍊按键控制方块代码整合
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <time.h>
int n_x = 6;//初始横坐标
int n_y = 6;//初始纵坐标
int n_num;//图形选择
int n_mode;//图形变化形态选择
int n_color;//图形颜色
int shape[7][4][18] =
{
{
{1,1,0,0, 1,1,0,0, 0,0,0,0, 0,0,0,0, 2,2},// [][]
{1,1,0,0, 1,1,0,0, 0,0,0,0, 0,0,0,0, 2,2},// [][]
{1,1,0,0, 1,1,0,0, 0,0,0,0, 0,0,0,0, 2,2},//
{1,1,0,0, 1,1,0,0, 0,0,0,0, 0,0,0,0, 2,2},//
},
{
{1,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0, 3,0},// [] [][][][]
{1,1,1,1, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0.3},// []
{1,0,0,0, 1,0,0,0, 1,0,0,0, 1,0,0,0, 3,0},// []
{1,1,1,1, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0.3},// []
},
{
{0,1,0,0, 1,1,1,0, 0,0,0,0, 0,0,0,0, 1,2},// [] [] [][][] []
{1,0,0,0, 1,1,0,0, 1,0,0,0, 0,0,0,0, 2,1},// [][][] [][] [] [][]
{1,1,1,0, 0,1,0,0, 0,0,0,0, 0,0,0,0, 1,2},// [] []
{0,1,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0, 2,1},//
},
{
{1,1,0,0, 0,1,1,0, 0,0,0,0, 0,0,0,0, 1,2},// [][] []
{0,1,0,0, 1,1,0,0, 1,0,0,0, 0,0,0,0, 2,1},// [][] [][]
{1,1,0,0, 0,1,1,0, 0,0,0,0, 0,0,0,0, 1,2},// []
{0,1,0,0, 1,1,0,0, 1,0,0,0, 0,0,0,0, 2,1},//
},
{
{0,1,1,0, 1,1,0,0, 0,0,0,0, 0,0,0,0, 1,2},// [][] []
{1,0,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0, 2,1},// [][] [][]
{0,1,1,0, 1,1,0,0, 0,0,0,0, 0,0,0,0, 1,2},// []
{1,0,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0, 2,1},//
},
{
{0,0,1,0, 1,1,1,0, 0,0,0,0, 0,0,0,0, 1,2},// [] [] [][][] [][]
{1,0,0,0, 1,0,0,0, 1,1,0,0, 0,0,0,0, 2,1},// [][][] [] [] []
{1,1,1,0, 1,0,0,0, 0,0,0,0, 0,0,0,0, 1,2},// [][] []
{1,1,0,0, 0,1,0,0, 0,1,0,0, 0,0,0,0, 2,1},//
},
{
{1,0,0,0, 1,1,1,0, 0,0,0,0, 0,0,0,0, 1,2},// [] [][] [][][] []
{1,1,0,0, 1,0,0,0, 1,0,0,0, 0,0,0,0, 2,1},// [][][] [] [] []
{1,1,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0, 1,2},// [] [][]
{0,1,0,0, 0,1,0,0, 1,1,0,0, 0,0,0,0, 2,1},//
},
}
void print_mode_shape(int n,int m,int x,int y,int c)//打印图形形状
{
int i = 0;
int xx = x;
int yy =y;
for(i = 0;i < 16;i++)//横坐标的变化
{
shape[n][m][i];
if(shape[n][m][i] == 1)
{
printf("\033[%d;%dH",yy,xx);//指定坐标输出
printf("\033[%dm[]",c);//输出颜色
printf("\033[0m");//关闭属性
}
xx += 2; //这里之所以加2 是因为我们是用[]表示方格,一个 [ 代表一列
if(i != 0 && i% 4 ==0)
{
yy += 1;//我们用4 * 4 的空间来打印方格,当一行中四个方格打印完成之后,换行,横坐标重新变为初识横坐标
xx = x;
}
}
}
void init_shape()//初始化形状
{
srandom(time(NULL));
n_num = random() % 7; //随机生成图形
n_mode = random() % 4; //随机生成图形状态
n_color = random() % 7 + 40;//随机生成图形颜色
//在指定位置输出图形
printf_mode_shape(n_num,n_mode,n_x,n_y,n_color);
fflush(NULL);//刷新缓存
return;
}
void change_shape()//改变图形
{
int m = (n_mode + 1) % 4;//生成一个新的形状
eraser_shape(n_num,n_mode,n_x,n_y);
n_mode = m;
print_mode_shape(n_num,n_mode,n_x,n_y,n_color);
}
void move_left(int n_num,int n_mode)//左移动
{
//消除原有图形,左移一个单位重新绘制
eraser_shape(n_num,n_mode,n_x,n_y);
n_x -= 2;
print_mode_shape(n_num,n_mode,n_x,n_y,n_color);
}
void move_right(int n_num,int n_mode)//右移动
{
//消除原有图形,右移一个单位重新绘制
eraser_shape(n_num,n_mode,n_x,n_y);
n_x += 2;
print_mode_shape(n_num,n_mode,n_x,n_y,n_color);
}
void move_down(int n_num,int n_mode)//下移动
{
//消除原有图形,下移一个单位重新绘制
eraser_shape(n_num,n_mode,n_x,n_y);
n_y += 2;
print_mode_shape(n_num,n_mode,n_x,n_y,n_color);
}
void key_control()//按键控制函数
{
int ch;
while(1)
{
ch = getch(); //'^['---->ese
if(ch == 'q' || ch == 'Q')
{
break;
}
else if(ch == '\r')//判断是否是回车键
{
printf("down\n");
}
else if(ch == '\033')
{
ch = getch();// '['
if(ch == '[')
{
ch = getchar();//A,B,D,C
switch(ch)
{
case 'A'://上
change_shape();
breeak;
case 'B'://下
move_down(n_num,n_mode);
breeak;
case 'D'://左
move_left(n_num,n_mode);
breeak;
case 'C'://右
move_right(n_num,n_mode);
breeak;
}
}
}
}
}
void eraser_shape(int n,int m,int x,int y)//清除指定位置的图案
{
int i = 0;
int xx = x;
int yy = y;
for(i = 0;i < 16;i++)
{
if(i != 0 && i % 4 ==0)
{
yy++;
xx = x;
}
if(shape[n][m][i] == 1)
{
printf("\033[%d;%dH \033[0m",yy,xx);
}
//[]占两个坐标点的位置
xx += 2;
}
fflush(NULL);
}
int main()
{
//清屏
printf("\033[2J");
//隐藏图标
printf("\033[?25l");
init_shape();
key_control();
//显示光标
printf("\033[?25h");
return 0;
}