[color=#ff8c00][font=verdana]Introductions![/font][/color]
[font=verdana]Hello everehbody! [/font]
[font=verdana]This is my first journal entry/tutorial here on GameDev, and I hope I do a good job at explaining the concepts I want to get across. This tutorial series is mainly aimed at beginners (given the blog's name) and I hope it's simple enough.[/font]
[color=#ff8c00][font=verdana]What We'll Be Making.[/font][/color]
Using the fantastical language of C++ (which is what this blog is primarily for), we'll be making a turn-and-text-based combat game.
[font=verdana]"What's a turn-and-text-based combat game," you ask? Well, you'll see by the end of this tutorial.[/font]
[font=verdana]The point of this series is to introduce beginners to the basic game loop and to simple algorithms, such as would be seen in any basic RPG game. We'll be making a game where you have a set of things you can do while fighting an enemy.[/font]
[font=verdana]The target product of the first few tutorials is a game where you:[/font]
[font=verdana]Have an intro to set your name[/font]
Hopefully, as we go along, this list will become more apparent.
[color=#ff8c00]
Let's Get Started!
[/color]Alright, so fire up whatever IDE you're using and let's get started!
So, first off, we're only making the barebones of the system in this tutorial. We'll be implementing an Engine later. Open up a new C++ file and name it Being.h.
#ifndef Being_h#define Being_h#include class Being { protected: std::string name; int health, maxHealth; int baseDamage, damage, gunDamage; int ammo, maxAmmo; int potions, maxPotions; public: void meleeAttack(Being& target); void gunAttack(Being& target); void useHP(Being& target); std::string getName(); int getHealth(); int getAmmo(); int getPotions(); void Reset();};#endif
What're we doing here? This is actually pretty simple. We're going to use inheritance to make our Character and Monster classes smaller and simpler.
In Protected, we're defining the values that both Character and Monster will need. Protected makes sure that only Being and it's 'children' will be able to get/modify those values.
In Public, we're defining the methods that other classes (outside of Character and Monster) will need to use; for example, since string name; is a Protected value, we'll need some way for other classes to find out what that Being's name is. The attack types will also need to be used outside of Being.
Not too bad, right? Grab a cup of joe and let's continue.
Alright, so we've got our Being class now. Create a new file and name it Being.cpp, but leave it alone for now. Create a new header file and call it Character.h. Put this stuff in it:
#ifndef Character_h#define Character_h#include "Being.h"class Character : public Being { public: Character(string newName, int newHealth, int newDamage, int newAmmo, int newPotions); void Display();};#endif
Much shorter, thanks to Being. Time for explanations!
Character is inheriting all the values that Being.h has, and that's why we don't need to redefine all the ints. You can call Character a 'child' of Being.
Like before, make a new file called Character.cpp and leave it be. Now, we're going to need to be able to create a Monster just like we will for Character. Create a new header called Monster.h.
#ifndef Monster_h#define Monster_h#include "Being.h"class Monster : public Being { public: Monster(string newName, short newHealth, short newDamage);};#endif
Hopefully, this is self-explanatory. Monster is also a 'child' of Being, since we're inheriting it from Being as well. If you haven't noticed, we're defining fewer values in Monster's constructor; this is because (for now) Monster will only have the Melee Attack type. Once we delve into a more involved combat system, we'll give the monsters their own special attacks.
[color=#ff8c00]
Mortal Combat~!
[/color]I'm sure you've gathered what class is next, based on the title of this section. We'll need to have our Combat class up and ready for the next part of the tutorial, and I think this will be the most interesting part of our game.
#ifndef Combat_h#define Combat_h#include "Character.h"#include "Monster.h"class Combat { private: Monster& M; void combatChoice(Character& C); public: Combat(Monster& newM); void combat1(Character& C);};#endif
Although the class itself is small, our Combat.cpp is going to be pretty involved. Alright, time to explain the things we see in here...
- Monster& M; - This is creating a reference to Monster, and it's how we'll be affecting the monster in Combat.cpp. Our reference here will be referencing the instance of the Monster we'll make in Main.cpp.
- Combat(Monster& newM); - This is Combat's constructor, which is how we'll create new combat instances for when we start a fight with a monster. We're referencing a new Monster here, similar to Monster& M.
- void combatChoice(Character& C); - Here, we're going through loop for the player's options in battle. We're referencing the Character instance (or object?) that will be created in Main.cpp.
- void combat1(Character& C); - This will be the loop for our combat. We'll use this to go through the attacks, and check to see wether or not the Monster died. We're referencing the Character instance (or object?) here.
Simple enough, right? Now, the reason we're putting our reference to Monster (Monster& M) and our combatChoice() into Private, is because only Combat will need to use these. For an explanation of the reasons behind using Public, Private and Protected, click here.
Note bad for a beginning, so I get it right that you are aiming at somewhat experienced C++ programmers that just want to start out with game programming? Otherwise, you need to explain much more on the C++ stuff, most of this is not self-explanatory to a complete beginner.
One thing about the code itself, I know it is aimed at startes, but still, you should make more use of "private" and make the buisness logic more immanent for each class. Right now everything is a public field, thats an important thing even for beginners to get right - having probper encapsulation and not a can-access-all state, that is. Since you obviously target user with some knowledge of c++, this is even more important to get right.
Other than that, I don't see many things to complain, its not the best possible design (composition is a far better approach than inheritance for modeling game units IMHO), but its fairly well for starters. Keep up, hope you can take something out of this criticism!