Featured

Developing a Game: SamuraiDuel

Now-a-days, I am developing a game called SamuraiDuel. It is going to be a 2D game and being built on OpenGL, SDL2, Box2D and CEGUI all in the language, C++.

SamuraiDuel Title
SamuraiDuel Title

Before starting to develop the game, I first developed the base of a very low level Game Engine which I should call a GL Engine, GL SDK or something like that, in order to work with OpenGL with ease. Of course, I called it BAC GL Engine 2 (I tried to make one before but it has some big flaws and so, I put ‘2’ this time after the name).

I am not satisfied with that name. If you had one in your mind, please do comment below.

SamuraiDuel is going to be a side scroller fast paced pixelated game. Of course, it will take time to get completed and once it is completed, this is the place where I will confirm this.

There is the screenshot of the game! I hope that you like that…

SamuraiDuelDevelopment4

All pixel arts are made by me and I think it looks fair.

That’s all that I have to say! If you want some features in it or just has something to tell, comment below and tell that to me! It will be really appreciable.

UPDATE: The project is closed and is no more under development due to lack of funding and maybe, motivation. Sorry guys! But who knows… I might complete it someday!

Advertisements

C++ Programming Series: Arrays

Previous two posts are (or just looks like) useless and really, are not interesting. But in this post, you are going to get the reward! Yes, a reward! You are going to learn my favourite thing, favourite in the way that it makes life with programming very easy! We call it: Arrays.

Before proceeding, remember that this post is a very long post because it is very significant.

What is an array? Array is a list of something; group of variables of a specific type. They are group together in a linear way just like ‘3 after and 2 before the 1’.

#include <iostream>
using namespace std;
int main()
{
  //This is an array of integers which holds 5 elements(variable of type 'int')
  int array[5] = { 5, 10, 15, 20, 25 };

  //Printing all elements of the array using index.
  //Possible indexes are from 0 to 4, not 5 as it don't starts from 1.
  //Anything other than that, would give you error or just garbage values!
  for(int i = 0; i < 5; i++)
  {
    cout << array[i] << endl;
  }
}

It looks very simple and I hope that you may have got it here! Array of variables can be for any type! It is not necessary to put 5 before 10 and so on, you can put any number for any index. Here is the format of an array declaration:

-type- -name- [-number_of_elements-]; 

It’s too simple to make one. To initialize every element of an array, there is another method. Just like we output the element of an array via the index, we can alter that number as well!

int array[5]; //Only declaration

for(int i = 1; i <= 5; i++)
  array[i-1] = i*5; //i-1 as index to get a proper range of 0-4 instead of 1-5
//array is now = 5, 10, 15, 20, 25

array[0] = 10; //previous value = 5
array[1] *= 100; //previous value = 10
array[2] -= 5; //previous value = 15
array[3] = 0; //previous value = 20
array[4] %= 5; //previous value = 25

It might be possible that the for-statement and %= may look unclear to you. If that is the case, please check out my previous posts on C++ Programming Series to remember them.

Arrays are just a list of elements with the number of elements or range of indexes unalterable or constant. We can’t change the total number of elements in an array! But there are arrays which can have alterable number of elements; this term is associated with STL or C++ Standard Template Library which we will talk about later.

In the first block of this post, it is not necessary to type in the total number of variables inside the square brackets as the compiler can check that by the number of elements in between the curly brackets. It is not possible to initialize only one or two variables in between the curly brackets, we must initialize all variables in such a case.

Now, we are going to discuss a small case of arrays to pinpoint two things: its importance and the sizeof() operator’s importance (yeah, that useless sizeof()!)

Suppose that we are going to make a game with many enemies. Lets say that they are just 100 in count and we need to have a variable that holds the health for each!

Lets us try to do it without arrays. In the main(or int main()) method:

int enemy1_HP = 10;
int enemy2_HP = 10;
int enemy3_HP = 10;
//and so on to...
int enemy99_HP = 10;
int enemy100_HP = 10;

//ISN'T THAT SOMETHING LENGTHY WHICH DOES
//NOTHING BUT KEEP TRACK OF ALL ENEMIES HP?

OK? Is that OK? If your answer is ‘No’, then you really don’t like to work hard. But if it is ‘Yes’, then I should have told you that you also need a variable for the position, speed, attack, bullets, coins… and more. Just with the mentioned ones, there will be about 500 lines of code! That is one of the reasons why we should not measure a programmer’s capability by the number of lines in his/her program.

Apart from the problem of having a lot of lines being wasted on variable declaration, there is an even bigger problem! Just tell me, how are you supposed to combine your 100 enemies’ variables with the looping? I mean that there is nothing like index in a large list of singleton variables. It will just get more and more complex!

In short, it will take enough time to complete the game without arrays that we will begin to hate programming. Of course, it is NOT OK at all!

