📄 function.c
字号:
#include <intrins.h>
#include <reg52.h>
#include "define.h"
#include "code.h"
#include <stdlib.h>
//******************全局变量******************
uint16 Map[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //地图
uint16 Black_Ground[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //动态显示缓存
uchar temp0,temp1,time,keyvalue=0xff;//按键判断使用的变量
uchar Auto_Down=6,keyvalue1,speed;
uchar Draw_Block_To_BG_Flag;
uint16 horiz=0;
uchar NewBlock=1;
uchar Quick_Down=0;
uchar Block_X,Block_Y; //方块当前的坐标
uchar I,J,I1,J1; //选取7行4列方块种类的变量
uchar score1=0;
//******************全局变量******************
void delay_1us(void) //1us延时函数
{
unsigned int i;
for(i=0;i<1000;i++);
}
//1ms Delayfunction
void Delay(uint16 uiDelayCount)
{
uint16 uiTempCount;
uchar uci;
for(uiTempCount=0; uiTempCount<uiDelayCount; uiTempCount++)
{
uci = 230;
while(uci--)
{
_nop_();
}
}
}
/*-----------------------------------------------------------------------
LCD_write_byte : 使用SPI接口写数据到LCD
输入参数:data :写入的数据;
command :写数据/命令选择;
-----------------------------------------------------------------------*/
void LCD_write_CMD(unsigned char ucWriteData)
{
unsigned char uci;
LCD_CE = 0;
LCD_DC = 0;
for(uci=0;uci<8;uci++)
{
if(ucWriteData & 0x80)
{
LCD_SDIN = 1;
}
else
{
LCD_SDIN = 0;
}
LCD_SCLK = 0;
ucWriteData = ucWriteData << 1;
LCD_SCLK = 1;
}
LCD_CE = 1;
}
/*-----------------------------------------------------------------------
LCD_write_byte : 使用SPI接口写数据到LCD
输入参数:data :写入的数据;
command :写数据/命令选择;
-----------------------------------------------------------------------*/
void LCD_write_Data(unsigned char ucWriteData)
{
unsigned char uci;
LCD_CE = 0;
LCD_DC = 1;
for(uci=0;uci<8;uci++)
{
if(ucWriteData & 0x80)
{
LCD_SDIN = 1;
}
else
{
LCD_SDIN = 0;
}
LCD_SCLK = 0;
ucWriteData = ucWriteData << 1;
LCD_SCLK = 1;
}
LCD_CE = 1;
}
/*-----------------------------------------------------------------------
LCD_set_XY : 设置LCD坐标函数
输入参数:X -83
Y :0-5
-----------------------------------------------------------------------*/
void LCD_set_XY(unsigned char X, unsigned char Y)
{
LCD_write_CMD(0x40 | (Y & 0X07)); // column
LCD_write_CMD(0x80 | (X & 0X7F)); // row
}
/*-----------------------------------------------------------------------
LCD_clear : LCD清屏函数
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
LCD_write_char : 显示英文字符
输入参数:c :显示的字符;
-----------------------------------------------------------------------*/
void LCD_write_char(unsigned char c)
{
unsigned char line;
c -= 32;
for (line=0; line<6; line++)
{
LCD_write_Data(font6x8[c][line]);
}
}
/*-----------------------------------------------------------------------
LCD_write_english_String : 英文字符串显示函数
输入参数:*s :英文字符串指针;
X、Y : 显示字符串的位置,x 0-83 ,y 0-5
-----------------------------------------------------------------------*/
void LCD_write_english_string(unsigned char X,unsigned char Y,char *s)
{
LCD_set_XY(X,Y);
while (*s)
{
LCD_write_char(*s);
s++;
}
}
void LCD_Clear(void)
{
uint16 uii;
LCD_set_XY(0,0);
for(uii=0; uii<6*84; uii++)//c_Lcd_Max_x * c_Lcd_Max_y
{
LCD_write_Data(0x00);
}
}
void LCD_Init(void)
{
LCD_CE = 0;
// 产生一个让LCD复位的低电平脉冲
LCD_RST = 0;
delay_1us();
LCD_RST = 1;
// 关闭LCD
delay_1us();
// 使能LCD
delay_1us();
LCD_write_CMD(0x23); // 使用扩展命令设置LCD模式
// LCD_write_CMD(0xc8); // 设置偏置电压
LCD_write_CMD(Nokia5110_vot_set);
LCD_write_CMD(0x06); // 温度校正
LCD_write_CMD(0x14); // 1:48 对比度调节
LCD_write_CMD(0x22); // 使用基本命令,采用垂直模式
LCD_Clear(); // 清屏
LCD_write_CMD(0x0c); // 设定显示模式,c正常显示,d反白显示
LCD_CE = 1;
}
void LCD_Write_Frame()
{
unsigned char line;
LCD_write_CMD(0x20); // 使用基本命令,采用水平模式
LCD_set_XY(54,5);
for(line=0; line<30; line++)
{
LCD_write_Data(0x80);
}
LCD_set_XY(54,2);
for(line=0; line<30; line++)
{
LCD_write_Data(0x20);
}
LCD_write_english_string(56,0,"NEXT");
LCD_write_english_string(56,3,"LINE");
LCD_set_XY(60,4);
LCD_write_char(48); //分数先初始化为0
LCD_write_char(48); //分数先初始化为0
LCD_write_char(48); //分数先初始化为0
LCD_write_CMD(0x22); // 使用基本命令,采用垂直模式
LCD_set_XY(51,0);
for(line=0; line<18; line++)
{
LCD_write_Data(Blackground[line]);
}
LCD_set_XY(83,0);
for(line=0; line<6; line++)
{
LCD_write_Data(Blackground[line]);
}
}
void LCD_DrawBlackGround()
{
uchar i,j,line;
uint16 temp0,temp1,line0[6],line1[6],Mask=0x8000;
LCD_write_CMD(0x22);//垂直模式
//第一列的绘图
LCD_set_XY(0,0);
for(line=0; line<18; line++)
{
LCD_write_Data(Blackground[line]);
}
for(j=0;j<16;j++)
{
for(i=0;i<6;i++)
{
if( Black_Ground[3*i]&Mask)
{temp0=0x0007;temp1=0x0005;}
else temp0=temp1=0;
if( Black_Ground[3*i+1]&Mask)
{temp0=temp0|0x0038;temp1=temp1|0x0028;}
else {temp0=temp0|0;temp1=temp1|0;}
if( Black_Ground[3*i+2]&Mask)
{temp0=temp0|0x01c0;temp1=temp1|0x0140;}
else {temp0=temp0|0;temp1=temp1|0;}
line0[i]=temp0;
line1[i]=temp1;
}
line0[0]=(line0[1]<<9)|line0[0];
line0[1]=(line0[1]>>7)|(line0[2]<<2)|(line0[3]<<11);
line0[2]=(line0[3]>>5)|(line0[4]<<4)|(line0[5]<<13);
line1[0]=(line1[1]<<9)|line1[0];
line1[1]=(line1[1]>>7)|(line1[2]<<2)|(line1[3]<<11);
line1[2]=(line1[3]>>5)|(line1[4]<<4)|(line1[5]<<13);
Mask=Mask>>1;
LCD_write_Data((uchar)line0[0]);
LCD_write_Data((uchar)(line0[0]>>8));
LCD_write_Data((uchar)line0[1]);
LCD_write_Data((uchar)(line0[1]>>8));
LCD_write_Data((uchar)line0[2]);
LCD_write_Data((uchar)(line0[2]>>8));
LCD_write_Data((uchar)line1[0]);
LCD_write_Data((uchar)(line1[0]>>8));
LCD_write_Data((uchar)line1[1]);
LCD_write_Data((uchar)(line1[1]>>8));
LCD_write_Data((uchar)line1[2]);
LCD_write_Data((uchar)(line1[2]>>8));
LCD_write_Data((uchar)line0[0]);
LCD_write_Data((uchar)(line0[0]>>8));
LCD_write_Data((uchar)line0[1]);
LCD_write_Data((uchar)(line0[1]>>8));
LCD_write_Data((uchar)line0[2]);
LCD_write_Data((uchar)(line0[2]>>8));
}
}
void Load_Pic()
{
uint16 temp,pic;
if(!NewBlock) //方块没有触底和与其他方块接触以后就不重新载入方块
return;
temp=rand();
I1=(uchar)((temp>>8)%7); ////预览中的方块图案
J1=((uchar)temp%4); //预览中的方块图案
pic=Pic[I][J]; //在显示先前的方块之后,在确定方块下落完毕之后在是I=I1,J=J1
Black_Ground[0]=((pic>>6)&0x03c0);
Black_Ground[1]=((pic>>2)&0x03c0);
Black_Ground[2]=((pic<<2)&0x03c0);
Black_Ground[3]=((pic<<6)&0x03c0);
NewBlock=0;
Block_X=8;//记录最新方块初始位置
Block_Y=0;//按Black_Ground数组字节顺序记录,不是按5510液晶坐标方向记录
}
uchar Attack_Block() //接触到游戏背景中的方块
{
uchar i; //已经假设向左,向右或向下移动过,但是在判定游戏是否结束的时候并没有先前移动
for(i=15;i>0;i--)
{
if(((~Black_Ground[i])&Map[i])!=Map[i])
return 1; //移动1次方块和固定背景有接触
}
return 0; //移动1次方块和固定背景无接触
}
uchar Attack_Frame() //方块接触到游戏界面的边框(不包含底部)
{ //已经假设向左,向右移动过
if(horiz!=0)
{horiz=0;return 1;}//接触到边框的时候格点对应的数据Map一个16位的变量对应15位或0位为1
else return 0;//上面假设左移或者右移一位,则会对应有溢出或者借位对CY值1
}
uchar Attack_Bottom() //接触到底部(已经将设向下移动过一次)
{//在方块刚出现挨着顶部的时候,左右的移动,同样Black_Ground[0]不为0,形成误判
if(Draw_Block_To_BG_Flag==Down)//在设计如何方块下移移动的时候,将Black_Ground[16]的高字
if(Black_Ground[0]!=0)//和低字交换而Black_Ground[15]的数值循环到Black_Ground[0],
return 1; //只要检测到再次Black_Ground[0]不为0就知道上次下移是越界了
return 0;
}
void Transformer()
{
uint16 pic,i;
J=(J+1)%4; //循环求取当前方块4中模块中的后一种
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -