C++ Game Development [Tutorial] p1
by Toymaker
0. Introductions
1. To Get Started
2. To Make It Run
3. For It's Engine
4. To Get It Safe
5. The GoodByes
6. Get The Quiz!
7. Common Questions
0.(Introductions) In light of MPGH's own Game Engine, (Jetaengine) being well on it's way, I decided to make this simple tutorial for those of you interested in getting started with learning to create and secure games.
1.(To Get Started) All you need to get started is the standard Bloodshev Dev-C++ compiler and this tutorial. It is your creativity that will bring the most success.
2.(To Make It Run) I've compiled and working and simple model of a text based game for you to use and/or add onto sometime, to get you started. You'll notice a key concept to game creation is 'running while playing,' as my while loop gives away. This is a simple model of you fighting the computer. It does not even do random damage or have a working SP system. I suggest you try it out to make sure you understand it. It is all too basic of code for me to waste time explaining to you.
Code:
#include <iostream>
using namespace std;
int uhp = 100;
int usp = 10;
int mp = 100;
int ms = 10;
int turn = 1;
int atk;
int dmg;
int main() {
system("title GameName");
while ( uhp > 0 ) {
if ( turn == 1 ) {
turn--;
cout<<"It is YOUR turn hero.n";
cout<<"Your HP is: "<<uhp<<"n";
cout<<"Your SP is: "<<usp<<"n";
cout<<"Decide Your Next Moven";
cout<<"1. Punch(10 DMG 0 SP)n";
cout<<"2. Psyki(20 DMG 5 SP)n";
cin>>atk;
if ( atk == 1 ) {
dmg = 10;
cout<<"You do: "<<dmg<<" DMGn";
mp = mp - dmg;
cout<<"Monstr Life: "<<mp<<"n";
}
else {
dmg = 20;
cout<<"You do: "<<dmg<<" DMGn";
mp = mp - dmg;
usp = usp - 5;
cout<<"Monstr Life: "<<mp<<"n";
}
if ( turn == 0 ) {
turn++;
cout<<"Computer AIs Turnn";
cout<<"Computer Hits Youn";
cout<<"Suffer: 10 Damagen";
uhp = uhp - 10;
cout<<"New HP: "<<uhp<<"n";
}
if ( mp < 5 ) {
cout<<"You Just Wonn";
system("pause");
exit(0);
}
}
}
cout<<"Thanks For Playinn";
system("pause");
}
3.(For It's Engine) The idea of a game engine is to make an option for users to input unique information. It can be simple or complex. In my example, using the game above, you will see how to use a script file to determine the players name as if one small feature of a game engine. If you add the cpp code below to your game and create a few locations for it to output the variable, it will say the players uniquely set name every where.
Code:
Add To .Cpp
string playername;
ifstream b_file ( "script.txt" );
b_file>> playername;
cout<<"Welcome Player: " <<playername<< "n";
Add To .Txt
McAnthony
4.(To Get It Safe) This is the big one. You don't want users leaking or cracking your game! I will break this section up into two parts.
A. You don't want users to be able to leak a free version of your game. with that noted, you created a demo version in which you Trial protect so users can only load it so many times. You use the same concept as the engine code above but you want to hide it and change it a bit. In this example your players can only play 5 times before paying:
Code:
Add To .Cpp
int loudcount;
ifstream b_file ( "loadcounter.txt" );
b_file>> loadcount;
if ( loadcount > 5 ) {
cout<<"You've played to much!n";
exit(0);
}
Add To .Txt
1
B. You will notice, perhaps in cracking basic password programs, a common hacker can just debug your program and change one jump of memory to a no operation in memory and avoid it. In layman terms, in the "if you can use or not" code they can cancel out the 'or not' part so it always loads. A basic way to beat this, noting the Offset and Bytes are in decimal and not hexadecimal during compile time, is to check the memory's integrity. If the ASM code is like this:
4D0: 1111 | CMP YOURPASS TO CORRECTPASS
4D4: 2222 | IFWRONG GOTO WRONG LOCATION
4D8: 3333 | IFRIGHT GOTO RIGHT LOCATION
A hacker could simply 9090 (NoOperation) the 4D4 IFWRONG line so all that's left is the 4D8 IFRIGHT line. So you can use ReadProcessMemory to check for non changed values.
Code:
int address = 0x4D4;
int value;
DWORD pid;
GetWindowThreadProcessId(hwnd,&pid);
HANDLE phandle = OpenProcess(PROCESS_ALL_ACCESS,0,pid);
ReadProcessMemory(phandle,(LPVOID)address,&value,2,0);
cout << value;
getch();
if ( value != 2222 ) {
cout<<" You must be hackingn";
system("pause");
}
5. End Of Tutorial It's basic and quick but leaves room for a lot of learning direction and I hope you learn from it or are inspired with ideas. I must additionally credit using google to find certain code snippets and method ideas.
6.(GET THE QUIZ). As far as this quiz goes perhaps you can pass it! You simply need to achieve the following goals (Notice: If you do this quiz you can submit it here for additional group learning and thank you):
1. Add a working SP system to the TBG, (Text Based Game), so your player can only use the stronger attack two times total.
2. Increase the security of your safety measures through encryption or obscuring the information and or path locations
7.(Common Questions) Q. How do I do a save game feature? A. You simply ask the user to load a game or start a new one and if they want to load a game it reads from a .txt file that, if they saved the game saved, wrote information to. You could do an auto-save on escape and it'd store the HP/SP of you and the monster in a .txt file for auto-loading next execution, for testing and example.