Suggestions for C++ game! #179567
Replies: 6 comments
-
Hey there!I really like the foundation of your code for this simple shooter/duel game—it's got some great potential to be fun and engaging. That said, there are a few areas where it could use some tweaks to make it more stable, playable, and polished. I'll break it down step by step, highlighting the key issues I spotted (both structural and programming-wise) and suggesting improvements. I've also included a revised version of the code at the end to show how it could look after these changes. Key Issues and ImprovementsConst on Shared Pointers Blocks MutabilityProblem: Declaring Missing Names for EntitiesProblem: Without setting names, the output (like in Game Over Logic Doesn't Halt the GameProblem: The Lack of a Proper Game LoopProblem: Without a loop, the game runs linearly (e.g., just a couple of shots) and ends prematurely, making it feel more like a demo than an interactive game. No player input means it's not engaging. Suggestions for Taking It to the Next LevelTo make this more structured and professional, consider these upgrades—they'll help organize the code and add depth without overcomplicating things: Introduce a Game ClassThis moves logic out of class Game {
std::shared_ptr<Entity> player, enemy;
bool running = true;
public:
void init();
void update();
void render() const;
void run();
};Enhance the shoot() MethodAdd flair with sound effects in output, check for ammo, handle damage, and reward scores on kills. This makes combat feel more dynamic. void shoot(const std::shared_ptr<Entity>& target) {
if (ammo > 0) {
ammo--;
std::cout << name << " *PEW PEW*!\n";
target->take_damage();
if (target->is_dead()) {
increment_score();
std::cout << "KILL! +" << 1 << " SCORE\n";
}
} else {
std::cout << "CLICK! Out of ammo, scrub!\n";
}
}Add take_damage() and is_dead() MethodsThese separate concerns: void take_damage() {
if (lives > 0) lives--;
if (lives == 0) game_over();
}
bool is_dead() const { return lives == 0; }Incorporate Player InputLet the user decide actions, like shooting or quitting, to turn it into a real game. char cmd;
std::cout << "[s]hoot / [q]uit: ";
std::cin >> cmd;
if (cmd == 's') player->shoot(enemy);Updated Main Function ExampleHere's how the #include <iostream>
#include <memory>
#include <string>
// Assuming Entity class definition (expand as needed)
class Entity {
public:
std::string name;
int lives = 3;
int ammo = 5;
int score = 0;
void set_name(const std::string& n) { name = n; }
int get_lives() const { return lives; }
void shoot(const std::shared_ptr<Entity>& target) {
if (ammo > 0) {
ammo--;
std::cout << name << " *PEW PEW*!\n";
target->take_damage();
if (target->is_dead()) {
score++;
std::cout << "KILL! +1 SCORE\n";
}
} else {
std::cout << "CLICK! Out of ammo!\n";
}
}
void take_damage() {
if (lives > 0) lives--;
if (lives == 0) game_over();
}
bool is_dead() const { return lives == 0; }
void output_stats() const {
std::cout << name << "'s stats: Lives=" << lives << ", Ammo=" << ammo << ", Score=" << score << "\n";
}
void game_over() {
std::cout << name << " is defeated!\n";
// Set a flag or handle exit in the game loop
}
void increment_score() { score++; } // If needed separately
};
int main() {
auto player = std::make_shared<Entity>();
auto enemy = std::make_shared<Entity>();
player->set_name("Rambo");
enemy->set_name("Terminator");
std::cout << "=== DUEL TO THE DEATH ===\n\n";
while (player->get_lives() > 0 && enemy->get_lives() > 0) {
// Player turn with input
char cmd;
std::cout << "[s]hoot / [q]uit: ";
std::cin >> cmd;
if (cmd == 'q') break;
if (cmd == 's') player->shoot(enemy);
if (enemy->get_lives() == 0) break;
// Enemy turn (AI or simulated)
enemy->shoot(player);
player->output_stats();
enemy->output_stats();
std::cout << "--------\n";
}
std::cout << "GAME OVER!\n";
return 0;
}This version adds a loop, input, and better logic while keeping things simple. It should compile and run without crashes (assuming the full Overall, your code has a solid start—it's creative and straightforward. With these changes, it'll feel more like a complete game. 😊 |
Beta Was this translation helpful? Give feedback.
-
|
Haha, first off, love the attitude in the comments 😂. So, about your game:
Overall, good base, just expect to refactor a bit as you add complexity. |
Beta Was this translation helpful? Give feedback.
-
|
If you want some feedback on this, here are a few things I noticed: The comments are funny but they’re also kind of distracting. Maybe clean them up so the code is easier to read. In the shot() function, the game-over logic is a bit off. Right now it only triggers after lives drop below 1, but the logic stops decreasing at 0, so it never actually hits the else branch. Might be worth fixing that. You’re using shared_ptr, but it doesn’t look like you actually need shared ownership here. Regular references or pointers would work fine and keep things simpler. Maybe give entities a default name instead of an empty string. Just helps when printing stats or debugging. Some of the methods could probably return a value instead of doing everything silently (for example shoot() could return whether the shot actually happened). If you plan to expand this later, you might want to put all the game logic in a separate Game class instead of doing everything inside main(). |
Beta Was this translation helpful? Give feedback.
-
|
Hi @kill3rc, You’ve made a solid start on your C++ game’s Entity system—nice job on structuring the code and using smart pointers for entity management! Here are some friendly suggestions to help develop your game further:
Here’s a super-simple game loop sketch to inspire you: while (player->get_lives() > 0 && enemy->get_lives() > 0) {
// Player turn: prompt input (shoot/reload)
// Enemy turn: random action
// Show stats
}Keep experimenting and iterating—game development is all about trial, error, and fun improvements. Great start, and happy coding! |
Beta Was this translation helpful? Give feedback.
-
|
I think its pretty good on the beginner level, we all learn and go through these games, one thing I would suggest you to try board games, they are pretty fun to make and doesn't require you GUI, you can just play and render such games on your console, the main thing is you'll get to develop logic pretty well, you can also see some games I have built in my profile (not promoting, just for your reference), I have created Tower-of-Hanoi and TicTacToe (multiplayer), also I made those games when I was learning C++, I wanted to test my learning so when I was learning stack, I built Tower-of-Hanoi, when I was learning arrays and general stuff, I built TicTacToe, also whenever I try learning new language I prefer to build games in them as it makes me spend some time on them improving my logic and it helps me explore such aspects of that language we don't explore during traditional learning. |
Beta Was this translation helpful? Give feedback.
-
|
Cool ideas |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Body
Guidelines
Beta Was this translation helpful? Give feedback.
All reactions