Here is with arrays:

//Replacing 100 lines with just 3!
int enemyHP[100];
for(int i = 0; i < 100; i++)
  enemyHP[i] = 10;

//I am in love with arrays!

The index can serve as an ID number for the enemy and you can always get and set the health, position, speed, attack, bullets, coins, etc, of the appropriate enemy just by having the ID or index of that enemy.

Now, at the end of the program, for some reason, you want all enemies’ health(or HP) to be equal to zero. Lets say that the program consists of more than 10000 lines of code and 100 files. At the specific line of a specific file, there is the declaration of the enemyHP array. You just forgot the total number of elements in that array. Finding the declaration in the files might take some time. Your IDE have the feature to find the declaration but you don’t know how to access that feature. So, what? We can always use sizeof() to find out the amount of memory allocated by the variable or even array.

In this case, it will give the value 400 for enemyHP array. For each integer, it will have 4 bytes and so, for 100, 400 bytes. So, we need to divide sizeof(enemyHP) by sizeof(int) to get the total amount of elements for that array:

for(int i = 0; i < sizeof(enemyHP)/sizeof(int), i++)
  enemyHP[i] = 0;

And there you go! It is ended and with every post, our database are getting expanded. I can’t just let you go with nothing so, here are some exercises. Strictly speaking, if you are learning programming for the first time with this series of post, just stick with the first 3 exercises. Don’t bother to see the exercises beyond the third one because they might confuse you. The first 3 exercises exercises assumes that you must have some knowledge about console printing, arrays, user input, conditional statements. Just try these 3 exercises. If you get failed to get proper results, try to fix the problems in your code. But after that, if you still get failed, just check out the solution given with the exercise.

To learn how to program stuff is just like to learn how to paint stuff. It just gets better with you, trying to do it, face it and then, experiment with it, again and again, and not just by reading and watching tutorials.

C++ Programming Series: Using Conditional Statements (Part 5)

Argghhh! When will the topic of conditional statement gets over? Well, we have to be patient while learning something. I hope that you might have done the home task of the previous post. Just keep trying to code things up and try to implement small ideas with your skill, you will see yourself learning and improving!

Below is some similar looking code that we discussed in the previous post:

#include <iostream>
using namespace std;

int main()
{
  //Guess the number game!
  while(value != 7)
  {
    cout << "Enter the value: ";

    int value;
    cin >>; value;
  }

  cout << endl << "Horray! You guessed the number!";

  return 0;
}

Yeah! This time we programmed a very simple game! Guess the number! Sounds cool… Right? No! Why not? At least, we can call this our first GAME! At least, we started learning game development!

Try to run that code! Is it running fine? No! What the compiler said? Oh! It looks very hard… Somewhere, in the output log, it might be written that:

'value': undeclared identifier

Now, what it means? it means that the variable, value is not known to the compiler. Just see the code. The condition in the while-statement comes before the value being declared. Solution can be this:

#include <iostream>
using namespace std;

int main()
{
  //Guess the number game!
  cout << "Enter the value: ";

  int value;
  cin >> value;

  while(value != 7)
  {
    cout << "Enter the value: ";

    //Say No to: int value; Reason: already declared above.
    cin >> value;
  }

  cout << endl << "Horray! You guessed the number!";
  return 0;
}

Even better and a very neat solution is this:

int main()
{
 //Guess the number game!
 int value;
 do {
 cout << "Enter the value: ";
 cin >> value;
 } while(value != 7); //Semicolon is needed here!

 cout << endl << "Horray! You guessed the number!";

 return 0;
}

The code in the do block is first executed even if the condition in the while is not true. Then, it will only be repeated if the condition remains true. Very simple! Also, in this way, the condition will not get the garbage value of uninitialized but declared value as the do code give some value to value i.e initializes value.

Looping plays a key role in game development. Such a loop in which program updates logic and screen of a game is called game loop. Game loop is the heart of any game. A game loop may looks like:

bool quit = false;
while(!quit) //same as "quit == false"
{
  //> > > Polling Events
  //if(quit event happens)
  //  quit = true;
  //else if(key == arrow x)
  //  move player to direction = x;

  //> > > Updating Game Logic
  //if(enemy hits player)
  // player hp--;
  //else if(player hits enemy)
  // enemy hp--;

  //> > > Updating Screen
  //clear screen
  //render enemy
  //render player
  //update screen
}

In future, we will get to this point. But for now, this is a minimal view of a general video game.

Let us talk about another home task. This time it is not a joke! Make a program that takes interview of a user having such questions that have only two answers i.e Yes or No. On the basis of Yes or No of a question, change the route of questioning. In this way, make a hierarchy or tree kind of thing. If possible, comment that code here so, that I can review it! Also, try some experiments with the code in this post and understand it. To learn something like programming, it is necessary to be interested in it…