这是一个需要帮助的arduino代码。我的代码有一个我无法弄清楚的错误。我需要帮助把外星人放在一个阵列中。
这是问题代码---
#define ALIEN_COUNT 24
int yAliens[ALIEN_COUNT]={0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1};
int xAliens[ALIEN_COUNT] = {0,1,2,3,4,5,6,7,8,9,10,11,0,1,2,3,4,5,6,7,8,9,10,11};
//...
for(byte a=0; a<=24; a++){
if(fire&&xb== xAliens[a] && yb == yAliens[a]){
hitAlien = true;
break;
}
if(a>=25){
a=0;
}
}
if (hitAlien) {
fire = false; //bullet stops moving
lcd.setCursor(xb, yb); //the position of the alien
lcd.print(" "); //becomes blank
lcd.setCursor(xb+1,yb);
lcd.print(" ");
}
这是完整的代码---
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte Ship[8] = { //the code for the player ship
B00000,
B00000,
B00001,
B00111,
B11001,
B00111,
B00001,
B00000
};
byte Ailen[8] = {//the code for the aliens
B10001,
B10001,
B01010,
B01110,
B01110,
B01010,
B10001,
B10001
};
byte Bullet[8] = {//the code for the bullet
B00000,
B00000,
B11111,
B11111,
B11011,
B11111,
B11111,
B00000
};
int time = 0;//adds to make the bullet stop
int lButton = 13;//pin for the left button
int rButton = 6;//pin for the right button
int fButton = 7;//pin for the firing button
int lState = 0;//state of the left button
int rState = 0;//state of the right button
int fState = 0;//state of the firing button
int xb = 0;//the bullets x coords
int yb = 0;//the bullets y coords
int xa = 0;//the aliens x coords
int ya = 0;//the aliens y coords
int xShip=15;//the starting x coord of the ship
int yShip=0;//starting y coord of the ship
int xAilens[2] = {0,1};
int yAilens[12]= {0,1,2,3,4,5,6,7,8,9,10,11}; /* attempted use of the array
which did not work but I will leave it in*/
boolean ailen = true;//is the one ailen alive?
boolean fire = false;//is the bullet firing
const int dTime= 150;//what is the delay for movement
void setup() {
pinMode(lButton, INPUT);//sets up left button
pinMode(rButton, INPUT);//sets up right button
pinMode(fButton, INPUT);//sets up firing button
lcd.begin(16, 2);//parameter of the lcd
lcd.createChar(0, Ship);//sets char
lcd.createChar(1,Ailen);//sets char
lcd.createChar(2,Bullet);//sets char
lcd.setCursor(xa,ya);//coords of the one alien
lcd.write((byte)1);//prints the one alien
}
void loop() {
lState=digitalRead(lButton);//pulls the state of the left button
rState=digitalRead(rButton);//pulls the state of the right button
fState=digitalRead(fButton);//pulls the state of the firing button
xb = xShip;
if(fire==true&&yb==ya&&xb==xa&&ailen==true){/*if the bullet is moving
and the alien's position is the same as the bullets, then*/
fire=false;//bullet stops moving
ailen=false;//the alien is dead
lcd.setCursor(xa,ya);//the position of the alien
lcd.print(" ");//becomes blank
tone(9,400,100);//plays kill noise
}
if(fire==true&&time<17){//if the bullet is moving and is on the screen then
xb = xb-time;//the bullet is time further away than the spaceship
lcd.setCursor(xb-1,yb);//fires one away from ship not on ship
lcd.write((byte)2);//displays bullet
lcd.setCursor(xb+1,yb);//clears where bullet was
lcd.print(" ");//clear symbol
time++;//increase time
delay(dTime/2);//bullet moves twice as fast as ship
}
if(fire==true&&time>=17){//if the bullet is still moving offscreen then
fire=false;//stop moving the bullet
time=0;//reset the time for next time a bullet is fired
}
else if(fState==HIGH&&fire==false){//if the fire button is pressed
fire=true;//starts moving the bullet
yb=yShip;//makes the bullet move where the ship was not where is always is
}
if(rState==HIGH&&lState==LOW){//if the right button is pressed and the left isn't
yShip=0;//make the ship move to the right if already on right, nothing happens
lcd.setCursor(xShip,yShip);//ship's location
lcd.write((byte)0);//display the ship byte
lcd.setCursor(xShip,yShip+1);//clear where the ship was
lcd.print(" ");//clearing block
delay(dTime);//time to deter mashing
}
else if(lState==HIGH&&rState==LOW){// if the right button is pressed and the right isn't
yShip=1;//make the ship on the left of the screen, if on left, nothing happens
lcd.setCursor(xShip,yShip);//coorinates for the ship's location
lcd.write((byte)0);//displays the ship
lcd.setCursor(xShip,yShip-1);//clears where the ship was
lcd.print(" ");//clearing block
delay(dTime);//time to deter mashing
}
else{//if nothing is pressed
lcd.setCursor(xShip,yShip);//set previous coordinates for the ship*good practice
lcd.write((byte)0);//display the ship
}
}
答案 0 :(得分:2)
我试图简化代码并解决您的问题
我无法测试它,所以我不能确定一切都很好
我使用byte
代替int
来考虑内存占用
我已删除time
并直接处理子弹位置
为了便于理解,我删除旧位置,更新位置并在新位置打印对象。
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte Ship[8] = { //the code for the player ship
B00000,
B00000,
B00001,
B00111,
B11001,
B00111,
B00001,
B00000
};
byte Ailen[8] = {//the code for the ailens
B10001,
B10001,
B01010,
B01110,
B01110,
B01010,
B10001,
B10001
};
byte Bullet[8] = {//the code for the bullet
B00000,
B00000,
B11111,
B11111,
B11011,
B11111,
B11111,
B00000
};
#define lButton 13 //pin for the left button
#define rButton 6 //pin for the right button
#define fButton 7 //pin for the firing button
int xa = 0;//the aliens x coords
int ya = 0;//the aliens y coords
int xShip=15;//the starting x coord of the ship
int yShip=0;//starting y coord of the ship
int xAilens[2] = {0,1};
int yAilens[12]= {0,1,2,3,4,5,6,7,8,9,10,11}; /* attempted use of the array
which did not work but I will leave it in*/
#define DTIME 150 //what is the delay for movement
void setup() {
pinMode(lButton, INPUT);//sets up left button
pinMode(rButton, INPUT);//sets up right button
pinMode(fButton, INPUT);//sets up firing button
lcd.begin(16, 2);//parameter of the lcd
lcd.createChar(0, Ship);//sets char
lcd.createChar(1, Ailen);//sets char
lcd.createChar(2, Bullet);//sets char
lcd.setCursor(xa,ya);//coords of the one ailen
lcd.write((byte)1);//prints the one ailen
}
void loop() {
static boolean ailen = true; //is the one ailen alive?
static boolean fire = false; //is the bullet firing
static byte xb = 0; //the bullets x coords
static byte yb = 0; //the bullets y coords
byte lState = digitalRead(lButton); //pulls the state of the left button
byte rState = digitalRead(rButton); //pulls the state of the right button
byte fState = digitalRead(fButton); //pulls the state of the firing button
/*if the bullet is moving and the alien's position is
* the same as the bullets, then
* */
if (fire && ailen && yb == ya && xb == xa) {
fire = false; //bullet stops moving
ailen = false; //the alien is dead
lcd.setCursor(xa, ya); //the position of the alien
lcd.print(" "); //becomes blank
tone(9,400,100); //plays kill noise
}
if (fire) { //if the bullet is moving and is on the screen then
lcd.setCursor(xb1,yb);//clears where bullet was
lcd.print(" ");//clear symbol
if (xb-- < 0) {//move the bullet
/* bullet ouside of the screen */
fire = false;
} else {
lcd.setCursor(xb, yb);
lcd.write((byte)2); //displays bullet
delay(DTIME / 2);//bullet moves twice as fast as ship
}
}
if (fState == HIGH && !fire) { //if the fire button is pressed
fire = true; //starts moving the bullet
/* bullet starts one away from ship */
xb = xShip - 1;
yb = yShip;
}
//if the right button is pressed and the left isnt
if (rState == HIGH && lState == LOW && yShip != 0) {
lcd.setCursor(xShip, yShip); //ship's location
lcd.print(" "); //clearing block
yShip = 0; //make the ship move to the right
lcd.setCursor(xShip, yShip); //ship's location
lcd.write((byte)0); //display the ship byte
delay(DTIME); //time to deter mashing
}
// if the right button is pressed and the right isnt
else if (lState == HIGH && rState == LOW && yShip != 1) {
lcd.setCursor(xShip, yShip); //ship's location
lcd.print(" "); //clearing block
yShip = 1; //make the ship move to the right
lcd.setCursor(xShip, yShip); //ship's location
lcd.write((byte)0); //display the ship byte
delay(DTIME); //time to deter mashing
}
//if nothing is pressed
else{
lcd.setCursor(xShip, yShip);//set previous coordinates for the ship*good practice
lcd.write((byte)0);//display the ship
}
}