Reputation: 11
The code is for a game that uses joystick buttons and various sensors. However, when compiling, there are no error messages, but the code still does not compile successfully. By not compiling successfully I mean that my software (Bricx Command Center) gives me a message that the compiling failed but the software doesnt tell me why it failed. Normally the programm tells me what I did wrong and shows me the line where i made a mistake but this time there is no single one of those messages and it still fails to compile.
The code contains functions like GameOver(), ShowMenu(), StartGame(), and sensor initialization with SetSensorTouch(). Despite the lack of error outputs during compilation, the program does not work as expected.
What could be the reason the code is not compiling without providing any clear error messages, and how can I resolve this issue? You can also try to fix it yourself imma post the code. Additional Notes:
The code uses a joystick and buttons that are connected to ports IN_1, IN_2, IN_3, and IN_4. There are no specific error messages during the compilation process, but the program does not run properly. The sensors are correctly initialized and configured with SetSensorTouch(). The joystick button is actually the same type of button as the others; it's just integrated into the joystick assembly, so the button itself behaves identically to the other buttons.
This is a basic touch-based game where the player must react to button prompts, earn points, and avoid errors to keep playing. The difficulty can be adjusted to provide a challenge for players of different skill levels. The joystick button is integrated into the game like the other buttons, and the program uses these inputs to drive the gameplay. This is the Code:
// Global variables
int score = 0;
int errors = 0;
int gameRunning = 0;
int selectedLevel = 0;
// Game Over function
void GameOver() {
ClearScreen();
TextOut(10, 2, "GAME OVER!");
PlayTone(300, 50);
Wait(2000);
while (Sensor(IN_4) == 0) {
// Wait until the joystick button is pressed
}
}
// Display menu
void ShowMenu() {
selectedLevel = 0; // Default level
ClearScreen();
TextOut(10, 0, "Menu");
while (true) {
ClearLine(1); // Clear menu entries
if (selectedLevel == 0) {
TextOut(10, 1, "Easy");
} else if (selectedLevel == 1) {
TextOut(10, 1, "Medium");
} else if (selectedLevel == 2) {
TextOut(10, 1, "Hard");
}
// Joystick: Change selection
if (Sensor(IN_1) == 1) { // Right
selectedLevel = (selectedLevel + 1) % 3;
Wait(300);
} else if (Sensor(IN_2) == 1) { // Left
selectedLevel = (selectedLevel + 2) % 3; // Go backward
Wait(300);
}
// Confirm with joystick button
if (Sensor(IN_4) == 1) { // Joystick button
Wait(300);
return; // Selection completed
}
}
}
// Start game
void StartGame(int level) {
ClearScreen();
score = 0;
errors = 0;
gameRunning = 1;
TextOut(10, 0, "Score: 0");
TextOut(10, 1, "Errors: 0/5");
int speed;
if (level == 0) speed = 800; // Easy
else if (level == 1) speed = 500; // Medium
else speed = 300; // Hard
for (int i = 0; i < 20 && gameRunning; i++) {
ClearLine(2);
int action = Random(2); // 0 = normal hit, 1 = Slider
int button = Random(3) + 1; // Random button (1, 2, 3)
if (action == 0) { // Normal hit
TextOut(10, 2, "Hit");
NumOut(50, 2, button);
PlayTone(440, 20);
Wait(speed);
if (Sensor(IN_1) == 1) { // Corrected line for the buttons
score += 5;
PlayTone(600, 20);
} else {
errors += 1;
}
} else { // Slider
TextOut(10, 2, "Slider");
NumOut(50, 2, button);
int sliderProgress = 0;
for (int j = 0; j < 5; j++) {
if (Sensor(IN_1) == 1) { // Corrected line for the buttons
sliderProgress++;
PlayTone(400, 10);
Wait(100);
} else {
break;
}
}
if (sliderProgress >= 5) {
score += 10;
} else {
errors += 1;
}
}
// Update score and errors
ClearLine(0);
TextOut(10, 0, "Score:");
NumOut(50, 0, score);
ClearLine(1);
TextOut(10, 1, "Errors:");
NumOut(60, 1, errors);
TextOut(75, 1, "/5");
// Exit with joystick
if (Sensor(IN_4) == 1) {
gameRunning = 0;
}
// Check for game over
if (errors >= 5) {
GameOver(); // Call the function
return;
}
}
}
// Main program
task main() {
// Initialize sensors
SetSensorTouch(IN_1); // Button 1 at port IN_1
SetSensorTouch(IN_2); // Button 2 at port IN_2
SetSensorTouch(IN_3); // Button 3 at port IN_3
SetSensorTouch(IN_4); // Joystick button at port IN_4
while (true) {
ShowMenu();
StartGame(selectedLevel);
}
}
I tried asking ChatGPT but his Codes don't work at all.
Upvotes: 1
Views: 6