Feel like I'm hitting a lot of bumps in the road today for whatever reason. Any way I have created a class to represent the monsters in a game i am making with my adurino uno and a LCD. The class and its functions work perfectly in the setup phase however even though they were previously defined I cannot use it in the loop stage. And if i initialize them in the loop stage it just keeps remaking the class and the code that changes the variables such as the monster's position and health never get to run.
Here it is sorry for the mess.
const int TxPin = 2;
const int LED = 13;
const int rightBUTTON = 7;
const int leftBUTTON = 6;
const int upBUTTON = 5;
const int downBUTTON = 4;
#include "map.h"
#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);
// button values
int Rval = 0;
int Lval = 0;
int Uval = 0;
int Dval = 0;
// counter
int i;
//player cordinates
int playerX = 0;
int playerY = 1;
int ghostDELAY = 0;
//map
int MAP [2] [16] = {
{128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143},
{148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163}
};
//LCD command corosponding to the courdenents
int playerLOCATION;
byte steeve[8] = {
B00000, //00000
B01110, // XXX
B01110, // XXX
B11111, //XXXXX
B10101, //X X X
B01110, // XXX
B01010, // X X
};
byte ghost[8] = {
B00000,//
B01110,// XXX
B10101,//X X X
B11111,//XXXXX
B11111,//XXXXX
B11111,//XXXXX
B01010,// X X
B00000,//
};
//HERE IS THE CLASS
//ghost AI
class Ghost{
private:
int x;
int y;
public:
int LOCATION;
Ghost(){
x = 8;
y = 1;
LOCATION = MAP[y] [x];
}
void render(int z){
mySerial.write(z);
mySerial.write(2);
}
void chase(){
// if(ghostDELAY >= 500){
if(x > playerX){
mySerial.write(LOCATION);
mySerial.print(" ");
x--;
if (x == -1){
x = 15;
}
LOCATION = MAP [y] [x];
mySerial.write(LOCATION);
mySerial.write(2);
ghostDELAY = 0;
}else if(x < playerX){
mySerial.write(LOCATION);
mySerial.print(" ");
x++;
if (x == -1){
x = 15;
}
LOCATION = MAP [y] [x];
mySerial.write(LOCATION);
mySerial.write(2);
ghostDELAY = 0;
}
}
// }
};
void setup() {
pinMode(LED, OUTPUT);
pinMode(rightBUTTON, INPUT);
pinMode(leftBUTTON, INPUT);
pinMode(upBUTTON, INPUT);
pinMode(downBUTTON, INPUT);
pinMode(TxPin, OUTPUT);
digitalWrite(TxPin, HIGH);
mySerial.begin(9600);
delay(100);
mySerial.write(249);
for (i = 0; i < 8; i++) {
mySerial.write(steeve[i]);
}
mySerial.write(250);
for (i = 0; i < 8; i++) {
mySerial.write(ghost[i]);
}
mySerial.write(12); // Clear
mySerial.write(17); // Turn backlight on
mySerial.write(22);
delay(5); // Required delay
mySerial.print("Look What I Made"); // First line
delay(3000);
mySerial.write(12);
delay(5);
playerLOCATION = MAP [playerY] [playerX];
mySerial.write(playerLOCATION);
mySerial.write(1);
//IT WORKS FINE HERE
Ghost ghost;
ghost.render(ghost.LOCATION);
delay(500);
ghost.chase(); //Ghost moves one space towards the player
delay(1000); // Wait 3 seconds
}
void loop() {
Rval = digitalRead(rightBUTTON);
Lval = digitalRead(leftBUTTON);
Uval = digitalRead(upBUTTON);
Dval = digitalRead(downBUTTON);
playerLOCATION = MAP [playerY] [playerX];
//command for right button
if (Rval == HIGH){
digitalWrite(LED, HIGH);
mySerial.write(playerLOCATION);
mySerial.print(" ");
playerX++;
if (playerX == 16){
playerX = 0;
}
playerLOCATION = MAP [playerY] [playerX];
mySerial.write(playerLOCATION);
mySerial.write(1);
ghostDELAY += 250;
delay(250);
}else if (Lval == HIGH){ //left button
digitalWrite(LED, HIGH);
mySerial.write(playerLOCATION);
mySerial.print(" ");
playerX--;
if (playerX == -1){
playerX = 15;
}
playerLOCATION = MAP [playerY] [playerX];
mySerial.write(playerLOCATION);
mySerial.write(1);
ghostDELAY += 250;
delay(250);
}else if (Dval == HIGH){ //up button
digitalWrite(LED, HIGH);
mySerial.write(playerLOCATION);
mySerial.print(" ");
playerY++;
if (playerY == 2){
playerY = 0;
}
playerLOCATION = MAP [playerY] [playerX];
mySerial.write(playerLOCATION);
mySerial.write(1);
ghostDELAY += 250;
delay(250);
}else if (Uval == HIGH){ //up button
digitalWrite(LED, HIGH);
mySerial.write(playerLOCATION);
mySerial.print(" ");
playerY--;
if (playerY == -1){
playerY = 1;
}
playerLOCATION = MAP [playerY] [playerX];
mySerial.write(playerLOCATION);
mySerial.write(1);
ghostDELAY += 250;
delay(250);
}else {
digitalWrite(LED, LOW);
}
ghostDELAY += 1;
ghost.chase(); //THIS IS WILL IT WILL NOT WORK
delay (1);
}
static Ghost ghost; // <<<<<<<<<< Add this line outside of all functions.
void setup() {
...
Ghost ghost; // <<<<<<<<<< Remove this line.
...
}
now it just says it is a conflicting deceleration "Ghost ghost" but it seemed to work after I changed it from "ghost" to "ghost1". I compiled it and loaded it onto my board and all is going smoothly. Thank you!!!
~aaaelite21
byte ghost[8] = {
B00000,//
B01110,// XXX
...
Ah yes. Sorry about that.
What does static do for us here?
Thanks,
John
https://www.google.com/search?q=c%2B%2B+scope
Module (file) level scope is correct for ghost. It has the benefits of a global variable but one less potential problem.
In my experience, linkers are evil. Making it module scope keeps it away from the linker. Staying away from evil is a good thing.
As a bonus, the more restrictive the scope the more aggresive the optimizer can be.
Note that this didn't really have anything to do with classes or class construction; it was a simple "scoping" problem. The variable ("ghost") was declared inside of setup(), which means that it was only visible inside of setup(). To be visible inside of loop() as well, the variable has to be declared more globally...