Super Mario Project - GroupProjectMiniProject/Super-mario-project GitHub Wiki

#UML

UML

Description

For our programming mini-project we decided to make a game in Java language which imitates the super Mario gameplay. For the game, we wanted to include classical super Mario gameplay elements, such as enemies, coins and power ups. To get the gameplay to work, we had to implement codes for collision between Mario and other game objects, as well as implement the code for the health, coin (point) and power up system. Finally we wanted to implement a little animation for Mario and his enemies and some GUI to keep track of the score.

1. General

Goal of the game

Goal of the game is to get to the flag which is in the map in order to finish the game successfully. The player loses if it collides with the enemies 2 times without any power ups. Player can cancel/restart the game by pressing ESC key.

Software/Code Language

For this project we used Eclipse compiler of Java language code. For sharing and tracking our project work we used repository on GitHub. Both Eclipse and GitHub client works comfortably together and allowed us to effectively develop the code.

Java Libraries

Choice of libraries was based on the type of the product which was a game. We chosen Slick2D library which allowed us to use special interface and methods designed for 2D games.

Code Structure/Interface

The Slick2D library allows the user to work within interface/structure called StateBasedGame which creates base functions init, update and render in a class that extends from this interface and allows a user to navigate through different states(classes) by their unique IDs.

2. Game Elements

Game Mechanics

Game consist of the basic physics Gravity and Acceleration. These forces are multiplying the speed values of the game elements and the limits cap the speeds on certain values. Various boolean-type variables are ensuring that different states of movement(jumping, falling, colliding etc.) are in the progress so that a character behave as intended.

Enemies

Enemies are created by creating an arrayList and thereafter adding new enemies to that list.

public static ArrayList<Enemies> enemyList = new ArrayList<Enemies>();

We made a class for Enemies for simplicity. In that we give our Enemies different parameters so they individually can change their coordinates, collisions, etc.

