Sunday, 15 May 2016

C++ SFML 2.3.2 Mario (remake) with full Source Code made available

As an end (2nd)semester project, I have developed just a tiny remake of Classical Mario Game.



  • Compiler used : MS VISUAL STUDIO 2013
  • Library used for graphics : SFML 2.3.2


This model yet does not have a lot of variety in terms of enemies and mario's super powers but it does have a large number of enemies and a considerably high difficulty level. Mario must score a total score of 25000 in order to win(since there's only one level for now)


  • Most importantly, one must understand how to install SFML 2.3.2. First I had downloaded this version of SFML  
http://www.sfml-dev.org/download/sfml/2.3.2/

  • Next, I had to set the visual studio 2013 (Ultimate) configurations to x64, and declare location of this SFML as under:

  • And added all the required .lib files shown under:



Here's a brief description of how I have implemented Mario and other game functionality:
    
  • A virtual base class:


class Base  //  Virtual Base class for player and enemies

{

public:  //  Common data members for all things that will move

float dx, dy, currentFrame;  // Displacement(dx & dy) and frame variables 

FloatRect rect; // Rectangle that will hold objects and move in the map

Sprite sprite; // Object sprite

int power; 

bool life, onGround; // OnGround checks if landed

Player_direction direction; // enum to get direction

RectangleShape power_indicator; //  Power Indicator for Enemies and Mario


public:
virtual void update(float time) = 0; //  This is the virtual update function
virtual void Collision(int num) = 0; //  Virtual Collision function 
virtual void operator --(int) = 0; // To reduce power
};



  • Also the coins, player and all the enemies inherit from this class publically: 
Obviously the Base class methods need to be over rided  in order to instantiate objects of coins, player and the enemies. 


  • If you have seen the video, you might be wondering how the bullets have been generated. For this I had to take some help from "subConcious bias"
                                          https://www.youtube.com/watch?v=qB0Pr08X_vM 

Actually all of the bullets are stored in vector and updated & deleted using an iterator using ERASE REMOVE IDIOM. Bullet class is as following:

// Projectile.h

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <vector>

enum Player_direction{ stop, go_left, go_right }; // This for player's direction 

class projectile
{
public:
//  Data members
float velocity = 20;  // Bullet speed
Player_direction direction;  //  1. Left,   2. Right
sf::RectangleShape rect;  //  Bullets rectangle
bool hit; //  Check if enemy was hit

//  Methods
projectile();
void update();


};

// Projectile.cpp

#include "projectile.h"

projectile::projectile()

{
rect.setSize(sf::Vector2f(4, 2));
rect.setFillColor(sf::Color::Black);
rect.setPosition(0, 0);
hit = false;
direction = stop;
}

void projectile::update()

{
if (direction == 1)
rect.move(-velocity, 0);  // Right

if (direction == 2)

rect.move(velocity, 0);  //  Left
}


  • And what about the map. Well that is a tile map. I got that idea from here and the code that he provided:




The one in my game looks something like this


