I want to make a brick breaking game with Arduino. I want the ball to break and bounce whichever brick it hits above, but this does not happen with the code I wrote, where is my mistake?
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 4
#define potPin A0
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int paletBas = SCREEN_WIDTH / 2;
int paletBas_e = SCREEN_WIDTH / 2;
int palet_g = 20;
int palet_y = 4;
int topBoyut = 2;
int topX = SCREEN_WIDTH / 2;
int topX_e = SCREEN_WIDTH / 2;
int topY = SCREEN_HEIGHT - 25;
int topY_e = SCREEN_HEIGHT - 25;
int topDX = 1;
int topDY = -1;
int tugla_g = 12;
int tugla_y = 5;
int toplamTugla = SCREEN_WIDTH / (tugla_g + 2);
int t_Sira = 3;
bool tugla[3][20];
boolean game = true;
void setup()
{
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
tuglaVar();
}
void loop()
{
if(game)
{
tuglaCiz();
paletCiz();
topCiz();
paletHareket();
topHareket();
boolean palet_kontrol = paletKontrol();
boolean tugla_kontrol = tuglaKontrol();
if(tugla_kontrol)
{
tuglaCiz();
}
}
}
void tuglaVar()
{
for (int i = 0; i < t_Sira; i++) {
for (int j = 0; j < toplamTugla; j++) {
tugla[i][j] = true;
}
}
}
boolean tuglaKontrol()
{
int topSol = topX - topBoyut; // Topun sol koordinatı
int topSag = topX + topBoyut; // Topun sağ koordinatı
int topUst = topY - topBoyut; // Topun üst koordinatı
int topAlt = topY + topBoyut; // Topun alt koordinatı
for (int i = 0; i < t_Sira; i++) {
for (int j = 0; j < toplamTugla; j++) {
if (tugla[i][j]) {
int tuglaX = j * (tugla_g + 2);
int tuglaY = i * (tugla_y + 2);
if(topAlt >= tuglaY && topUst <= tuglaY + tugla_g){
if(topSag >= tuglaX && topSol <= tuglaX + tugla_y){
tugla[i][j] = false;
topDY = -topDY;
return true;
}
}
}
}
}
return false; // Çarpışma olmadığını bildir
}
void tuglaCiz()
{
for (int i = 0; i < t_Sira; i++) {
for (int j = 0; j < toplamTugla; j++) {
int tuglaX;
int tuglaY;
if (tugla[i][j]) {
tuglaX = j * (tugla_g + 2);
tuglaY = i * (tugla_y + 2);
display.fillRect(tuglaX, tuglaY, tugla_g, tugla_y, WHITE);
}
else{
display.fillRect(tuglaX, tuglaY, tugla_g, tugla_y, BLACK);
}
}
}
display.display();
}
void paletHareket()
{
int potValue = analogRead(potPin);
paletBas_e = paletBas;
paletBas = map(potValue, 0, 1023, 0, SCREEN_WIDTH - palet_g);
}
boolean paletKontrol()
{
if (topX >= paletBas && topX <= paletBas + palet_g && topY >= SCREEN_HEIGHT - 8 - topBoyut) {
topDY = -topDY; // Palete çarptığında yön değiştir
return true;
}
return false;
}
void paletCiz()
{
display.fillRect(paletBas_e, SCREEN_HEIGHT - 8, palet_g, palet_y, BLACK);
display.fillRect(paletBas, SCREEN_HEIGHT - 8, palet_g, palet_y, WHITE);
}
void topHareket()
{
topX_e = topX;
topY_e = topY;
topX += topDX; // Topu sağa doğru hareket ettir
topY += topDY; // Topu yukarıya doğru hareket ettir
if (topX >= SCREEN_WIDTH - topBoyut || topX <= topBoyut) {
topDX = -topDX; // Duvarlara çarpınca yön değiştir
}
if (topY <= topBoyut) {
topDY = -topDY; // Tavana çarpınca yön değiştir
}
}
void topCiz()
{
display.fillCircle(topX_e, topY_e, topBoyut, BLACK);
display.fillCircle(topX, topY, topBoyut, WHITE);
}
I want to make a brick breaking game with Arduino. I want the ball to break and bounce whichever brick it hits above, but this does not happen with the code I wrote, where is my mistake?