文章目录
- 🍊自我介绍
- 🍊方块自动下落(下)
- main.c
- uesr_global.c
- user_print.c
- user_print.h
你的点赞评论就是对博主最大的鼓励
当然喜欢的小伙伴可以:点赞+关注+评论+收藏(一键四连)哦~
🍊自我介绍
Hello,大家好,我是小珑也要变强(也是小珑),我是易编程·终身成长社群的一名“创始团队·嘉宾” 和“内容共创官” ,现在我来为大家介绍一下有关物联网-嵌入式方面的内容。
🍊方块自动下落(下)
根据之前的内容,我们对于之前写的文件进行稍加修改和补充使其实现方块的自动下落。
main.c
#include <stdio.h>
#include <termios.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include "user_print.h"
#include "user_control.h"extern int tm;extern int score_x;
extern int score_y;
extern int level_x;
extern int level_y;void print_start_ui()
{printf("\33[2J");//清屏int i;for(i = 0;i < 47;i++)//输出黄色最顶行。最低行{printf("\033[%d;%dH\033[43m \033[0m",5,i+10);printf("\033[%d;%dH\033[43m \033[0m",30,i+10);}for(i = 0;i < 26;i++){printf("\033[%d;%dH\033[43m \033[0m",i+5,10);printf("\033[%d;%dH\033[43m \033[0m",i+5,40);printf("\033[%d;%dH\033[43m \033[0m",i+5,56);}for(i = 0;i < 17;i++)//输出用户下一图形分割行{printf("\033[%d;%dH\033[43m \033[0m",12,40+i);}printf("\033[%d;%dH分数:\033[0m",score_y,score_x);//输出分数printf("\033[%d;%dH分数:\033[0m",level_y,level_x);//输出等级
}void init_game_ui()
{print_start_ui();//输出窗体界面getch();//等待用户输出,然后程序开始运行//获取随机数;设置随机数种子srand(timr(NULL));dynamic_num = random()%7;dynamic_mode = random()%4;dynamic_color = random()%7+40;dynamic_x = init_x;dynamic_y = init_y;//生成图形print_mode_shape(dynamic_num,dynamic_mode,dynamic_x,dynamic_y,dynamic_color);print_next_shape();printf("\033[?25l");
}void sig_handler(int signum)
{move_down(dynamic_num,dynamic_mode);//图形向下移动
}
int main()
{init_game_ui();signal(SIGALRM,sig_handler);alarm_us(tm);//tm = 0.8swhile(1);return 0;
}
uesr_global.c
#include "user_print.h"//下一次出现图形的变量
int next_num = 0;
int next_mode = 0;
int next_color = 0;//初始化图形坐标
int init_x = 24;
int init_y = 6;//右侧图形的坐标位置
int next_x = 46;
int next_y = 8;//实时动态变化的坐标位置(图形下移的时候的坐标变化,使用的变量)
int dynamic_x = 0;
int dynamic_y = 0;//实时动态变化的具体形状,颜色,模式
int dynamic_num = 0;
int dynamic_mode = 0;
int dynamic_color = 0;//设置定时器时间
int tm = 800000;//0.8sint score_x = 45;
int score_y = 18;
int level_x = 45;
int level_y = 22;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},// },
}
user_print.c
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <stdlib.h>
#include "user_print.h"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 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);
}void print_next_shape()//右侧准备生成下一个位置的图形并输出
{ //擦除上一次图形erase_last_shape(next_num,next_mode,next_x,next_y);next_num = random() % 7; //随机生成图形next_mode = random() % 4; //随机生成图形状态next_color = random() % 7 + 40;//随机生成图形颜色//在指定位置输出图形printf_mode_shape(next_num,next_mode,next_x,next_y,next_color);fflush(NULL);//刷新缓存}void move_down(int num,int mode)
{erase_last_shape(num,mode,dynamic_x,dynamic_y);dynamic_y++;print_mode_shape(num,mode,dynamic_x,dynamic_y,dynamic_color);
}
user_print.h
#ifndef _USER_PRINT_H_
#define _USER_PRINT_H_extern int next_num;
extern int next_mode;
extern int next_color;//初始化图形坐标
extern int init_x;
extern int init_y;//右侧图形的坐标位置
extern int next_x;
extern int next_y;//实时动态变化的坐标位置(图形下移的时候的坐标变化,使用的变量)
extern int dynamic_x;
extern int dynamic_y;//实时动态变化的具体形状,颜色,模式
extern int dynamic_num;
extern int dynamic_mode;
extern int dynamic_color;//三维数组存储俄罗斯方块
extern int shape[7][4][18];extern void print_mode_shape(int n,int m,int x,int y,int c);
extern void print_next_shape();
extern void erase_last_shape(int n,int m,int x,int y);
extern void move_dowm(int num,int mode);#endif
user_control.c
#include "user_control.h"int getch()//获得一个字符,不回显;切记是1个字符
{struct termios tm,tm_old;//1.获得用户输入的属性到 tm_oldtcgetattr(0,&tm_old);//2.获取原始输入的属性cfmakeraw(&tm);//3.把输入的属性设置到终端上tcsetattr(0,0,&tm);//4.读取用户输入的一个字符int ch = getchar();//5.恢复正常输入tcsetattr(0,0,&tm_old);return ch;
}//微秒定时器,当定时器启动后,每隔一段时间会发送SIGALRM信号
void alarm_us(int n)
{struct itimerval value;//设置定时器启动的初始化值nvalue.it_value.tv_sec = 0;value.it_value.tv_usec = n;//设置定时器启动的间隔时间value.it_interval.tv_sec = 0;value.it_interval.tv_usec = n;//n微秒后启动定时器,同时发送SIGALRM信号,接下来每隔n微秒发送一次SIGALRAM信号setitimer(ITIMER_REAL,&value,NULL);
}
user_control.h
#ifndef _USER_CONTROL_H_
#define _USER_CONTROL_H_#include <stdio.h>
#include <termios.h>
#include <signal.h>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>extern int getch();
extern alarm_us(int n);#endif
运行上面所有的代码文件就可以实现方块的下落。