实战项目十的更新代码
文章目录
- main.c
- uesr_global.c
- user_print.c
- user_print.h
- user_control.c
- user_control.h
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.8s
while(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.8s
int score_x = 45;
int score_y = 18;
int level_x = 45;
int level_y = 22;
//存储坐标点信息
int matrix[24][28] = {0};
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);//刷新缓存
}
int move_down(int num,int mode)
{
if(dtnamic_y + (4 - shape[num][mode][17] )-1 >= 29)//已经到底端了
{
store_current_shape();//已经触底 或 越界,不能再向下移动
return 1;
}
//清除现有图形
erase_last_shape(num,mode,dynamic_x,dynamic_y);
dynamic_y++;
print_mode_shape(num,mode,dynamic_x,dynamic_y,dynamic_color);
return 0;
}
void store_current_shape()//存储到底端的方块
{
int m_line = dynalic_y - 6;
int m_column = dynamic_x - 12;
int i = 0;
for(i = 0;i < 16;i++)//回到初始状态
{
m_line++;
m_column = dynamic_x - 12;
}
if(shape[dynamic_num][dynamic_mode][i] == 1)
{
matrix[m_line][m_column] = dynamic_color;
matrix[m_line][m_column + 1] = dynamic_color;
}
m_column += 2;
}
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 matrix[24][28];
//到底端存储方块信息
extern void store_current_shape();
//三维数组存储俄罗斯方块
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 int move_dowm(int num,int mode);
#endif
user_control.c
#include "user_control.h"
int getch()//获得一个字符,不回显;切记是1个字符
{
struct termios tm,tm_old;
//1.获得用户输入的属性到 tm_old
tcgetattr(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;
//设置定时器启动的初始化值n
value.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
运行上面所有的代码文件就可以实现方块的下落。