Question
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
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. I know that I'm missing the actual game logic and some of the actual stuff so if someone could edit it a bit for me and add that stuff in that would be great so that it can be a playable game.
#include
// 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);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started