Hi! I'm working on a final project for one of my classes and decided to make a simple platforming game. However, I'm having trouble with the collision detection of blocks. Right now the player controlled sprite just gets printed in the same space as the block.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // RS, E, D4, D5, D6, D7
byte AMONGUS[] =
{
B01110,
B10001,
B10111,
B10101,
B10111,
B10001,
B10101,
B11011
};
byte Block[]
{
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
int joyPosY;
int joyPosX;
int col;
void setup()
{
// Specify the LCD's number of columns and rows:
lcd.begin(16, 2);
// Create new characters:
lcd.createChar(0, AMONGUS);
// Clears the LCD screen:
lcd.clear();
pinMode(A0, INPUT); //sets pin A0 to input info
pinMode(A1, INPUT);
Serial.begin(9600);
}
void bottomRowMove()
{
if(joyPosX >= 400)
{
lcd.clear();
col++;
}
else if(joyPosX <= 300)
{
lcd.clear();
if(col > 0)
{
col--;
}
else
{
col = 0;
}
}
}
void upperRowMove()
{
if(joyPosX >= 520)
{
lcd.clear();
if(col < 16)
{
col++;
}
else
{
col = 15;
}
}
else if(joyPosX <= 300)
{
lcd.clear();
if(col > 0)
{
col--;
}
else
{
col = 0;
}
}
}
void placeBlocks()
{
lcd.createChar(1, Block);
lcd.setCursor(4,1);
lcd.write(byte(1));
lcd.setCursor(7, 0);
lcd.write(byte(1));
}
void loop()
{
joyPosY = analogRead(A0);
joyPosX = analogRead(A1);
Serial.print("X Pos: ");
Serial.println(joyPosX);
Serial.print("Y Pos: ");
Serial.println(joyPosY);
lcd.clear();
// Print all the custom characters:
lcd.setCursor(col, 1);
lcd.write(byte(0));
placeBlocks();
while(joyPosY >= 900)
{
lcd.clear();
joyPosY = analogRead(A0);
joyPosX = analogRead(A1);
placeBlocks();
lcd.setCursor(col, 0);
lcd.write(byte(0));
delay(100);
upperRowMove();
}
delay(100);
bottomRowMove();
}