SlideShare a Scribd company logo
What's wrong with my code?
I am coding a battleship game that uses a dpad using C on an arduino mega which is connected
to a screen. I noticed that something is missing from my code but I can't figure out what it is and
why battleship is not running. For context, we have two buttons: one for select and one for
deselect. I need that incorporated as well.
#include <TFT_eSPI.h>
// Define the TFT display object
TFT_eSPI tft = TFT_eSPI();
// Define the dpad pins
#define UP_PIN 2
#define DOWN_PIN 3
#define LEFT_PIN 4
#define RIGHT_PIN 5
#define CONFIRM_PIN 6
#define SELECT_PIN 7
#define DESELECT_PIN 8
// Define the grid size
const int GRID_SIZE = 10;
// Define the ship lengths
const int SHIP_LENGTHS[] = {1, 2, 3, 4};
// Define the player grids
int player1Grid[GRID_SIZE][GRID_SIZE];
int player2Grid[GRID_SIZE][GRID_SIZE];
// Define the current player and ship length
int currentPlayer = 1;
int currentShipLength = 1;
// Define the ship placement position
int shipRow = 0;
int shipCol = 0;
int shipDirection = 0; // 0 = horizontal, 1 = vertical
// Function to initialize the game
void initializeGame() {
// Clear the player grids
memset(player1Grid, 0, sizeof(player1Grid));
memset(player2Grid, 0, sizeof(player2Grid));
// Set the current player and ship length
currentPlayer = 1;
currentShipLength = 1;
// Set the initial ship placement position
shipRow = 0;
shipCol = 0;
shipDirection = 0;
// Draw the player grids
tft.fillScreen(TFT_BLACK);
tft.drawRect(0, 0, tft.width() / 2 - 1, tft.height() - 1, TFT_WHITE);
tft.drawRect(tft.width() / 2, 0, tft.width() / 2 - 1, tft.height() - 1, TFT_WHITE);
// Draw the current ship
tft.drawRect(tft.width() / 2 + shipCol * 20, shipRow * 20, currentShipLength * 20 - 1, 19,
TFT_WHITE);
}
// Function to update the ship placement position
void updateShipPosition(int direction) {
// Calculate the new ship position
int newRow = shipRow;
int newCol = shipCol;
if (direction == 0) { // up
newRow--;
} else if (direction == 1) { // down
newRow++;
} else if (direction == 2) { // left
newCol--;
} else if (direction == 3) { // right
newCol++;
}
// Check if the new position is valid
if (newRow >= 0 && newRow < GRID_SIZE && newCol >= 0 && newCol < GRID_SIZE -
currentShipLength + 1) {
// Erase the current ship
tft.drawRect(tft.width() / 2 + shipCol * 20, shipRow * 20, currentShipLength * 20 - 1, 19,
TFT_BLACK);
// Update the ship position
shipRow = newRow;
shipCol = newCol;
// Draw the current ship
tft.drawRect(tft.width() / 2 + shipCol * 20, shipRow * 20, currentShipLength * 20 - 1, 19,
TFT_WHITE);
}
}
// Function to update the ship placement direction
void updateShipDirection() {
// check if the "right" button is pressed
if (digitalRead(RIGHT_BUTTON_PIN) == LOW) {
// increment the ship direction
shipDirection++;
// wrap the direction around if it goes past 3
if (shipDirection > 3) {
shipDirection = 0;
}
// draw the ship in the new direction
drawShip(shipX, shipY, shipLength, shipDirection);
}
// check if the "left" button is pressed
else if (digitalRead(LEFT_BUTTON_PIN) == LOW) {
// decrement the ship direction
shipDirection--;
// wrap the direction around if it goes below 0
if (shipDirection < 0) {
shipDirection = 3;
}
// draw the ship in the new direction
drawShip(shipX, shipY, shipLength, shipDirection);
}
}

More Related Content

TXT
PIC and LCD
DOCX
2.1 ### uVision Project, (C) Keil Software .docx
PDF
i have a runable code below that works with just guessing where the .pdf
PDF
This is the Java code i have for a Battleship project i am working o.pdf
DOCX
Microcontroladores: programas de CCS Compiler.docx
DOCX
codings related to avr micro controller
PDF
send EDITED code pleaseBattleship.javapackage part3;public cla.pdf
TXT
Senior design project code for PPG
PIC and LCD
2.1 ### uVision Project, (C) Keil Software .docx
i have a runable code below that works with just guessing where the .pdf
This is the Java code i have for a Battleship project i am working o.pdf
Microcontroladores: programas de CCS Compiler.docx
codings related to avr micro controller
send EDITED code pleaseBattleship.javapackage part3;public cla.pdf
Senior design project code for PPG

Similar to What's wrong with my code- I am coding a battleship game that uses a d (1).pdf (20)

