What's new

Java Virtual Pet - OOP

Laxxy

Eternal Poster
Established
Joined
Oct 15, 2016
Posts
681
Reaction
961
Points
341
Hello po, gumagawa po ako ngayon ng virtual pet game para sa subject ko sa oop. Pero I need help kung pano po maiimplement yung coins, energy bar, hygiene bar (kapag low need na maligo), and yung hunger bar. bali gumagamit po kami ng JFrame tapos need na mababawasan or madadagdagan yung bar everytime na may ginawang activity. tapos yung coins naman po is nadadagdagan kapag naglaro ng mini games tapos nababawasan kapag bumili sa shop.

recommend naman po kayo ng mga possible methods para maimplement po yung nasa taas. Thank you!

Edit: Need po pala na nasa txt file yung mga attributes na nasa taas, bali parang nakadatabase po siya

PS. JAVA PO GAMIT NAMIN
 
Last edited:
Hi there! I would be happy to help you with your virtual pet game. Here are some possible methods to implement the features you mentioned:

1. Coins: You can create a variable that stores the number of coins the player has. Whenever the player wins a mini game, you can add a certain number of coins to this variable. Similarly, when the player buys something from the shop, you can subtract the cost from the coins variable.

For example:

Code:
int coins = 0;

// When player wins a mini game:
coins += 10;

// When player buys an item from shop:
coins -= 5;

2. Energy, hygiene, and hunger bars: You can create variables that represent the current level of energy, hygiene, and hunger. Whenever the player performs an action that affects these bars, you can update the variables accordingly. For example, if the player feeds the pet, you can increase the hunger variable.

You can also create a timer that gradually decreases the energy and hygiene variables over time. This simulates the pet becoming tired or dirty if it's not taken care of.

For example:

Code:
int energy = 100;
int hygiene = 100;
int hunger = 0;

// When player feeds the pet:
hunger -= 10;

// When player cleans the pet:
hygiene += 20;

// Timer to decrease energy and hygiene over time:
Timer timer = new Timer(1000, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        energy -= 5;
        hygiene -= 2;
    }
});
timer.start();

3. Updating the bars on the JFrame: Once you have the variables for coins, energy, hygiene, and hunger, you can display them on the JFrame using JLabels or JProgressBars. Whenever these variables change, you can update the corresponding label or progress bar to reflect the new value.

For example:

Code:
// Create the labels and progress bars:
JLabel coinsLabel = new JLabel("Coins: " + coins);
JLabel energyLabel = new JLabel("Energy: " + energy);
JProgressBar hygieneBar = new JProgressBar(0, 100);
hygieneBar.setValue(hygiene);
JProgressBar hungerBar = new JProgressBar(0, 100);
hungerBar.setValue(hunger);

// Update the labels and progress bars when the variables change:
coinsLabel.setText("Coins: " + coins);
energyLabel.setText("Energy: " + energy);
hygieneBar.setValue(hygiene);
hungerBar.setValue(hunger);

I hope this helps! Let me know if you have any questions or need further assistance.
 

Similar threads

Back
Top