String TileMap1[H] = {
    "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr",
      "r                                                                                                                                                                                                                                                       b                                                                                                                                                                           b                                                                                                                                                                                               b                                                                                                                                                                                                                                                         r",
        "r                 w                                                                                     w                                                            bbbbbbbbbbb                                                                   bbbbbbbbbbb                                           w                               w                                                                                            b                                                     bbbb                                                                                                                                   b                             w                                              w                                                                                                                                                                             r",
          "r                                        w                                                                                                                           b         b                                                                   b         b                                                           k                                                                                                              b                                                  b    b                         t0                                       kkkkkk                                                         b                                                                                                   r           r   rrrrrrrrr  rrrrrrrrr     rrr rrrrrrrrr                                                                                                  r",
            "r                                                                                   kk                                                                               b         b                                                                   bbbbbbbbbbb                                                                                                                                               b                            b                                               b      b                        00                                                                                                     b                  k            w                  t0                          w                     r r       r r   r          r        r     r  r       r                                                                                                  r",
              "r                                                                                  k  k    k k                                                                       b         b                                                                                                                                                                                                                            b b                             b                                            b        b                       00                                                                                                    b                 k                                 00                                                r  r     r  r   r                         r  r       r                                                                                                  r",
                "r         w                                                                        c      kkkkk                          bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb                           kkk                            bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb b          b b                 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb          bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb                          bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb                k                bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb                     r   r   r   r   r       r                                                                                                                               r",
                  "r                                                                       r          k       k k                          b                                                                                                                                                                    b                                                        b                                                       b         b   b                                                                                                                            b                         b                                                          k                                                                                       r    r r    r   rrrrrrrrr  rrrrrrrrr                                                                                                                    r",
                    "r                                             kckck                    rr          k  k   kkkkk                        b                                                                                                                                                                      b                                                      b                                                        b          b b                                   kk      kk                                                                                b                         b                                                        k                                                                                         r     r     r   r       r  r        r     r  r       r                                                                                                  r",
                      "r                                                                     rrr           kk     k k                                                                                                                                                kkkkkk                                                            kkk             kkk                                                                                                                                kk                                                                                    bbbbbbbb         bbbbbbbbbb                                                      k                                                                                           r           r   r       r  r         r    r  r       r                                                                                                  r",
                        "r                    kckck                                           rrrr                                                                              kkkkkkkkkkkkkkkkckkkkkkkkkkkkkkkkk                                           kkkkkk                                                                                                                                                                                                                       kk  kk                                                                                                                                                                 k                                                                                             r           r   r       r  r          r   r  r       r         r                                                                                        r",
                          "r                                      t0                           rrrrr                                           b                                                                                                     kkkkkk                                                                 b                                                b                                                                                                                kk                                                         t0                                                                                                      k                                     kkkckkkckkkckkkckkk                                       r           r   r       r  r           r rrr rrrrrrrrr         r                                    t0                              t0                  r",
                            "r                                      00              t0          rrrrrr                                t0        b                                                                                             kkkkkk                                                                           b                                              b                            t0                              b                                                kk      kk                                                     00                                                                                                    k                                                                                                                                                                                                     00                              00                  r",
                              "r                                      00              00         rrrrrrr                                00       b                                                                                                                                                                                b                                            b                             00                              b                                                                                                               00                                                                                                  k                                                                                                                                                                                                       00                              00                  r",
                                "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP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PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP", };
                                      I can understand it looks crazy and I could have used a better option(Tiled, for instance) but I was able to understand this one by enabling the virtual space in scroll options of VS 2013.

                                      • Collisions were detected in a bit wise fashion again adapted from the last declared source (NO PLAGIARISM!) 
                                      // Player class

                                      void Collision(int num)
                                      {
                                      for (int i = rect.top / 16; i < (rect.top + rect.height) / 16; i++)
                                      for (int j = rect.left / 16; j<(rect.left + rect.width) / 16; j++)
                                      {
                                      if ((TileMap1[i][j] == 'P') || (TileMap1[i][j] == 'k') || (TileMap1[i][j] == '0') || (TileMap1[i][j] == 'r') || (TileMap1[i][j] == 't') || (TileMap1[i][j] == 'c') || (TileMap1[i][j] == 'b') || (TileMap2[i][j] == 'u'))
                                      {
                                      if (dy>0 && num == 1)
                                      {
                                      rect.top = i * 16.f - rect.height;
                                      dy = 0;
                                      onGround = true;
                                      }

                                      if (dy<0 && num == 1)
                                      {
                                      rect.top = i * 16.f + 16.f;
                                      dy = 0;
                                      if (TileMap1[i][j] == 'k' || TileMap1[i][j] == 'b')    //     Wall breaking here
                                      {
                                      power--; // Reduce and balance power
                                      if (power <= 0) { power = 0; life = false; }

                                      TileMap1[i][j] = ' ';
                                      wallbreak.play();
                                      }
                                      if (TileMap1[i][j] == 'c')   //    Pickables here
                                      {
                                      TileMap1[i][j] = ' ';  // Clear it any way

                                      if (rand() % 2 == 1 && reloadPlay)
                                      {
                                      Reload.play();
                                      reloadPlay = false;
                                      isGun = true;
                                      }
                                      else if (life) // Increase and balance power if it's not dead
                                      power += 10;
                                      }
                                      }
                                      if (dx>0 && num == 0)
                                      {
                                      rect.left = j * 16.f - rect.width;
                                      }

                                      if (dx<0 && num == 0)
                                      {
                                      rect.left = j * 16.f + 16.f;
                                      }
                                      }
                                      if (TileMap2[i][j] == 'u') // Check for coin collect
                                      {
                                      TileMap2[i][j] = ' ';
                                      coin_count++;
                                      coin_collected.play();
                                      }
                                      }
                                      }

                                      As you can see, I have not developed all of this game by my self, some fractions such as the base class and bullets have been adopted from some other sources.

                                      Oh yes! and don't forget to comment if you have anything to say regarding this blog and its contents.

                                      And finally here's the source folder that I had promised. Enjoy!

                                      https://drive.google.com/file/d/0B85VdlLZ1OW6YkhlMUZBVkhhWDA/view?usp=sharing