|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
/*
随机数种子是rtc时钟
*/
#include "time.h"
#include "plant.h"
#include "stdlib.h"
#include "lcd.h"
#include "tft_1_44.h"
#include "rtc.h"
int Souce=0;
TimeVale TimeValeStructure;
IntervalSet IntervalSetStruct;
p_Coor p_Bullet = NULL;//子弹的结构体指针
p_Coor p_Enemy = NULL; //敌机
p_Coor p_Mine = NULL; //自己飞机
IconSizeTypedef Mp1_Size;
IconSizeTypedef Ep1_Size;
IconSizeTypedef Bl1_Size;
/**
* @brief 初始化链表
* @note
* @param
* @retval
*/
void LinkedListInit(void)
{
/*子弹的初始位置*/
p_Bullet = (p_Coor)malloc(sizeof(Coor));
p_Bullet->next = NULL;
p_Bullet->x = p_Mine->x+Mp1_Size.x/2-(Bl1_Size.x/2);
p_Bullet->y = p_Mine->y-Bl1_Size.y;
p_Bullet->Last_x = p_Bullet->x;
p_Bullet->Last_y = p_Bullet->y;
/*自己的初始位置*/
p_Mine = (p_Coor)malloc(sizeof(Coor));
p_Mine->next = NULL;
p_Mine->x = XNUM/2;//-Mp1_Size.x/2
p_Mine->y = YNUM - Mp1_Size.y;
p_Mine->Last_x = p_Mine->x;
p_Mine->Last_x = p_Mine->y;
/*敌机开辟空间*/
p_Enemy = (p_Coor)malloc(sizeof(Coor));
p_Enemy->next = NULL;
}
/**
* @brief 增加节点(头插)
* @note 1、开辟空间
2、填充数据
3、连接链表
头插法
* @param NodeNum:0添加子弹,1添加敌机,2添加炮弹
* @retval
*/
void AddNode(ICON_TYPE NodeNum)
{
p_Coor p_new;
p_new = (p_Coor)malloc(sizeof(Coor));
if(NodeNum == BULLET1)
{
p_new->x = p_Mine->x+Mp1_Size.x/2-(Bl1_Size.x/2);
p_new->y = p_Mine->y-Bl1_Size.y; //这里最好减飞机的长度
if(BULLET1 !=NULL)
{
p_new->next = p_Bullet;
p_Bullet = p_new;
}
else
{
p_Bullet = p_new;
p_Bullet->next = NULL;
}
}
else if(NodeNum == ENEMY_PLANT)
{
p_new->x = rand()%XNUM-Ep1_Size.x;
p_new->y = 0;
if(ENEMY_PLANT !=NULL)
{
p_new->next = p_Enemy;
p_Enemy = p_new;
}
else
{
p_Enemy = p_new;
p_Enemy->next = NULL;
}
}
}
/**
* @brief 清除指定图标
* @note
* @param
* @retval
*/
void DelIcon(p_Coor picon,IconSizeTypedef psize)
{
LCD_Full(picon->x, picon->y, picon->x+psize.x+1 ,picon->y+psize.y+3,0xffff);
}
/**
* @brief 碰撞检测
* @note
* @param
* @retval
*/
void ShootCheak(void)
{
p_Coor Ep_last = p_Enemy;
p_Coor Mp_last = p_Bullet;
p_Coor Ene1_pbuf = p_Enemy;
p_Coor Bul1_pbuf = p_Bullet;
while(Ene1_pbuf != NULL)
{
while(Bul1_pbuf != NULL)
{
if(( Bul1_pbuf->x >= Ene1_pbuf->x - Bl1_Size.x && Bul1_pbuf->x <= Ene1_pbuf->x + Ep1_Size.x)\
&&( Bul1_pbuf->y >= Ene1_pbuf->y - Bl1_Size.y && Bul1_pbuf->y <= Ene1_pbuf->y + Ep1_Size.y))
{
/*说明碰上了*/
DelIcon(Bul1_pbuf,Bl1_Size);
DelIcon(Ene1_pbuf,Ep1_Size);
/*删掉敌机*/
if(Ene1_pbuf != p_Enemy)
{
/*连接*/
Ep_last->next = Ene1_pbuf->next;
/*删除*/
free(Ene1_pbuf);
}
else if(Ene1_pbuf == p_Enemy)
{
/*连接*/
p_Enemy = Ene1_pbuf->next;
/*删除*/
free(Ene1_pbuf);
}
/*删掉子弹*/
if(Bul1_pbuf != p_Bullet)
{
/*连接*/
Mp_last->next = Bul1_pbuf->next;
/*删除*/
free(Bul1_pbuf);
}
else
{
/*连接*/
p_Bullet = Bul1_pbuf->next;
/*删除*/
free(Bul1_pbuf);
}
Souce++;
}
Mp_last = Bul1_pbuf;
Bul1_pbuf = Bul1_pbuf->next;
}
Ep_last = Ene1_pbuf;
Ene1_pbuf = Ene1_pbuf->next;
}
}
/**
* @brief 画游戏
* @note
* @param
* @retval
*/
void DrawGame(void)
{
extern const unsigned char gImage_em_plant1_20x15[];
extern const unsigned char gImage_my_plant2_16x20[];
extern const unsigned char gImage_my_plant1_16x20[];
extern const unsigned char gImage_bullet1_4x10[];
extern const unsigned char gImage_bullet2_4x10[];
p_Coor pbuf;
pbuf = p_Bullet;
while(pbuf!=NULL)
{
/*子弹1*/
LcdDrawPic(gImage_bullet2_4x10,pbuf->x,pbuf->y,4,11);
pbuf = pbuf->next;
}
pbuf = p_Mine;
while(pbuf!=NULL)
{
/*清除上一次的飞机*/
if(pbuf->Last_x != pbuf->x || pbuf->Last_y != pbuf->y)
{
LCD_Full(pbuf->Last_x,pbuf->Last_y,pbuf->Last_x+Mp1_Size.x ,pbuf->Last_y+Mp1_Size.y,0xffff);
}
/*我的飞机*/
LcdDrawPic(gImage_my_plant2_16x20,pbuf->x,pbuf->y,Mp1_Size.x,Mp1_Size.y);
pbuf = pbuf->next;
}
pbuf = p_Enemy;
while(pbuf!=NULL)
{
/*敌机*/
LcdDrawPic(gImage_em_plant1_20x15,pbuf->x,pbuf->y,20,15);
pbuf = pbuf->next;
}
}
/**
* @brief 移动图像
* @note
* @param
* @retval
*/
void MoveGame(KEY_NUM Keynum)
{
p_Coor pbuf;
pbuf = p_Bullet;
p_Mine->Last_x = p_Mine->x;
p_Mine->Last_y = p_Mine->y;
while(pbuf!=NULL)
{
pbuf->y-=1;
pbuf = pbuf->next;
}
pbuf = p_Enemy;
while(pbuf!=NULL)
{
pbuf->y+=1;
pbuf = pbuf->next;
}
if(Keynum == KEY4 ||Keynum == KEY4L)
{
p_Mine->x+=3;
}
else if(Keynum == KEY2 || Keynum == KEY2L)
{
p_Mine->x-=3;
}
else if(Keynum == KEY5 || Keynum == KEY5L)
{
p_Mine->y -=3;
}
else if(Keynum == KEY3 || Keynum == KEY3L)
{
p_Mine->y +=3;
}
if(p_Mine->x > XNUM-Mp1_Size.x || p_Mine->x <= 0)
{
p_Mine->x = p_Mine->Last_x;
}
else if(p_Mine->y <= 0 || p_Mine->y > YNUM-Mp1_Size.y)
{
p_Mine->y = p_Mine->Last_y;
}
}
/**
* @brief 添加图像
* @note
* @param
* @retval
*/
void AddIcon(void)
{
static int i0,i1; //会自动赋值为0
i0++;
i1++;
if(i0>IntervalSetStruct.Tim_MineBullet)
{
AddNode(BULLET1);
i0=0;
}
if(i1>IntervalSetStruct.Tim_Enemy)
{
AddNode(ENEMY_PLANT);
i1=0;
}
}
/**
* @brief 回收图标
* @note
* @param
* @retval
*/
void ReclaimIcon(void)
{
p_Coor pbuf,p0;
pbuf = p_Bullet;
p0 = pbuf;
while(pbuf!=NULL)
{
if(pbuf->y > YNUM - Bl1_Size.y || pbuf->x > XNUM - Bl1_Size.x || pbuf->x <= 0 || pbuf->y <= 0)
{
DelIcon(pbuf ,Bl1_Size);
/*连接*/
if(pbuf != p_Bullet)
{
p0->next = pbuf->next;
}
else
{
p_Bullet = p_Bullet->next;
}
/*释放本节*/
free( pbuf );
pbuf = p0;
}
p0 = pbuf; //上一次的p
pbuf = pbuf->next;
}
pbuf = p_Enemy;
p0 = pbuf;
while(pbuf!=NULL)
{
if(pbuf->y > YNUM-Ep1_Size.y || pbuf->x > XNUM - Ep1_Size.x || pbuf->x < 0 || pbuf->y < 0)
{
DelIcon(pbuf ,Ep1_Size);
/*连接*/
if(pbuf != p_Enemy)
{
p0->next = pbuf->next;
}
else
{
p_Enemy = p_Enemy->next;
}
/*释放本节*/
free( pbuf );
pbuf = p0;
}
p0 = pbuf; //上一次的p
pbuf = pbuf->next;
}
}
/**
* @brief 检测结束
* @note
* @param
* @retval 结束返回1
*/
uint8_t CheakOver(void)
{
extern const unsigned char gImage_GameOver_128x128[];
p_Coor pbuf = p_Enemy;
char GameOverFlag = 0;
while(pbuf!=NULL)
{
if(pbuf->y > YNUM-Ep1_Size.y-1 )
{
LcdDrawPic(gImage_GameOver_128x128,0,0,128,128);
GameOverFlag = 1;
}
pbuf = pbuf->next;
}
return GameOverFlag;
} |
|