📄 clock.c
字号:
// 说明: 用LPC2131实现射击游戏
// 编写: 王晓荣
// 日期: 20080518
#include <LPC213X.H>
const unsigned char LEDMAP[] =
{
0x3f, 0x06, 0x5b, 0x4f, 0x66,
0x6d, 0x7d, 0x07, 0x7f, 0x6f,0x00
};
#define RAND_LENGTH 17
const unsigned char randEnemy[] = {0,1,0,0,1,0,0,1,0,1,1,0,1,1,0,0,0 }; // 随机产生新的敌人
unsigned char gunPositionMap[] = {0x01,0x20,0x40,0x10,0x08};
unsigned char LEDBuf[6];
unsigned int timerLoop;
unsigned int enemyPosition[5];
unsigned int enemyTemp[5];
unsigned char gunPosition;
unsigned char refreshFlg;
unsigned int score;
//////////////////////////////////////////////////////////////////////
initial();
enemyGoAhead();
shoot();
delay1ms();
display();
gameOver();
refresh();
///////////////////////////////////////////////////
//枪位置切换按键
void __irq IRQ_gunPosition() //需加IRQ标识号
{
gunPosition ++;
if(5 == gunPosition)
gunPosition = 0;
LEDBuf[5] = gunPositionMap[gunPosition];
EXTINT = 0X01; //清中断标志
VICVectAddr = 0x00; //ppt//清中断地址
}
//射击按键
void __irq IRQ_shoot() //需加IRQ标识号
{
switch(gunPosition)
{
case 0:
score ++;
if(enemyPosition[0] >= 0x10) enemyPosition[0] -= 0x10;
else if(enemyPosition[0] >= 0x08) enemyPosition[0] -= 0x08;
else if(enemyPosition[0] >= 0x04) enemyPosition[0] -= 0x04;
else if(enemyPosition[0] >= 0x02) enemyPosition[0] -= 0x02;
else if(enemyPosition[0] >= 0x01) enemyPosition[0] -= 0x01;
else score --;
break;
case 1:
score ++;
if(enemyPosition[1] >= 0x400) enemyPosition[1] -= 0x400;
else if(enemyPosition[1] >= 0x200) enemyPosition[1] -= 0x200;
else if(enemyPosition[1] >= 0x100) enemyPosition[1] -= 0x100;
else if(enemyPosition[1] >= 0x80) enemyPosition[1] -= 0x80;
else if(enemyPosition[1] >= 0x40) enemyPosition[1] -= 0x40;
else if(enemyPosition[1] >= 0x20) enemyPosition[1] -= 0x20;
else if(enemyPosition[1] >= 0x10) enemyPosition[1] -= 0x10;
else if(enemyPosition[1] >= 0x08) enemyPosition[1] -= 0x08;
else if(enemyPosition[1] >= 0x04) enemyPosition[1] -= 0x04;
else if(enemyPosition[1] >= 0x02) enemyPosition[1] -= 0x02;
else if(enemyPosition[1] >= 0x01) enemyPosition[1] -= 0x01;
else score --;
break;
case 2:
score ++;
if(enemyPosition[2] >= 0x10) enemyPosition[2] -= 0x10;
else if(enemyPosition[2] >= 0x08) enemyPosition[2] -= 0x08;
else if(enemyPosition[2] >= 0x04) enemyPosition[2] -= 0x04;
else if(enemyPosition[2] >= 0x02) enemyPosition[2] -= 0x02;
else if(enemyPosition[2] >= 0x01) enemyPosition[2] -= 0x01;
else score --;
break;
case 3:
score ++;
if(enemyPosition[3] >= 0x400) enemyPosition[3] -= 0x400;
else if(enemyPosition[3] >= 0x200) enemyPosition[3] -= 0x200;
else if(enemyPosition[3] >= 0x100) enemyPosition[3] -= 0x100;
else if(enemyPosition[3] >= 0x80) enemyPosition[3] -= 0x80;
else if(enemyPosition[3] >= 0x40) enemyPosition[3] -= 0x40;
else if(enemyPosition[3] >= 0x20) enemyPosition[3] -= 0x20;
else if(enemyPosition[3] >= 0x10) enemyPosition[3] -= 0x10;
else if(enemyPosition[3] >= 0x08) enemyPosition[3] -= 0x08;
else if(enemyPosition[3] >= 0x04) enemyPosition[3] -= 0x04;
else if(enemyPosition[3] >= 0x02) enemyPosition[3] -= 0x02;
else if(enemyPosition[3] >= 0x01) enemyPosition[3] -= 0x01;
else score --;
break;
case 4:
score ++;
if(enemyPosition[4] >= 0x10) enemyPosition[4] -= 0x10;
else if(enemyPosition[4] >= 0x08) enemyPosition[4] -= 0x08;
else if(enemyPosition[4] >= 0x04) enemyPosition[4] -= 0x04;
else if(enemyPosition[4] >= 0x02) enemyPosition[4] -= 0x02;
else if(enemyPosition[4] >= 0x01) enemyPosition[4] -= 0x01;
else score --;
break;
default:
break;
}
EXTINT = 0X02; //清中断标志
VICVectAddr = 0x00; //ppt//清中断地址
}
//敌人前进时间定时;刷屏定时
void __irq IRQ_TimerAhead()
{
timerLoop++;
if(10 == timerLoop)
{
timerLoop = 0;
enemyGoAhead();
}
refresh();
T0IR = 0x01; //清中断标志
VICVectAddr = 0x00;
}
///////////////////////////////////////////////////////////////////////////////
main()
{
initial();
while(1)
{
if( enemyPosition[0] >= 0x20 || //当敌人冲上最后一个显示器时
enemyPosition[1] >= 0x0400 ||
enemyPosition[2] >= 0x20 ||
enemyPosition[3] >= 0x0400 ||
enemyPosition[4] >= 0x20 )
{
gameOver();
}
display();
}
}
/////////////////////////////////////////////////////////////////////
initial()
{
//将引脚P0_14设为外部中断1输入引脚
//将引脚P0_16设为外部中断0输入引脚
PINSEL0 = 0x20000000;
PINSEL1 = 0x01;
IO0DIR |= 0X00003F7F;
//外部中断0和1均使用边沿触发
EXTMODE = 0X03;
T0TC = 0; //定时器0赋初值
T0PR = 0; //TC每经过PR + 1 个pclk周期加1
T0MCR = 0X03; //MR0与TC匹配时,产生中断,TC清0
T0MR0 = 500000; //1S定时
T0TCR = 0x01; //启动定时器
VICIntSelect = 0x00; // 所有中断都不采用FIQ
//将14#中断设为向量IRQ,并绑定通道0(最高优先级)
VICVectCntl0 = 0x20 | 14;
VICVectAddr0 = (unsigned int) IRQ_gunPosition;
//将15#中断设为向量IRQ,并绑定通道1
VICVectCntl1 = 0x20 | 15;
VICVectAddr1 = (unsigned int) IRQ_shoot;
//将 4#中断设为向量IRQ,并绑定通道2
VICVectCntl2 = 0x20 | 4;
VICVectAddr2 = (unsigned int) IRQ_TimerAhead;
VICIntEnable = 0xc010; //使能4、14、15
}
//////////////////////////////////////////////
delay1ms()
{
unsigned int i;
for(i = 5000; i > 0; i--);
}
display()
{
unsigned char i;
unsigned long outbit;
outbit = 0x01; // 从右边开始显示
for (i = 0; i < 6; i++)
{
// 关所有LED
IO0SET = 0x3f << 8;
// 送数据
IO0SET = LEDBuf[i];
IO0CLR = ~LEDBuf[i] & 0XFF;
IO0CLR = outbit << 8;
delay1ms();
outbit <<= 1; // 显示下一位
}
}
////////////////////////////////////////////////////////////////
//敌人前进和随机增加新的敌人
enemyGoAhead()
{
unsigned char i,r;
for(i = 0;i < 5;i ++)
{
enemyPosition[i] <<= 1;
enemyPosition[i] += randEnemy[r];
r++;
if(r == RAND_LENGTH)
r = 0;
}
}
//////////////////////////////////////////////
// 游戏结束,显示得分
gameOver()
{
VICIntEnable = 0;
LEDBuf[0] = LEDMAP[score %10];
LEDBuf[1] = LEDMAP[score /10%10];
LEDBuf[2] = LEDMAP[score /100%10];
LEDBuf[3] = LEDMAP[score /1000%10];
LEDBuf[4] = LEDMAP[10];
LEDBuf[5] = LEDMAP[10];
T0TCR = 0x0;
}
/////////////////////////////////////////////
//刷屏
refresh()
{
unsigned char i,j;
for(i = 0;i < 5;i ++)
{
enemyTemp[i] = enemyPosition[i];
}
for(i = 0;i < 5;i ++)
{
LEDBuf[i] = (enemyTemp[0] & 0x01) +
(enemyTemp[1] & 0x01)*2 +
(enemyTemp[3] & 0x01)*4 +
(enemyTemp[4] & 0x01)*8 +
(enemyTemp[3] & 0x02)*8 +
(enemyTemp[1] & 0x02)*16 +
(enemyTemp[2] & 0x01)*64;
for(j = 0;j < 5;j ++)
{
enemyTemp[j] >>= 1;
}
enemyTemp[1] >>= 1;
enemyTemp[3] >>= 1;
}
LEDBuf[5] = gunPositionMap[gunPosition];
}
///////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -