Super Mario

> Recreation of Super Mario bros using the SDL libraries.
> My Hand-in for Games Engine Creation Assignment



Learn More

View On Github

Project Overview

Super Mario was my Level 4 Games Engine Creation project for semester 2. Our task was to try and create the original Mario Bros(Wikipedia) using the SDL libraries. However I wanted to push myself so I set myself out to create Super Mario which would also achieve me the top marks for additional features.

Super Mario has much more complex features than Mario Bros so this allowed me to achieve 100% on this assignment. This project was my first complex project where I had to create classes and methods to handle textures sounds and basic rendering for the game. Throughout the project I learnt alot about how a basic games engine could be created and how certain functions are repeatedly used like drawing a sprite for example. Below are some of the main features of the game I believe are worth noting as these features got me the additional marks.

Code Snippets / Screenshots

Camera Movement

Since Super Mario has a moving camera this meant I had to render everything in respect to where the camera. After implementing a camera parameter to my Texture2D.Draw() and did the calculations to render object in the correct place everything came together quite easily until the camera would only follow mario when playing multiplayer. To solve this I took the center points of both players and put the camera in the middle point and then stopped the players from walking off the screen. Below are some tweets I made demonstrating the camera.

...
void GameScreenLevel1::CameraMovementLogic()
{
	//Two Players
	if (mPlayers.size() == 2)
	{
		if (mPlayers.size() == 2)
		{
			if (mPlayers[0]->GetX() > mPlayers[1]->GetX())
			{
				mCamera->x = (mPlayers[1]->GetX() + (mPlayers[1]->GetHitbox()->w / 2) 
					+ ((mPlayers[0]->GetX() - mPlayers[1]->GetX())) / 2) - (mCamera->w / 2);
			}
			else
			{
				mCamera->x = (mPlayers[0]->GetX() + (mPlayers[0]->GetHitbox()->w / 2) 
					+ ((mPlayers[1]->GetX() - mPlayers[0]->GetX())) / 2) - (mCamera->w / 2);
			}
		}
	}
	//Single Player
	else
	{
		mCamera->x = mPlayers[0]->GetX() - (SCREEN_WIDTH / 2);
	}

	//Stop camera going past world boundaries
	if (mCamera->x < 0)
	{
		mCamera->x = 0;
	}
	if (mCamera->y < 0)
	{
		mCamera->y = 0;
	}
	if (mCamera->x + mCamera->w > mMapTileWidth* DEFAULTTILEWIDTH* RENDERSCALE)
	{
		mCamera->x = (mMapTileWidth * DEFAULTTILEWIDTH * RENDERSCALE) - mCamera->w;
	}
	if (mCamera->y < mCamera->h)
	{
		mCamera->y = 0;
	}
}					

Map Loading

Each level is stored within a .txt file which is loaded in when the level is being loaded and all the objects are being placed into the correct places. Having a txt loaded allowed me to debug my game very quickly as I could move a object I wished to debug closer to my character, also if I had more time It would be easier to add a tile based map editor which would save in txt format.
Also having a txt file allowed me to see the map within the txt file and made it much quicker for me to recreate some of the original mario levels without having to set the exact positions or each object manually. Also using a txt file allowed me to create smaller methods to correct certain objects to make them look correct in game. An example of this is the pipes. The pipes position is symbolised as a 'p' in the text file and the texture of the pipe is corrected dependant on adjacent tiles, as shown in the image on the right.

Bricks & Power-Ups

In Super Mario there are powerups which have different effects on Mario, I implemented a mushroom and the fire flower however due to the lack of time I had with the project I was unable to implement fire balls however the characters sprites change and when hit he will return to big Mario. Because of these powerups I needed a way to spawn them into the game and allow the player to collect them so just like Super Mario I created bricks. These bricks work how they do in super mario where small Mario cannot break bricks but can open '?' blocks and big mario can break bricks.
As Shown in the tweet on the left there is support for multiplayer if a player is stood ontop of a brick. Also in the video you can see mario picking up power Ups. To show a player is collecting a powerup i created a function which follows the 0.5 Cos(x) + 0.5 graph to create a smooth fade as opposed to a flash. Below shows the function in code:

...
void Player::FadeLogic()
{
	if (mFading)
	{
		float result = ((0.5 * cos(mFadeDegrees * 3.14159265 / 180.0)) + 0.5) * 255;
		mFadeDegrees += 0.5;

		if (mFadeTime > 500 && mFadeDegrees >= 360)
		{
			mFading = false;
			mFadeTime = 0;
			mInvunerable = false;
		}
		else
		{
			SDL_SetTextureAlphaMod(mTexture->GetTexture(), result);
			mFadeTime++;
		}

		if (mFadeDegrees >= 360.0)
		{
			mFadeDegrees = 0.0;
		}
	}
}
						

I am happy to go into further detail for this or any projects on my portfolio if needs be.

View On Github