#include <LedControl.h>
#include <TimerOne.h>
#include "pitch.h"
#define VERT_PIN A0
#define HORZ_PIN A1
#define SEL_PIN 2
#define Speaker_PIN 6
#define device_num 3
#define module_num 3
LedControl lc[module_num] = {LedControl(12, 11, 10, device_num),
LedControl(12, 11, 9, device_num),
LedControl(12, 11, 8, device_num)
};
int row[2] = {23, 23}, col[2] = {12, 12};
int mis[22][2] = {};
int mis_num = 0;
int mon = -1;
int n_r = 0, n_c = 0;
int count1=0;
void plane_move()
{
int vert = analogRead(VERT_PIN);
int horz = analogRead(HORZ_PIN);
if (horz > 600)
col[0] = (col[1] < device_num * 8 - 2) ? col[0] + 1 : device_num * 8 - 2;
else if (horz < 400)
col[0] = (col[1] > 1) ? col[0] - 1 : 1;
if (row[1] != row[0] || col[1] != col[0])
{
if (col[0] < col[1]) //往右
lc[row[1] / 8].setLed((col[1] + 1) / 8, row[1] % 8, (col[1] + 1) % 8, 0); //左
else
lc[row[1] / 8].setLed((col[1] - 1) / 8, row[1] % 8, (col[1] - 1) % 8, 0); //右
lc[(row[1] - 1) / 8].setLed(col[1] / 8, (row[1] - 1) % 8, col[1] % 8, 0); //上
//lc[row[0] / 8].setLed(col[0] / 8, row[0] % 8, col[0] % 8, 1); //中心
lc[row[0] / 8].setLed((col[0] + 1) / 8, row[0] % 8, (col[0] + 1) % 8, 1); //左
lc[row[0] / 8].setLed((col[0] - 1) / 8, row[0] % 8, (col[0] - 1) % 8, 1); //右
lc[(row[0] - 1) / 8].setLed(col[0] / 8, (row[0] - 1) % 8, col[0] % 8, 1); //上
}
row[1] = row[0];
col[1] = col[0];
}
void monster_move()
{
if (mon == -1)
mon = random(22);
//先暗,移動,再亮
if (random(10) % 2 == 1) //往右
{
if (mon > 0)
{
lc[0].setLed((mon + 1) / 8, 0, (mon + 1) % 8, 0); //左邊暗
lc[0].setLed((mon + 1) / 8, 1, (mon + 1) % 8, 0);
mon--;
lc[0].setLed(mon / 8, 0, mon % 8, 1); //右邊亮
lc[0].setLed(mon / 8, 1, mon % 8, 1);
}
}
else //往左
{
if (mon < 22)
{
lc[0].setLed(mon / 8, 0, mon % 8, 0); //右邊暗
lc[0].setLed(mon / 8, 1, mon % 8, 0);
mon++;
lc[0].setLed((mon + 1) / 8, 0, (mon + 1) % 8, 1); //左邊亮
lc[0].setLed((mon + 1) / 8, 1, (mon + 1) % 8, 1);
}
}
}
void fire()
{
if (mis_num < 21)
mis_num++;
else
mis_num = 0;
mis[mis_num][0] = 21; //missile row
mis[mis_num][1] = col[1];
tone(Speaker_PIN, c6, 30);
//tone(Speaker_PIN,1046,30);
lc[mis[mis_num][0] / 8].setLed(mis[mis_num][1] / 8, mis[mis_num][0] % 8, mis[mis_num][1] % 8, 1);
}
//void hit(){
//if (miss[mis_num][0] == )}
void missile_move()
{
plane_move();
count1++;
//先暗, 上移, 再亮
for (int i = 0; i < 22; i++)
{
lc[mis[i][0] / 8].setLed(mis[i][1] / 8, mis[i][0] % 8, mis[i][1] % 8, 0);
if (mis[i][0] > -1)
{
mis[i][0]--;
}
lc[mis[i][0] / 8].setLed(mis[i][1] / 8, mis[i][0] % 8, mis[i][1] % 8, 1);
}
}
void setup() {
// put your setup code here, to run once:
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
lc[i].shutdown(j, false); // 關閉省電模式
lc[i].setIntensity(j, 0); // 設定亮度為 0 (介於0~15之間)
lc[i].clearDisplay(j); // 清除螢幕
}
for (int i = 0; i < 22; i++)
{
mis[i][0] = -1;
}
pinMode(VERT_PIN, INPUT);
pinMode(HORZ_PIN, INPUT);
pinMode(SEL_PIN, INPUT_PULLUP);
pinMode(6, OUTPUT);
attachInterrupt(digitalPinToInterrupt(SEL_PIN), fire, FALLING);
Timer1.initialize(50000);
Timer1.attachInterrupt(missile_move);
Serial.begin(9600);
//顯示Plane
lc[row[0] / 8].setLed(col[0] / 8, row[0] % 8, col[0] % 8, 1); //中心
lc[row[0] / 8].setLed((col[0] + 1) / 8, row[0] % 8, (col[0] + 1) % 8, 1); //左
lc[row[0] / 8].setLed((col[0] - 1) / 8, row[0] % 8, (col[0] - 1) % 8, 1); //右
lc[(row[0] - 1) / 8].setLed(col[0] / 8, (row[0] - 1) % 8, col[0] % 8, 1); //上
//顯示Monster
mon = random(22);
lc[0].setLed(mon / 8, 0, mon % 8, 1); //右邊
lc[0].setLed(mon / 8, 1, mon % 8, 1);
lc[0].setLed((mon + 1) / 8, 0, (mon + 1) % 8, 1); //左邊
lc[0].setLed((mon + 1) / 8, 1, (mon + 1) % 8, 1);
}
void loop() {
if (count1>=10)
{
monster_move();
count1=0;
}
}