Reputation: 1
Game code:
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
class GamePanel extends JPanel {
// Game parameters
int squareSize = 100;
int squareX;
int squareY;
int health = 10;
boolean showHealthBar = false;
boolean gameOver = false;
boolean gameStarted = false;
Random random = new Random();
Timer hideHealthBarTimer;
Timer gameTimer;
boolean paused = false;
int counter = 0;
Timer counterTimer;
// Variables for square shaking
boolean isShaking = true; // Start shaking immediately
int originalSquareX;
int originalSquareY;
Timer shakeTimer;
int shakeAmplitude = 2; // Amplitude of shaking
// Difficulty settings
String difficulty = "Normal"; // Default difficulty
int selectedDifficulty = -1;
int shakeDelay = 20; // Delay for the shake timer
public GamePanel() {
// Timer for shaking
shakeTimer = new Timer(shakeDelay, e -> {
if (isShaking) {
squareX = originalSquareX + (random.nextBoolean() ?
shakeAmplitude : -shakeAmplitude);
squareY = originalSquareY + (random.nextBoolean() ?
shakeAmplitude : -shakeAmplitude);
repaint(); // Repaint on each shake
}
});
shakeTimer.start(); // Start the shake timer immediately
hideHealthBarTimer = new Timer(500, e -> {
showHealthBar = false;
repaint();
});
hideHealthBarTimer.setRepeats(false);
gameTimer = new Timer(30, e -> {
if (paused || !gameStarted || gameOver) return;
if (health <= 0) {
gameOver = true;
gameTimer.stop();
}
repaint();
});
gameTimer.start();
counterTimer = new Timer(333, e -> { // Slower counter
increase
if (!paused && gameStarted && !gameOver) {
counter++;
repaint();
}
});
counterTimer.start();
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (!gameStarted) {
// ... (difficulty selection logic - unchanged)
} else if (gameStarted && !paused && !gameOver) {
if (e.getX() >= squareX && e.getX() <= squareX +
squareSize &&
e.getY() >= squareY && e.getY() <= squareY +
squareSize) {
counter++;
showHealthBar = true;
hideHealthBarTimer.restart();
updatePositions();
switch (difficulty) {
case "Simple":
shakeAmplitude = 1;
shakeDelay = 30; // Slower shake
break;
case "Normal":
shakeAmplitude = 2;
shakeDelay = 20;
break;
case "Difficult":
shakeAmplitude = 4;
shakeDelay = 10; // Faster shake
break;
}
shakeTimer.setDelay(shakeDelay); // Apply
the delay change
} else {
health--;
if (health <= 0) {
gameOver = true;
gameTimer.stop();
}
}
repaint();
}
}
});
updatePositions();
}
// ... (rest of the GamePanel code - unchanged)
}
public class Game {
public static void main(String[] args) {
// ... (Game class code - unchanged)
}
}
This is the menu code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class menu_introduttivo {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
System.out.println("Starting application...");
JFrame frame = new JFrame("Test Frame");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.BLACK);
JPanel panel = new JPanel() {
// ... (Existing variables)
@Override
protected void paintComponent(Graphics g) {
// ... (Existing drawing code)
}
// ... (Existing drawOptionsPanel method)
@Override
public void addNotify() {
super.addNotify();
// ... (Existing mouseMoved listener)
addMouseListener(new MouseAdapter() {
// ... (Existing mousePressed code for START and MODES)
@Override
public void mousePressed(MouseEvent e) {
// ... (Existing "OPTIONS" and "Did" click handling)
if (optionsMenuOpen) {
// ... (Existing volume circle click handling)
// Check if the sound circle is clicked
// ... (sound circle click handling - unchanged)
}
}
@Override
public void mouseReleased(MouseEvent e) {
// ... (mouseReleased - unchanged)
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (optionsMenuOpen) {
// ... [Existing dragging logic]
}
}
});
// Add mouse listener for click handling
addMouseListener(new MouseAdapter() {
// ... [mousePressed method - unchanged]
});
}
};
frame.add(panel);
frame.setVisible(true); // Make the frame visible after adding components
});
}
}
Trying to put these two files together:
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
// ... (GamePanel and Game classes remain unchanged)
public class menu_introduttivo {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
// ... (Existing setup code)
JPanel panel = new JPanel() {
// ... (Existing variables)
@Override
protected void paintComponent(Graphics g) {
// ... (Existing drawing code)
}
// ... (Existing drawOptionsPanel method)
@Override
public void addNotify() {
super.addNotify();
MouseInputAdapter mouseAdapter = new
MouseInputAdapter() { // Combined listener
@Override
public void mouseMoved(MouseEvent e) {
// ... (Existing mouseMoved logic -
unchanged)
}
@Override
public void mousePressed(MouseEvent e) {
// ... (Existing mousePressed logic -
unchanged)
}
@Override
public void mouseDragged(MouseEvent e) {
if (optionsMenuOpen) {
if (draggingVolume) {
// ... (volume dragging logic -
unchanged)
}
if (draggingSound) {
// ... (sound dragging logic -
unchanged)
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
draggingVolume = false;
draggingSound = false;
}
};
addMouseListener(mouseAdapter); // Add
combined listener
addMouseMotionListener(mouseAdapter); // Add
combined listener
}
};
frame.add(panel);
frame.setVisible(true); // Make the frame visible
*after* adding the panel
});
}
}
They are 2 different code files
This is the menu. It should be the first thing that opens in the game, and if I press start, it should start the other file The game, that opens first a menu difficulty and after when the player chose the difficulty, the game starts
If you press one of the difficulties the game starts:
In this case, the files were separated. If you put the code together, it only can start one of them, or it starts only the menu or only the game with his menu.
So, I want to get this by putting everything together: Opens the game, first thing you see is the menu with options, mode and start, if you press start it should open the difficulty menu of the other file, once you choose the difficulty, the game starts.
Upvotes: -7
Views: 90
Reputation: 76943
You do not need your game and menu files to be merged. It would be an anti-pattern to mix up two files like that. What you want instead is to have a reference to the menu from the game and/or vice-versa.
You can achieve that by defininig data members, for example:
//...
public class Game {
//...
protected Menu menu;
//...
public Game(Menu menu) {
this.menu = menu;
}
//...
}
and
//...
public class Menu {
//...
protected Game game;
//...
public Menu() {
}
//...
public start() {
this.game = new Game(this);
game.start();
}
}
Of course, you may have other parameters in your constructors and other methods/data members of your classes, but basically you can create your menu via
Menu menu = new Menu();
and then start the game whenever it is most feasible, like upon clicking on NEW GAME.
Upvotes: 0