Arduino LCD太空入侵者
这是一个需要帮助的arduino代码。我的代码中有一个我无法理解的错误。我需要帮助把外星人排成一排 这是问题代码---Arduino LCD太空入侵者,arduino,arduino-ide,Arduino,Arduino Ide,这是一个需要帮助的arduino代码。我的代码中有一个我无法理解的错误。我需要帮助把外星人排成一排 这是问题代码--- #定义外星生物计数24 int yAliens[ALIEN_COUNT]={0,0,0,0,0,0,0,0,0,0,0,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}; //... 用于(字节a=0;a=25){ a=0; }
#定义外星生物计数24
int yAliens[ALIEN_COUNT]={0,0,0,0,0,0,0,0,0,0,0,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};
//...
用于(字节a=0;a=25){
a=0;
}
}
如果(希特勒){
fire=false;//子弹停止移动
lcd.setCursor(xb,yb);//外星人的位置
lcd.print(“”;//变为空白
lcd.setCursor(xb+1,yb);
lcd.打印(“”);
}
这是完整的代码---
#包括
液晶显示器(12,11,5,4,3,2);
字节Ship[8]={//播放器Ship的代码
B00000,
B00000,
B00001,
B00111,
B11001,
B00111,
B00001,
B00000
};
字节Ailen[8]={//外星人的代码
B10001,
B10001,
B01010,
B01110,
B01110,
B01010,
B10001,
B10001
};
字节项目符号[8]={//项目符号的代码
B00000,
B00000,
B11111,
B11111,
B11011,
B11111,
B11111,
B00000
};
整数时间=0//添加以使子弹停止
int lButton=13//左边按钮的别针
int rButton=6//右边按钮的别针
int fButton=7//点火按钮的别针
int lState=0//左按钮的状态
int rState=0//右键的状态
int fState=0//点火按钮的状态
int xb=0//子弹x坐标
int-yb=0//子弹在空中飞来飞去
int-xa=0//外星人x坐标
int-ya=0//外星人
int xShip=15//船的起始x坐标
int-yShip=0//启动船舶的y坐标
int-xAilens[2]={0,1};
int yAilens[12]={0,1,2,3,4,5,6,7,8,9,10,11};/*试图使用数组
这不起作用,但我会把它留在*/
布尔值ailen=true//爱伦还活着吗?
布尔火=假//子弹开了吗
常数int dTime=150//移动的延迟是多少
无效设置(){
pinMode(lButton,INPUT);//设置左按钮
pinMode(rButton,INPUT);//设置右侧按钮
pinMode(fButton,INPUT);//设置点火按钮
lcd.begin(16,2);//lcd的参数
lcd.createChar(0,Ship);//设置字符
lcd.createChar(1,Ailen);//设置字符
lcd.createChar(2,Bullet);//设置字符
setCursor(xa,ya);//一个外星人的坐标
lcd.write((字节)1);//打印一个外星人
}
void循环(){
lState=digitalRead(lButton);//提取左按钮的状态
rState=digitalRead(rButton);//提取右按钮的状态
fState=digitalRead(fButton);//触发按钮的状态
xb=xShip;
如果(fire==true&&yb==ya&&xb==xa&&ailen==true){/*如果子弹在移动
外星人的位置和子弹一样,那么*/
fire=false;//子弹停止移动
ailen=false;//外星人死了
lcd.setCursor(xa,ya);//外星人的位置
lcd.print(“”;//变为空白
音调(9400100);//消除噪音
}
如果(fire==true&&time=17){//如果项目符号仍在屏幕外移动,则
fire=false;//停止移动子弹
time=0;//重置下次发射子弹的时间
}
如果(fState==HIGH&&fire==false){//如果按下了fire按钮
fire=true;//开始移动子弹
yb=yShip;//使子弹移动到船不在的地方,而一直在的地方
}
如果(rState==HIGH&&lState==LOW){//如果按下了右键而没有按下左键
yShip=0;//使船向右移动如果已经向右移动,则不会发生任何事情
lcd.setCursor(xShip,yShip);//船舶位置
lcd.write((字节)0);//显示ship字节
lcd.setCursor(xShip,yShip+1);//清除飞船所在的位置
lcd.print(“”;//清除块
延迟(dTime);//阻止糖化的时间
}
else如果(lState==HIGH&&rState==LOW){//如果按下了右键,但没有按下右键
yShip=1;//使飞船在屏幕左侧,如果在左侧,则什么也不会发生
lcd.setCursor(xShip,yShip);//船的位置坐标
lcd.write((字节)0);//显示船舶
lcd.setCursor(xShip,yShip-1);//清除船只所在的位置
lcd.print(“”;//清除块
延迟(dTime);//阻止糖化的时间
}
否则{//如果未按任何键
lcd.setCursor(xShip,yShip);//为船舶设置以前的坐标*良好实践
lcd.write((字节)0);//显示船舶
}
}
我已尝试简化我们的代码并解决您的问题
我不能测试它,所以我不能确定一切都好
出于内存占用的考虑,我使用byte
而不是int
我删除了时间
,直接处理子弹位置
为了便于理解,我删除了旧位置,更新了位置,并在新位置打印了对象
#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
}
}
#包括
液晶显示器(12,11,5,4,3,2);
字节Ship[8]={//播放器Ship的代码
B00000,
B00000,
B00001,
B00111,
B11001,
B00111,
B00001,
B00000
};
字节Ailen[8]={//ailens的代码
B10001,
B10001,
B01010,
B01110,
B01110,
B01010,
B10001,
B10001
};
字节项目符号[8]={//项目符号的代码
B00000,
B00000,
B11111,
B11111,
B11011,
B11111,
B11111,
B00000
#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
}
}
#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
}
}