So, I am trying to make Space Invader(whatever) with Arduinos.
I am using a 8x8 Matrix display, passive buzzer and a joystick.
But I have a problem.
That is, that I have a variable pos
.
This variable is the players x
position.
When I am shooting a bullet, the pos
variable magically is set to 0.
Why do I not understand it?
Because no where in my code, is there anything that sets pos
to 0 or similar.
For some reason, if I remove these lines of code:
if (bulletYs[i] == 0 && bulletXs[i] != 0) {
Serial.println("BAD BULLET BOY!");
bulletYs[i] = 0;
bulletXs[i] = 0;
}
It magically works?
Here is my full code:
#include <LedControl.h>
#define SpeakerPin 48
//defining global variables
int pos = 3;
int bulletXs[100] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
int bulletYs[100] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int index = 0;
int amountOfBullets = 0;
LedControl lc1 = LedControl(12, 10, 11, 1);
//telling C++ that we are going to make these functions
void drawPlayer(int midX, int midY);
void ComputeInput();
void handleBullets();
void drawBullets();
void setup() {
//wake up the MAX72XX from power-saving mode
lc1.shutdown(0, false);
//set a medium brightness for the Leds
lc1.setIntensity(0, 8);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(2, INPUT_PULLUP);
pinMode(SpeakerPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
lc1.clearDisplay(0);
//lc1.setColumn(0,0,255);
Serial.println(pos);
ComputeInput();
handleBullets();
drawBullets();
drawPlayer(pos, 7);
Serial.println("Drawing the player");
delay(200);
}
void drawPlayer(int midX, int midY) {
lc1.setLed(0, midX + 1, midY, true);
lc1.setLed(0, midX, midY, true);
lc1.setLed(0, midX - 1, midY, true);
lc1.setLed(0, midX, midY - 1, true);
}
void ComputeInput() {
if (analogRead(A2) >= 300) {
pos++;
Serial.println("Changing pos because the player is moving right");
tone(8, 400, 200);
}
else if (analogRead(A2) == 0) {
pos--;
Serial.println("Changing pos because the player is moving left");
tone(8, 200, 200);
}
if (digitalRead(2) == 0) {
Serial.println("Creating A bullet!");
bulletXs[index] = pos;
bulletYs[index] = 6;
index++;
amountOfBullets++;
int startTime = millis();
int startPitch = 1000;
int tempPos = pos;
Serial.print("Creating a tempPos:");
Serial.println(tempPos);
while (millis() - startTime <= 100) {
drawPlayer(pos, 7);
drawBullets();
tone(8, startPitch, 10);
startPitch = startPitch - 100;
delay(10);
}
pos = tempPos;
Serial.println("Setting Pos to tempPos");
}
}
void handleBullets() {
if (amountOfBullets == 10) {
for (int i = 0; i <= 100; i++) {
bulletXs[i] = -1;
bulletYs[i] = 0;
}
}
for (int i = 0; i <= 100; i++) {
if (bulletYs[i] != 0) {
bulletYs[i]--;
tone(8, 1000, 50);
Serial.println(bulletYs[i]);
if (bulletYs[i] == 0 && bulletXs[i] != 0) {
Serial.println("BAD BULLET BOY!");
bulletYs[i] = 0;
bulletXs[i] = 0;
}
}
}
}
void drawBullets() {
for (int i = 0; i <= 100; i++) {
if (bulletXs[i] != -1) {
lc1.setLed(0, bulletXs[i], bulletYs[i], true);
}
}
}
(Sorry for the fact that it looks terrible, I didn't bother commenting that much)