PDF
i have a code that runs, but it only lets the player to guess where .pdf
PDF
main.pdf java programming practice for programs
DOCX
Dam gate open close lpc prog
PDF
The following code, is a one player battleship game in JAVA. Im tryi.pdf
PDF
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
PPTX
Cvim half precision floating point
PDF
Runtime Code Generation and Data Management for Heterogeneous Computing in Java
PDF
Creating an Uber Clone - Part III.pdf
DOCX
write the TODO part of the program.docx
PDF
Combine the keypad and LCD codes in compliance to the following requ.pdf
PDF
Embedded Systems Project 3rd Year
PPTX
Box2D with SIMD in JavaScript
PPTX
2011.02.18 marco parenzan - modelli di programmazione per le gpu
PDF
Can you finish and write the int main for the code according to the in.pdf
DOCX
Xiicsmonth
PDF
Arduino uno basic Experiments for beginner
PDF
All I know about rsc.io/c2go
PDF
PDF
Functions for Nano 5 Card
PDF
The Ring programming language version 1.5 book - Part 2 of 31
i have a code that runs, but it only lets the player to guess where .pdf
main.pdf java programming practice for programs
Dam gate open close lpc prog
The following code, is a one player battleship game in JAVA. Im tryi.pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
Cvim half precision floating point
Runtime Code Generation and Data Management for Heterogeneous Computing in Java
Creating an Uber Clone - Part III.pdf
write the TODO part of the program.docx
Combine the keypad and LCD codes in compliance to the following requ.pdf
Embedded Systems Project 3rd Year
Box2D with SIMD in JavaScript
2011.02.18 marco parenzan - modelli di programmazione per le gpu
Can you finish and write the int main for the code according to the in.pdf
Xiicsmonth
Arduino uno basic Experiments for beginner
All I know about rsc.io/c2go
Functions for Nano 5 Card
The Ring programming language version 1.5 book - Part 2 of 31
Ad

More from atexgarments (20)

PDF
When I input the number 3- my output is -Ready! 0 Go!----- what gives-.pdf
PDF
When is it appropriate to use a one-sample Z-test instead of a one-sam.pdf
PDF
When planning for the scope of a project- the following are created- P.pdf
PDF
When do think it would be advantageous to have background images- Word.pdf
PDF
When Griffin cultured the blood from the mice injected with R cells an.pdf
PDF
What's the best explanation of a level of significance- Select one- a-.pdf
PDF
When configuring an 802-1X-EAP solution- what must be configured on th.pdf
PDF
What would you predict if Morrison and Braciale tested their influenza.pdf
PDF
What type of unconformity does C represent- nonconformity disconformit.pdf
PDF
What would happen to the composition of the interstitial fluid if astr.pdf
PDF
What would happen if NAD+ was not generated for the citric acid cycle-.pdf
PDF
When constructing a pro forma Balance Sheet- is-are considered as Curr (1).pdf
PDF
What type of programming error is an attacker likely to attempt to exp.pdf
PDF
what was henry wirtz executed for- what was henry wirtz executed for-.pdf
PDF
what would be the standard deciation of Bank B- Ba Bi Cick the ioon to.pdf
PDF
When cells release signal molecules (ligands) to send a message- and t (1).pdf
PDF
What type of volcano is shown in the ahoto- Glick to view laresimase d.pdf
PDF
When award without discussions is not stated in the solicitation and t.pdf
PDF
Which Munsell color notation has the lowest chroma- 10YR 1-2 5YR 6-7 2.pdf
PDF
What type of value is being created by JD-com's digital and online ini.pdf
When I input the number 3- my output is -Ready! 0 Go!----- what gives-.pdf
When is it appropriate to use a one-sample Z-test instead of a one-sam.pdf
When planning for the scope of a project- the following are created- P.pdf
When do think it would be advantageous to have background images- Word.pdf
When Griffin cultured the blood from the mice injected with R cells an.pdf
What's the best explanation of a level of significance- Select one- a-.pdf
When configuring an 802-1X-EAP solution- what must be configured on th.pdf
What would you predict if Morrison and Braciale tested their influenza.pdf
What type of unconformity does C represent- nonconformity disconformit.pdf
What would happen to the composition of the interstitial fluid if astr.pdf
What would happen if NAD+ was not generated for the citric acid cycle-.pdf
When constructing a pro forma Balance Sheet- is-are considered as Curr (1).pdf
What type of programming error is an attacker likely to attempt to exp.pdf
what was henry wirtz executed for- what was henry wirtz executed for-.pdf
what would be the standard deciation of Bank B- Ba Bi Cick the ioon to.pdf
When cells release signal molecules (ligands) to send a message- and t (1).pdf
What type of volcano is shown in the ahoto- Glick to view laresimase d.pdf
When award without discussions is not stated in the solicitation and t.pdf
Which Munsell color notation has the lowest chroma- 10YR 1-2 5YR 6-7 2.pdf
What type of value is being created by JD-com's digital and online ini.pdf
Ad

