看到满屏的贪吃蛇,我也来开源一个Ti开发板(TM4C1294)的游戏
将简化版的管道鸟,根据自己玩的经历,在cortexm4开发板上重新撸了一边,
设计思路:
设计思路:将小鸟固定在一条竖线上规定其只能上下移动并记录下其最高和最低的坐标,管道从屏幕右边开始出现依次移动到屏幕左边消失,用户通过按键控制小鸟上下移动,小鸟也有一个自然下降的速度,用户要控制小鸟在前进过程中不碰到管道的边和屏幕的边界。
主要的方法是:
画管道按横坐标宽60个像素将屏幕分成4等分,第一块产生随机高度的管道,第二,三,四块在管道运动到相应的块结束时接着上一块的结尾显示;
画小鸟选取15x15像素的一块区域,以中心点计算出鸟上各个点的坐标,在画鸟时只需知道其中一个点就可以画出鸟。
画的鸟如下图:
移动鸟,先将前一步鸟所在的位置的图像消除,再根据中心点的位置变化计算出其他点的位置画出新的鸟位置。
鸟中心坐标改变是靠按键中断来实现;
重新开始是在屏幕上确定一块区域当触摸那块区域后从新开始。
源代码
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_epi.h"
#include "inc/hw_ints.h"
#include "driverlib/epi.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/pin_map.h"
#include "driverlib/systick.h"
#include "driverlib/interrupt.h"
#include "driverlib/ssi.h"
#include "driverlib/fpu.h"
#include "utils/uartstdio.h"
#include "TFTinit/TFT_400x240_OTM4001A_16bit.h"
//#include "TFTinit/picture.h"
#include "TOUCHinit/TOUCH_TSC2046.h"
#include "EPIinit/EPIinit.h"
#include "IQmath/IQmathLib.h"
//---
//-----mi
#define _NOP() _nop()
//*********************************************************************
//*********************************************************************
#define I2C0_MASTER_BASE 0x40020000
#define I2C0_SLAVE_BASE 0x40020000
//*********************************************************************
// 地址、寄存器等定义部分
//*********************************************************************
//*********************************************************************
//
// 设定slave(从)模块的地址。这是一个7-bit的地址加上RS位,具体形式如下:
// [A6:A5:A4:A3:A2:A1:A0:RS]
// RS位是一个指示位,如果RS=0,则说明是主发送数据,从接收数据;RS=1说明是主接收数据,从发送数据
//
//*********************************************************************
//U21控制4个米字管和特殊管脚的亮灭
#define I2C0_ADDR_TUBE_SEL 0x30 //00110000
//U22控制米字管7~14管脚对应的码段
#define I2C0_ADDR_TUBE_SEG_LOW 0x32 //00110010
//U23控制米字管15~18管脚对应的码段
#define I2C0_ADDR_TUBE_SEG_HIGH 0x34 //00110100
//U24控制LED光柱
//PCA9557内部寄存器,也称子地址
#define PCA9557_REG_INPUT 0x00
#define PCA9557_REG_OUTPUT 0x01
#define PCA9557_REG_PolInver 0x02
#define PCA9557_REG_CONFIG 0x03
//*************************************************************************************
#define NUM 0
//IIC 接受数据临时缓冲区
unsigned char I2C_RECV_DATA[] =
{
0x00,
0x00,
0x00,
0x00,
0x00,
0x00
};
/*******************************************
拉高 SDA 信号
********************************************/
void I2C_Set_sda_high( void )
{
GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_3,GPIO_PIN_3); //拉高PB3
_NOP();
_NOP();
return;
}
/*******************************************
拉低SDA 信号
********************************************/
void I2C_Set_sda_low ( void )
{
GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_3,0X00000000); //拉低PB3
_NOP();
_NOP();
return;
}
/*******************************************
拉高SCL 信号
********************************************/
void I2C_Set_scl_high( void )
{
GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_2,GPIO_PIN_2); //拉高PB2
_NOP();
_NOP();
return;
}
/*******************************************
拉低SCL 信号/
********************************************/
void I2C_Set_scl_low ( void )
{
GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_2,0X00000000); //拉低PB2
_NOP();
_NOP();
return;
}
/*******************************************
IIC 信号结束信号函数
********************************************/
void I2C_STOP(void)
{
int i;
I2C_Set_sda_low();
for(i = NUM;i > 0;i--);
I2C_Set_scl_low();
for(i = NUM;i > 0;i--);
I2C_Set_scl_high();
for(i = NUM;i > 0;i--);
I2C_Set_sda_high();
for(i = NUM+1;i > 0;i--);
return;
}
/*******************************************
IIC 信号初始化
********************************************/
void I2C_Initial( void )
{
I2C_Set_scl_low();
I2C_STOP();
return;
}
/*******************************************
IIC 信号起始信号函数
********************************************/
void I2C_START(void)
{
int i;
I2C_Set_sda_high();
for(i = NUM;i > 0;i--);
I2C_Set_scl_high();
for(i = NUM;i > 0;i--);
I2C_Set_sda_low();
for(i = NUM;i > 0;i--);
I2C_Set_scl_low();
return;
}
/*******************************************
IIC 获取应答函数
********************************************/
int I2C_GetACK(void)
{
int j;
_NOP();
_NOP();
I2C_Set_scl_low();
for(j = NUM;j> 0;j--);
I2C_Set_scl_high();
for(j = NUM;j> 0;j--);
I2C_Set_sda_low();
for(j = NUM;j > 0;j--);
I2C_Set_scl_low();
return 1;
}
/*******************************************
IIC 设置应答函数
********************************************/
void I2C_SetNAk(void)
{
I2C_Set_scl_low();
I2C_Set_sda_high();
I2C_Set_scl_high();
I2C_Set_scl_low();
return;
}
/*******************************************
IIC 发送字节函数
参数 1:要发送字节值
return :无返回
********************************************/
void I2C_TxByte(unsigned char nValue)
{
int i;
int j;
for(i = 0;i < 8;i++)
{
if(nValue & 0x80)
I2C_Set_sda_high();
else
I2C_Set_sda_low();
for(j = NUM;j > 0;j--);
I2C_Set_scl_high();
nValue <<= 1;
for(j = NUM;j > 0;j--);
I2C_Set_scl_low();
}
return;
}
/*******************************************
IIC 接收字节函数
参数 无
return :无返回
********************************************/
unsigned char I2C_RxByte(void)
{
GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_3);//配置PB3为输入口
unsigned char nTemp=0 ;
int i;
I2C_Set_sda_high();
_NOP();
_NOP();
_NOP();
_NOP();
for(i = 0;i < 8;i++)
{
I2C_Set_scl_high(); //模拟SCL信号
if(GPIOPinRead(GPIO_PORTB_BASE,GPIO_PIN_3) == 0x18) //依次判断PB3(SDA)输入
{
nTemp |= (0x01 << (7-i)); //8位SDA数据哪一位为高就置1
}
I2C_Set_scl_low();
//Delay5us();
}
return nTemp;
}
/*******************************************
IIC 发送数组函数
参数 1 num : 发送字节数
2 device_addr : iic目标地址
3 *data :发送数组地址
return :无返回
********************************************/
void i2c_write(int num, unsigned char device_addr,unsigned char *data)
{
int i = 0;
int count = num;
unsigned char *send_data = data;
unsigned char write_addr = device_addr;
I2C_Set_scl_high();
for(i = NUM;i > 0;i--);
I2C_Set_sda_high();
for(i = NUM;i > 0;i--);
for(i = 0;i < count;i++)
{
I2C_START(); //模拟I2C写数据的时序
I2C_TxByte(write_addr);
I2C_GetACK();
I2C_TxByte(send_data[i]);
I2C_GetACK();
i++;
I2C_TxByte(send_data[i]);
I2C_GetACK();
I2C_STOP();
}
}
/*******************************************
IIC 读取数组函数
参数 1 num : 发送字节数
2 device_addr : iic目标地址
3 *data :接收数组地址
return :无返回
********************************************/
void i2c_read(int num, unsigned char device_addr,unsigned char *data)
{
int i = 0;
int count = num;
unsigned char *send_data = data;
unsigned char read_addr = device_addr;
I2C_Set_scl_high();
for(i = NUM;i > 0;i--);
I2C_Set_sda_high();
for(i = NUM;i > 0;i--);
for(i = 0; i < count;i++)
{
I2C_START(); //模拟I2C读数据
I2C_TxByte((read_addr - 1));
I2C_GetACK();
I2C_TxByte(send_data[2*i]);
I2C_GetACK();
I2C_START();
I2C_TxByte(read_addr);
I2C_GetACK();
I2C_RECV_DATA[i] = I2C_RxByte();
data[2*i+1]=I2C_RECV_DATA[i];
I2C_SetNAk();
I2C_STOP();
}
}
//*********************************************************************/
//*********************************************************************/
//******配置I2C0模块的IO引脚,**********************************************/
void I2C0GPIOBEnable(void)
{ // Enable GPIO portB containing the I2C pins (PB2&PB3)
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_2|GPIO_PIN_3);
}
//******配置PCA9557芯片中连接米字管的各引脚为输出***********************************/
void I2C0DeviceInit(void)
{
unsigned char dataBuf[2] = {PCA9557_REG_CONFIG, 0x00};
i2c_write(2,I2C0_ADDR_TUBE_SEL,dataBuf);
i2c_write(2,I2C0_ADDR_TUBE_SEG_LOW,dataBuf);
i2c_write(2,I2C0_ADDR_TUBE_SEG_HIGH,dataBuf);
}
/*******设置米字管的管选信号**************************************************/
void I2C0TubeSelSet(char data)
{ //选择1、2、3、4、5哪个米字管亮
unsigned char dataBuf[2] = {PCA9557_REG_OUTPUT, data};
i2c_write(2,I2C0_ADDR_TUBE_SEL,dataBuf);
}
/*******点亮米字管的相应码段**************************************************/
void I2C0TubeLowSet(char data)
{ //点亮7-14管脚对应的码段
unsigned char dataBuf[2] = {PCA9557_REG_OUTPUT, data};
i2c_write(2,I2C0_ADDR_TUBE_SEG_LOW,dataBuf);
}
void I2C0TubeHighSet(char data)
{ //点亮15-18管脚对应的码段
unsigned char dataBuf[2] = {PCA9557_REG_OUTPUT, data};
i2c_write(2,I2C0_ADDR_TUBE_SEG_HIGH,dataBuf);
}
//********预设码段值,方便查找************************************************
static const char tubeCodeTable[10][2]=
{ // SegmLow, SegHigh
{ 0x10, 0x3E }, // 0
{ 0x00, 0x18 }, // 1
{ 0x70, 0x2C }, // 2
{ 0x70, 0x26 }, // 3
{ 0x60, 0x32 }, // 4
{ 0x70, 0x16 }, // 5
{ 0x70, 0x1E }, // 6
{ 0x00, 0x26 }, // 7
{ 0x70, 0x3E }, // 8
{ 0x70, 0x36 }, // 9
// { 0x60, 0x3E }, // a
};
/**************************************************************************/
unsigned char a[2];
void setnumber(int value)
{
switch(value){
case 0:{ a[0]=tubeCodeTable[0][0];a[1]=tubeCodeTable[0][1];break;}
case 1:{ a[0]=tubeCodeTable[1][0];a[1]=tubeCodeTable[1][1];break;}
case 2:{ a[0]=tubeCodeTable[2][0];a[1]=tubeCodeTable[2][1];break;}
case 3:{ a[0]=tubeCodeTable[3][0];a[1]=tubeCodeTable[3][1];break;}
case 4:{ a[0]=tubeCodeTable[4][0];a[1]=tubeCodeTable[4][1];break;}
case 5:{ a[0]=tubeCodeTable[5][0];a[1]=tubeCodeTable[5][1];break;}
case 6:{ a[0]=tubeCodeTable[6][0];a[1]=tubeCodeTable[6][1];break;}
case 7:{ a[0]=tubeCodeTable[7][0];a[1]=tubeCodeTable[7][1];break;}
case 8:{ a[0]=tubeCodeTable[8][0];a[1]=tubeCodeTable[8][1];break;}
case 9:{ a[0]=tubeCodeTable[9][0];a[1]=tubeCodeTable[9][1];break;}
}
}
void I2C0Tubeset()
{
I2C0TubeLowSet(a[0]);
I2C0TubeHighSet(a[1]);
}
void showvalue(int value)
{
setnumber(value);
I2C0Tubeset();
}
//--
void GPIOIntInitial(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
SysCtlGPIOAHBEnable(SYSCTL_PERIPH_GPIOD);//
GPIOPinTypeGPIOInput(GPIO_PORTD_BASE,GPIO_PIN_0);
GPIOIntTypeSet(GPIO_PORTD_BASE,GPIO_PIN_0,GPIO_LOW_LEVEL);
GPIOIntEnable(GPIO_PORTD_BASE,GPIO_INT_PIN_0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);
SysCtlGPIOAHBEnable(SYSCTL_PERIPH_GPION);//
GPIOPinTypeGPIOInput(GPIO_PORTN_BASE,GPIO_PIN_2);
GPIOIntTypeSet(GPIO_PORTN_BASE,GPIO_PIN_2,GPIO_LOW_LEVEL);
GPIOIntEnable(GPIO_PORTN_BASE,GPIO_INT_PIN_2);
}
// System clock rate in Hz.
//
//*****************************************************************************
//----------------------wave
static volatile unsigned long g_ulTickCount;
#define SERIES_LENGTH 240
typedef struct _Series
{
uint32_t xAxis;
int32_t data;
}tSeries;
tSeries g_cSeries[241];
int x,y,i;
#define TICKS_PER_SECOND 40
#define FSECONDS_PER_TICK (1.0F / (float)TICKS_PER_SECOND)
#ifndef M_PI
#define M_PI 3.14159265358979323846F
#endif
void
SysTickIntHandler(void)
{
//
// Update our tick counter.
//
g_ulTickCount++;
if(g_ulTickCount>=320)
g_ulTickCount = 0;
}
//-------------------------------------------------
uint32_t g_ui32SysClock;
extern uint32_t GetData[11];
void qingniao(int x,int y);
void huaniao(int x,int y);
uint32_t TouchXData[11];
uint32_t TouchYData[11];
uint32_t TouchZData[11]; //Z is for pressure measurement
void delay()
{
int ui32Loop0;
for(ui32Loop0=0;ui32Loop0<1000000;ui32Loop0++) //delay
{;}
}
void delay2()
{
int ui32Loop0;
for(ui32Loop0=0;ui32Loop0<500000;ui32Loop0++) //delay
{;}
}
//*****************************************************************************
//
// Configure the UART and its pins. This must be called before UARTprintf().
//
//*****************************************************************************
void
ConfigureUART(void)
{
//
// Enable the GPIO Peripheral used by the UART.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Enable UART0
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
//
// Configure GPIO Pins for UART mode.
//
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Initialize the UART for console I/O.
//
UARTStdioConfig(0, 115200, g_ui32SysClock);
}
//---------------------------------------
///
///中断加速上升
//-----
void
GPIOD(void)
{
UARTprintf("y=");
unsigned long Status;
Status=GPIOIntStatus(GPIO_PORTD_BASE,true);
if(Status==GPIO_INT_PIN_0)
{
qingniao(x,y);
y=y-10;
huaniao(x,y);
delay2();
}
GPIOIntClear(GPIO_PORTD_BASE,Status);
}
//---------------------------------------
//---------------------------------------
///
///中断向下移动
//-----
void
GPION(void)
{
unsigned long Status;
UARTprintf("y=%d,data=%d\n",y,g_cSeries[x].data);
Status=GPIOIntStatus(GPIO_PORTN_BASE,true);
if(Status==GPIO_INT_PIN_2)
{
qingniao(x,y);
y=y+10;
huaniao(x,y);
delay2();
}
GPIOIntClear(GPIO_PORTD_BASE,Status);
}
//----------画出小鸟
//----------
void huaniao(int x,int y)
{
TFTLCD_DrawPoint(x-7,y+1,BLUE);
TFTLCD_DrawPoint(x-7,y+3,BLUE);
TFTLCD_DrawPoint(x-6,y-2,BLUE);
TFTLCD_DrawPoint(x-6,y-1,BLUE);
TFTLCD_DrawPoint(x-6,y,BLUE);
TFTLCD_DrawPoint(x-6,y+2,BLUE);
TFTLCD_DrawPoint(x-6,y+4,BLUE);
TFTLCD_DrawPoint(x-6,y+5,BLUE);
TFTLCD_DrawPoint(x-5,y-5,BLUE);
TFTLCD_DrawPoint(x-5,y-4,BLUE);
TFTLCD_DrawPoint(x-5,y-3,BLUE);
TFTLCD_DrawPoint(x-5,y-2,BLUE);
TFTLCD_DrawPoint(x-5,y+5,BLUE);
TFTLCD_DrawPoint(x-5,y+6,BLUE);
TFTLCD_DrawPoint(x-4,y-6,BLUE);
TFTLCD_DrawPoint(x-4,y+6,BLUE);
TFTLCD_DrawPoint(x-4,y+7,BLUE);
TFTLCD_DrawPoint(x-3,y-5,BLUE);
TFTLCD_DrawPoint(x-3,y,BLUE);
TFTLCD_DrawPoint(x-3,y+1,BLUE);
TFTLCD_DrawPoint(x-3,y+7,BLUE);
TFTLCD_DrawPoint(x-2,y-5,BLUE);
TFTLCD_DrawPoint(x-2,y-5,BLUE);
TFTLCD_DrawPoint(x-2,y-4,BLUE);
TFTLCD_DrawPoint(x-2,y-1,BLUE);
TFTLCD_DrawPoint(x-2,y+2,BLUE);
TFTLCD_DrawPoint(x-2,y+7,BLUE);
TFTLCD_DrawPoint(x-1,y-6,BLUE);
TFTLCD_DrawPoint(x-1,y-1,BLUE);
TFTLCD_DrawPoint(x-1,y,BLUE);
TFTLCD_DrawPoint(x-1,y+1,BLUE);
TFTLCD_DrawPoint(x-1,y+2,BLUE);
TFTLCD_DrawPoint(x-1,y+7,BLUE);
TFTLCD_DrawPoint(x,y-5,BLUE);
TFTLCD_DrawPoint(x,y-1,BLUE);
TFTLCD_DrawPoint(x,y+2,BLUE);
TFTLCD_DrawPoint(x,y+7,BLUE);
TFTLCD_DrawPoint(x+1,y-4,BLUE);
TFTLCD_DrawPoint(x+1,y-3,BLUE);
TFTLCD_DrawPoint(x+1,y,BLUE);
TFTLCD_DrawPoint(x+1,y+1,BLUE);
TFTLCD_DrawPoint(x+1,y+6,BLUE);
TFTLCD_DrawPoint(x+2,y-2,BLUE);
TFTLCD_DrawPoint(x+2,y-1,BLUE);
TFTLCD_DrawPoint(x+2,y+5,BLUE);
TFTLCD_DrawPoint(x+3,y-1,BLUE);
TFTLCD_DrawPoint(x+3,y,BLUE);
TFTLCD_DrawPoint(x+3,y+1,BLUE);
TFTLCD_DrawPoint(x+3,y+2,BLUE);
TFTLCD_DrawPoint(x+3,y+3,BLUE);
TFTLCD_DrawPoint(x+3,y+4,BLUE);
TFTLCD_DrawPoint(x+4,y,BLUE);
TFTLCD_DrawPoint(x+4,y+4,BLUE);
TFTLCD_DrawPoint(x+5,y+1,BLUE);
TFTLCD_DrawPoint(x+5,y+3,BLUE);
TFTLCD_DrawPoint(x+6,y+2,BLUE);
}
//------------------------
//-----------------------消除小鸟
void qingniao(int x,int y)
{
TFTLCD_DrawPoint(x-7,y+1,BLACK);
TFTLCD_DrawPoint(x-7,y+3,BLACK);
TFTLCD_DrawPoint(x-6,y-2,BLACK);
TFTLCD_DrawPoint(x-6,y-1,BLACK);
TFTLCD_DrawPoint(x-6,y,BLACK);
TFTLCD_DrawPoint(x-6,y+2,BLACK);
TFTLCD_DrawPoint(x-6,y+4,BLACK);
TFTLCD_DrawPoint(x-6,y+5,BLACK);
TFTLCD_DrawPoint(x-5,y-5,BLACK);
TFTLCD_DrawPoint(x-5,y-4,BLACK);
TFTLCD_DrawPoint(x-5,y-3,BLACK);
TFTLCD_DrawPoint(x-5,y-2,BLACK);
TFTLCD_DrawPoint(x-5,y+5,BLACK);
TFTLCD_DrawPoint(x-5,y+6,BLACK);
TFTLCD_DrawPoint(x-4,y-6,BLACK);
TFTLCD_DrawPoint(x-4,y+6,BLACK);
TFTLCD_DrawPoint(x-4,y+7,BLACK);
TFTLCD_DrawPoint(x-3,y-5,BLACK);
TFTLCD_DrawPoint(x-3,y,BLACK);
TFTLCD_DrawPoint(x-3,y+1,BLACK);
TFTLCD_DrawPoint(x-3,y+7,BLACK);
TFTLCD_DrawPoint(x-2,y-5,BLACK);
TFTLCD_DrawPoint(x-2,y-5,BLACK);
TFTLCD_DrawPoint(x-2,y-4,BLACK);
TFTLCD_DrawPoint(x-2,y-1,BLACK);
TFTLCD_DrawPoint(x-2,y+2,BLACK);
TFTLCD_DrawPoint(x-2,y+7,BLACK);
TFTLCD_DrawPoint(x-1,y-6,BLACK);
TFTLCD_DrawPoint(x-1,y-1,BLACK);
TFTLCD_DrawPoint(x-1,y,BLACK);
TFTLCD_DrawPoint(x-1,y+1,BLACK);
TFTLCD_DrawPoint(x-1,y+2,BLACK);
TFTLCD_DrawPoint(x-1,y+7,BLACK);
TFTLCD_DrawPoint(x,y-5,BLACK);
TFTLCD_DrawPoint(x,y-1,BLACK);
TFTLCD_DrawPoint(x,y+2,BLACK);
TFTLCD_DrawPoint(x,y+7,BLACK);
TFTLCD_DrawPoint(x+1,y-4,BLACK);
TFTLCD_DrawPoint(x+1,y-3,BLACK);
TFTLCD_DrawPoint(x+1,y,BLACK);
TFTLCD_DrawPoint(x+1,y+1,BLACK);
TFTLCD_DrawPoint(x+1,y+6,BLACK);
TFTLCD_DrawPoint(x+2,y-2,BLACK);
TFTLCD_DrawPoint(x+2,y-1,BLACK);
TFTLCD_DrawPoint(x+2,y+5,BLACK);
TFTLCD_DrawPoint(x+3,y-1,BLACK);
TFTLCD_DrawPoint(x+3,y,BLACK);
TFTLCD_DrawPoint(x+3,y+1,BLACK);
TFTLCD_DrawPoint(x+3,y+2,BLACK);
TFTLCD_DrawPoint(x+3,y+3,BLACK);
TFTLCD_DrawPoint(x+3,y+4,BLACK);
TFTLCD_DrawPoint(x+4,y,BLACK);
TFTLCD_DrawPoint(x+4,y+4,BLACK);
TFTLCD_DrawPoint(x+5,y+1,BLACK);
TFTLCD_DrawPoint(x+5,y+3,BLACK);
TFTLCD_DrawPoint(x+6,y+2,BLACK);
}
void main()
{
x=120;
y=240;
i=0;
uint16_t ui32Loop = 0,pp= 0;
int start=0;
int end=0;
int fenshu[4]=0;
int high=0;
int zaici=0;
int t1,t2,t3,t4;
int p1=180,p2=120,p3=60;
int b1=0,b2=0,b3=0;
int c=0;
//-----------------------------------
uint16_t ulItemCount=0,FinishCalculateFlag=0,ulLastTickCount=0;
float fElapsedTime;
_iq24 fRadians,fSine;
FPUEnable();
FPULazyStackingEnable();
//
// Run from the PLL at 120 MHz.
//
g_ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_480), 120000000);
GPIOIntInitial();
IntPrioritySet(INT_GPION, 0x20);
IntPrioritySet(INT_GPIOD, 0x80);
I2C0GPIOBEnable();//配置I2C0模块的IO引脚
I2C0DeviceInit();//配置PCA9557芯片中连接米字管的各引脚为输出
IntMasterEnable();
SysTickPeriodSet(g_ui32SysClock / TICKS_PER_SECOND);
IntMasterEnable();
SysTickEnable();
ConfigureUART();
EPIGPIOinit();
TOUCH_TSC2046init(g_ui32SysClock);
GPIOIntEnable(GPIO_PORTB_BASE,GPIO_INT_PIN_0);
GPIOIntTypeSet(GPIO_PORTB_BASE,GPIO_PIN_0,GPIO_FALLING_EDGE);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_PIN_0);
UARTprintf("TFTLCD touch screen test\n");
UARTprintf("EPI Type: host-bus 16-bit interface\n");
UARTprintf("SSI Type: SSI0 8bit\n");
TFT_400x240_OTM4001Ainit(g_ui32SysClock);
SSIDataPut(SSI0_BASE,0xd0);
IntEnable(INT_GPION);
IntEnable(INT_GPIOD);
while(1)
{
//ulLastTickCount = g_ulTickCount;
if(ulItemCount == SERIES_LENGTH)
{
FinishCalculateFlag = 1;
}
//
// Otherwise, the series data buffer is less than full so just
// increment the count of data points.
//
else
{
g_cSeries[ulItemCount].xAxis = ++ulItemCount;
g_cSeries[239].data=100;
}
//
// Compute the elapsed time in decimal seconds, in floating point
// format.
//
fElapsedTime = (float)g_ulTickCount * FSECONDS_PER_TICK;
// Convert the time to radians.
//
// fRadians = fElapsedTime * (float)M_PI;
fRadians = _IQ24(fElapsedTime * (float)M_PI); //_IQ24 converts a floating-point constant or variable into an IQ number.
//
// Adjust the period of the wave. This will give us a wave period
// of 4 seconds, or 0.25 Hz. This number was chosen arbitrarily to
// provide a nice looking wave on the display.
//
// fRadians /= 2.0F;
fRadians = _IQdiv2(fRadians); //_IQdiv2 divides fRadians by two.
// fRadians = _IQdiv4(fRadians); //Divides fRadians by four.
//
// Compute the sine. Multiply by 0.5 to reduce the amplitude.
//
fSine = _IQ24sin(fRadians); //Computes the sine of the input value.
if(!FinishCalculateFlag)
{
}
else
{
/*/----------------------------------
*
*
* 开始画管道
*
----------------------------------------*/
for(ui32Loop=240;ui32Loop>0;ui32Loop--)
{
int k;
for(k=0;k<80000;k++)
{
}
if(ui32Loop==210)
{
c++;
c=c%2;
}
int num;
////-------------180到240的管道
if(g_cSeries[ui32Loop].data>20)
{
g_cSeries[ui32Loop-1].data = g_cSeries[ui32Loop].data;
TFTLCD_DrawPoint(g_cSeries[ui32Loop-1].xAxis,g_cSeries[ui32Loop-1].data,GREEN);
TFTLCD_DrawPoint(g_cSeries[ui32Loop-1].xAxis,g_cSeries[ui32Loop-1].data+50,GREEN);//设置管子中间夹缝是50个像素
num=g_cSeries[ui32Loop-1].data;
for(;num>0;num--)
{
TFTLCD_DrawPoint(g_cSeries[ui32Loop-1].xAxis,num,GREEN);
}
num=g_cSeries[ui32Loop-1].data+50;
for(;num<480;num++)
{
TFTLCD_DrawPoint(g_cSeries[ui32Loop-1].xAxis,num,GREEN);
}
}
else
{
num=g_cSeries[ui32Loop-1].data;
for(;num>0;num--)
{
TFTLCD_DrawPoint(g_cSeries[ui32Loop-1].xAxis,num,BLACK);
}
num=g_cSeries[ui32Loop-1].data+50;
for(;num<480;num++)
{
TFTLCD_DrawPoint(g_cSeries[ui32Loop-1].xAxis,num,BLACK);
}
TFTLCD_DrawPoint(g_cSeries[ui32Loop-1].xAxis,g_cSeries[ui32Loop-1].data,0);
TFTLCD_DrawPoint(g_cSeries[ui32Loop-1].xAxis,g_cSeries[ui32Loop-1].data+50,0);
g_cSeries[ui32Loop-1].data = g_cSeries[ui32Loop].data;
}
/*----------------------------------------
*
*
* 120到180管道
* -------------------------------------
*/
if(ui32Loop==181)
{
t1=rand()%300+50;
ui32Loop=ui32Loop+60;
b1=1;
t2=g_cSeries[185].data;
g_cSeries[p1].data=t2;
start++;
fenshu[0]=((start/2)-1)%10;
fenshu[1]=(((start/2)-1)%100-fenshu[0])/10;
fenshu[2]=(((start/2)-1)%1000-fenshu[1]-fenshu[0])/100;
fenshu[3]=((start/2)-1)/1000;
showvalue(fenshu[0]);
I2C0TubeSelSet(~0x08);
SysCtlDelay(20000);
}
if(c==1)
{
g_cSeries[240].data=t1;
}
else
{
g_cSeries[240].data=1;
}
if(b1==1)
{
if(g_cSeries[p1].data>20)
{
g_cSeries[p1-1].data = g_cSeries[p1].data;
TFTLCD_DrawPoint(g_cSeries[p1-1].xAxis,g_cSeries[p1-1].data,GREEN);
TFTLCD_DrawPoint(g_cSeries[p1-1].xAxis,g_cSeries[p1-1].data+50,GREEN);
num=g_cSeries[p1-1].data;
for(;num>0;num--)
{
TFTLCD_DrawPoint(g_cSeries[p1-1].xAxis,num,GREEN);
}
num=g_cSeries[p1-1].data+50;
for(;num<480;num++)
{
TFTLCD_DrawPoint(g_cSeries[p1-1].xAxis,num,GREEN);
}
}
else
{
num=g_cSeries[p1-1].data;
for(;num>0;num--)
{
TFTLCD_DrawPoint(g_cSeries[p1-1].xAxis,num,BLACK);
}
num=g_cSeries[p1-1].data+50;
for(;num<480;num++)
{
TFTLCD_DrawPoint(g_cSeries[p1-1].xAxis,num,BLACK);
}
TFTLCD_DrawPoint(g_cSeries[p1-1].xAxis,g_cSeries[p1-1].data,0);
TFTLCD_DrawPoint(g_cSeries[p1-1].xAxis,g_cSeries[p1-1].data+50,0);
g_cSeries[p1-1].data = g_cSeries[p1].data;
}
p1--;
}
/*----------------------
*
*
*
* 60到120管道
* ------------------------------
*/
if(p1==120)
{
t2=g_cSeries[125].data;
b2=1;
g_cSeries[p2].data=t2;
p1=p1+60;
}
if(b2==1)
{
if(g_cSeries[p2].data>20)
{
g_cSeries[p2-1].data = g_cSeries[p2].data;
TFTLCD_DrawPoint(g_cSeries[p2-1].xAxis,g_cSeries[p2-1].data,GREEN);
TFTLCD_DrawPoint(g_cSeries[p2-1].xAxis,g_cSeries[p2-1].data+50,GREEN);
num=g_cSeries[p2-1].data;
for(;num>0;num--)
{
TFTLCD_DrawPoint(g_cSeries[p2-1].xAxis,num,GREEN);
}
num=g_cSeries[p2-1].data+50;
for(;num<480;num++)
{
TFTLCD_DrawPoint(g_cSeries[p2-1].xAxis,num,GREEN);
}
}
else
{
num=g_cSeries[p2-1].data;
for(;num>0;num--)
{
TFTLCD_DrawPoint(g_cSeries[p2-1].xAxis,num,BLACK);
}
num=g_cSeries[p2-1].data+50;
for(;num<480;num++)
{
TFTLCD_DrawPoint(g_cSeries[p2-1].xAxis,num,BLACK);
}
TFTLCD_DrawPoint(g_cSeries[p2-1].xAxis,g_cSeries[p2-1].data,0);
TFTLCD_DrawPoint(g_cSeries[p2-1].xAxis,g_cSeries[p2-1].data+50,0);
g_cSeries[p2-1].data = g_cSeries[p2].data;
}
p2--;
}
/*---------------------------
*
*
* 0到60管道
* ---------------------------
*/
if(p2==60)
{
t3=g_cSeries[115].data;
b3=1;
g_cSeries[p3].data=t3;
p2=p2+60;
}
if(b3==1)
{
if(g_cSeries[p3].data>20)
{
g_cSeries[p3-1].data = g_cSeries[p3].data;
TFTLCD_DrawPoint(g_cSeries[p3-1].xAxis,g_cSeries[p3-1].data,GREEN);
TFTLCD_DrawPoint(g_cSeries[p3-1].xAxis,g_cSeries[p3-1].data+50,GREEN);
num=g_cSeries[p3-1].data;
for(;num>0;num--)
{
TFTLCD_DrawPoint(g_cSeries[p3-1].xAxis,num,GREEN);
}
num=g_cSeries[p3-1].data+50;
for(;num<480;num++)
{
TFTLCD_DrawPoint(g_cSeries[p3-1].xAxis,num,GREEN);
}
}
else
{
num=g_cSeries[p3-1].data;
for(;num>0;num--)
{
TFTLCD_DrawPoint(g_cSeries[p3-1].xAxis,num,BLACK);
}
num=g_cSeries[p3-1].data+50;
for(;num<480;num++)
{
TFTLCD_DrawPoint(g_cSeries[p3-1].xAxis,num,BLACK);
}
TFTLCD_DrawPoint(g_cSeries[p3-1].xAxis,g_cSeries[p3-1].data,0);
TFTLCD_DrawPoint(g_cSeries[p3-1].xAxis,g_cSeries[p3-1].data+50,0);
g_cSeries[p3-1].data = g_cSeries[p3].data;
}
p3--;
}
if(p3==0)
{
p3=60;
}
// We need to update sine value per Systick period.
huaniao(x,y);
/*--------------------------
*
*
*
* 画出小鸟并让小鸟向下掉落
* ----------------------
*
*/
i=i+1;
if(i>3)
{
qingniao(x,y);
y=y+7;
i=0;
}
//UARTprintf("y=%d,data=%d\n",y,g_cSeries[x].data);
/*`````````````````````````````
*
*
*
*死亡预设当小鸟碰到管道或在碰到屏幕边界
*
* `````````````````````````````````
*/
if((y<g_cSeries[x].data)||(y>g_cSeries[x].data+40)&&(g_cSeries[x].data>20))
{
UARTprintf("gameover");
end=1;
}
if(y<0||y>481)
{
UARTprintf("gameover");
end=1;
}
/*-----------------------------
*
*
* 出现gameover
*
* -----------------------------
*/
if(end==1)
{
TFTLCD_CLEAR(BLACK);
TFTLCD_ShowString(60,180,"game over",WHITE,BLACK);
TFTLCD_ShowString(60,200,"ahahahahahhaha",WHITE,BLACK);
TFTLCD_DrawHorizontalLine(60,180,240,WHITE);
TFTLCD_DrawHorizontalLine(60,180,320,WHITE);
TFTLCD_DrawVerticalLine(240,320,60,WHITE);
TFTLCD_DrawVerticalLine(240,320,180,WHITE);
TFTLCD_ShowString(80,260,"return",WHITE,BLACK);
int lemon=0;
/*-------------------------
*
*
* 判断是否触摸到gameover
*
*
* ------------------------
*/
while(end==1)
{
if((TouchXData[10]<=240)&&(TouchYData[10]<=400))
{
if(TouchXData[10]<=180&&TouchXData[10]>=60&&TouchYData[10]>240&&TouchYData[10]<320)
end=0;
}
for(lemon=0;lemon<=9;lemon++)
{
SSIDataPut(SSI0_BASE,0x90);
SysCtlDelay(3);
SSIDataGet(SSI0_BASE,&TouchXData[lemon]);
SysCtlDelay(3);
SSIDataPut(SSI0_BASE,0xd0);
SysCtlDelay(3);
SSIDataGet(SSI0_BASE,&TouchYData[lemon]);
SysCtlDelay(3);
}
TouchXData[10] = (TouchXData[0]+TouchXData[1]+TouchXData[2]+TouchXData[3]+TouchXData[4]+TouchXData[5]+TouchXData[6]+TouchXData[7]+TouchXData[8]+TouchXData[9])/10;
TouchYData[10] = (TouchYData[0]+TouchYData[1]+TouchYData[2]+TouchYData[3]+TouchYData[4]+TouchYData[5]+TouchYData[6]+TouchYData[7]+TouchYData[8]+TouchYData[9])/10;
TOUCH_PointAdjust(&TouchXData[10], &TouchYData[10]);
}
if (start>high)
high=start;
start=0;
/*------------------------
* 复位出现开始
*
*
*
* ------------------------
*/
I2C0TubeLowSet(0x00);
I2C0TubeHighSet(0x00);
I2C0TubeSelSet(~0x08);
int u;
TFTLCD_CLEAR(BLACK);
for( u=0;u<240;u++)
{
g_cSeries[u].data=0;
}
for(u=0;u<11;u++)
{
TouchXData[u]=0;
TouchYData[u]=0;
}
x=120;
y=240;
//showvalue(high);
//I2C0TubeSelSet(~0x08);
}
}
}
}
}
实现效果如下图
希望看到的能在基础上进行改进
时间: 2024-10-30 15:35:20