public Enemies(int xpos, int ypos, float spX, float spY, int ovrX, int blwX, Rectangle eOtS, Rectangle bndboxR, Rectangle bndboxP, Rectangle eInS, boolean falls, boolean bo, boolean to, boolean righ, boolean lef, boolean outCol, boolean inCol) {

Adding an enemy to the list is done as below.

enemyList.add(new Enemies(2*texSize, bottom-11*texSize, 0.0f, 0.0f, 50, 50, enemyOuterShape, BoundingBoxRed, BoundingBoxPurple, enemyInnerShape, false, false, false, false, false, false, false));

The different parameters can seem overwhelming at first but are all essential for making the enemies work dynamically.

xpos and ypos: coordinates of enemies spX and spY: setting the x and y direction speed. ovrX and blwX: To make an enemy patrol back and forth. eOts and eInS: Collision for enemy to the world (platforms). bndboxR and bndboxP: Collision for enemy and Mario. bo, to, righ, lef: For checking collision if it is bottom, top, right or left.

Collisions in general

Collisions in this game were designed for rectangle shapes which are only shapes used in this game. The collision detection reads through the shape’s corners (4) and find out which of those are intersecting with an object(platform, enemy, coin). At the same time it looks for neighbor corners if any are also intersecting with the object. This mechanism results in unique cases from all possible combinations of intersections of corners of a shape with the object. Set of conditions then perform commands that set the resulting position for the shape.

Enemy-Mario collision

The idea for this collision is that when Mario jumps on the enemy, the enemy will die, but if Mario runs into the enemy, then mario will also lose a HP.

So two bounding boxes where created

dragonas.BoundingBoxPurple = new Rectangle(dragonas.x+3, dragonas.y+12, enemyDragonTex.getWidth()-4, enemyDragonTex.getHeight()/2); dragonas.BoundingBoxRed = new Rectangle(dragonas.x+3, dragonas.y-2, enemyDragonTex.getWidth()-4, enemyDragonTex.getHeight()-22);

The purple one is located at the enemy’s lower body and the red one at its upper body. The idea is to check if mario’s feet are touching the red bounding box or his whole body is touching the purple bounding box.

Mario-collision

Red Collision (the box at the top of enemy)

First an point array is created getting the 2 bottom coordinates around Mario. Example of one is shown below. All the coordinates are set to 1 pixel less to give a better feel of hitting the enemy and avoiding that players feel cheated that they died even though they didn’t touch the enemy. //Bot Right corner arr2[3].setX(marioShape.getMaxX()-1); arr2[3].setY(marioShape.getMaxY()-1);

Next we check if the red bounding box (d) are containing any of the points. If it does, it should remove the current enemy from the list.

if (d.contains(arr2[2].getMinX(), arr2[2].getMinY())||d.contains(arr2[3].getMinX(), arr2[3].getMinY())){ enemyList.remove(i);

Purple Collision (the bottom box of enemy)

Continuing, the if-statement we check to see if marioShape is intersecting with the purple bounding box (e). The result is removing an HP from mario if he is not powered up and or removing his power up if he got one. In both cases enemy will be removed from the list.

else if (marioShape.intersects(e)){ if (!poweredUp) { HP--; killMario = true; } poweredUp = false; enemyList.remove(i); }

Coins

In this game the coins do not have any reasonable purpose related to successfully finishing the game. They are however common elements in mario games and bring additional motivation to the players. The code for creating a coin is similar to creating mario character, enemies and power ups.

Power ups

In the game there is one type of power up that is adding additional power to mario which makes him invulnerable to next health dropping event (hitting enemy etc.). There are 2 power ups in the game and they look like small white-red mushrooms. Similar to enemies the mushrooms are removed if they intersect with Mario, turns him green and makes the boolean variable poweredup equal to true. Next time Mario intersects with an enemy poweredup becomes false and Mario is no longer invulnerable.
Animation:

In the beginning of the GameState class we initialized animation for movement for left and right for the enemy and mario. We set the “speed” of animation to 200 for all the animations. Its shown below:

enemyWalkLeft = new Animation (enemySheetLeft, 200); enemyWalkRight = new Animation (enemySheetRight, 200);

For the Mario we made it so if the left key is pressed the animation for Mario moving left should be displayed and vice versa. If neither the left or right key is pressed none of the animation is displayed and the Mario stands still.

System.out.println("Keys: left/right = " + keyLeft + "/" + keyRight); if (keyLeft) { MarioWalkLeft.start(); MarioWalkLeft.update(delta); } else if (keyRight) { MarioWalkRight.start(); MarioWalkRight.update(delta); } else { MarioWalkLeft.restart(); MarioWalkLeft.stop(); MarioWalkRight.restart(); MarioWalkRight.stop(); }

The enemies are a bit different, since they do not respond directly to user input. The enemies have a horizontal movement (x-coordinate) and if they hit a platform or the edge of the screen they move in the opposite direction. This means that their X directional speed is positive when they are walking to the right and negative when they are walking to the left. Based on this we have made it so that if their X speed is below 0 (negative) the animation for enemyWalkLeft is displayed and if it is anything less (positive or 0) the animation for enemyWalkRight is displayed.

for(Enemies dragon: enemyList ) // selecting list items with for loop method 1 { if (dragon.speedX < 0) enemyWalkLeft.draw(dragon.x, dragon.y); else enemyWalkRight.draw(dragon.x, dragon.y); }

GUI

The game has 3 main states (Menu, Game, Ending). In these states the player can navigate respectively. Enter key enters the game from Menu state. ESC key ends the game in Game state. Enter key reenters Menu state while in Ending state.

In the Game state the players is provided 2 important information. Health remaining and number of coins he/she gathered.

User Controls(character)

A player is controlling the character by arrow keys and additional option of closing the game by ESC key.

⚠️ **GitHub.com Fallback** ⚠️