Recently uploaded (20)

PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Institutional Correction lecture only . . .
PPTX
Cell Structure & Organelles in detailed.
PPTX
master seminar digital applications in india
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
01-Introduction-to-Information-Management.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Classroom Observation Tools for Teachers
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
O7-L3 Supply Chain Operations - ICLT Program
Supply Chain Operations Speaking Notes -ICLT Program
Chinmaya Tiranga quiz Grand Finale.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Institutional Correction lecture only . . .
Cell Structure & Organelles in detailed.
master seminar digital applications in india
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
01-Introduction-to-Information-Management.pdf
Microbial disease of the cardiovascular and lymphatic systems
O5-L3 Freight Transport Ops (International) V1.pdf
Cell Types and Its function , kingdom of life
Classroom Observation Tools for Teachers
202450812 BayCHI UCSC-SV 20250812 v17.pptx

What's wrong with my code- I am coding a battleship game that uses a d (1).pdf

  • 1. What's wrong with my code? I am coding a battleship game that uses a dpad using C on an arduino mega which is connected to a screen. I noticed that something is missing from my code but I can't figure out what it is and why battleship is not running. For context, we have two buttons: one for select and one for deselect. I need that incorporated as well. #include <TFT_eSPI.h> // Define the TFT display object TFT_eSPI tft = TFT_eSPI(); // Define the dpad pins #define UP_PIN 2 #define DOWN_PIN 3 #define LEFT_PIN 4 #define RIGHT_PIN 5 #define CONFIRM_PIN 6 #define SELECT_PIN 7 #define DESELECT_PIN 8 // Define the grid size const int GRID_SIZE = 10; // Define the ship lengths const int SHIP_LENGTHS[] = {1, 2, 3, 4}; // Define the player grids int player1Grid[GRID_SIZE][GRID_SIZE]; int player2Grid[GRID_SIZE][GRID_SIZE]; // Define the current player and ship length int currentPlayer = 1;
  • 2. int currentShipLength = 1; // Define the ship placement position int shipRow = 0; int shipCol = 0; int shipDirection = 0; // 0 = horizontal, 1 = vertical // Function to initialize the game void initializeGame() { // Clear the player grids memset(player1Grid, 0, sizeof(player1Grid)); memset(player2Grid, 0, sizeof(player2Grid)); // Set the current player and ship length currentPlayer = 1; currentShipLength = 1; // Set the initial ship placement position shipRow = 0; shipCol = 0; shipDirection = 0; // Draw the player grids tft.fillScreen(TFT_BLACK); tft.drawRect(0, 0, tft.width() / 2 - 1, tft.height() - 1, TFT_WHITE); tft.drawRect(tft.width() / 2, 0, tft.width() / 2 - 1, tft.height() - 1, TFT_WHITE); // Draw the current ship tft.drawRect(tft.width() / 2 + shipCol * 20, shipRow * 20, currentShipLength * 20 - 1, 19, TFT_WHITE);
  • 3. } // Function to update the ship placement position void updateShipPosition(int direction) { // Calculate the new ship position int newRow = shipRow; int newCol = shipCol; if (direction == 0) { // up newRow--; } else if (direction == 1) { // down newRow++; } else if (direction == 2) { // left newCol--; } else if (direction == 3) { // right newCol++; } // Check if the new position is valid if (newRow >= 0 && newRow < GRID_SIZE && newCol >= 0 && newCol < GRID_SIZE - currentShipLength + 1) { // Erase the current ship tft.drawRect(tft.width() / 2 + shipCol * 20, shipRow * 20, currentShipLength * 20 - 1, 19, TFT_BLACK); // Update the ship position shipRow = newRow; shipCol = newCol;
  • 4. // Draw the current ship tft.drawRect(tft.width() / 2 + shipCol * 20, shipRow * 20, currentShipLength * 20 - 1, 19, TFT_WHITE); } } // Function to update the ship placement direction void updateShipDirection() { // check if the "right" button is pressed if (digitalRead(RIGHT_BUTTON_PIN) == LOW) { // increment the ship direction shipDirection++; // wrap the direction around if it goes past 3 if (shipDirection > 3) { shipDirection = 0; } // draw the ship in the new direction drawShip(shipX, shipY, shipLength, shipDirection); } // check if the "left" button is pressed else if (digitalRead(LEFT_BUTTON_PIN) == LOW) { // decrement the ship direction shipDirection--; // wrap the direction around if it goes below 0 if (shipDirection < 0) {
  • 5. shipDirection = 3; } // draw the ship in the new direction drawShip(shipX, shipY, shipLength, shipDirection); } }