Dragon Slayer RPG - jasontemelo/dragon-slayer-rpg GitHub Wiki

Dragon Slayer RPG Technical Document

Dragon Slayer RPG is a text-based role-playing game where the player can go to different locations, purchase items, fight monsters, and collect resources like gold and experience points (XP). The main objective is to become strong enough to defeat the dragon to win the game. Dragon Slayer RPG is built using HTML, CSS, and JavaScript. I created this game as part of FreeCodeCamp’s JavaScript Course, based on their Dragon Repeller certification project. Link to live version of game (Netlify): https://rpg-dragon-slayer.netlify.app/

# HTML Structure

The HTML file defines the structure of the game, including the game area, stats, controls, and various sections to display information dynamically.

html-structure

Elements

  • Game Container (#game): Main container for the game.
  • Stats (#stats): Displays the player's XP, health, and gold.
  • Controls (#controls): Buttons for navigating different actions/locations.
  • Monster Stats (#monsterStats): Displays the monster's name and health when fighting.
  • Text Display (#text): Shows the game narrative and updates based on player actions.

CSS Styling

The CSS file (styles.css) defines the visual appearance of the game, including colors, fonts, and layout.

css-styling

Styling Highlights

  • Body: Dark background color to create a contrasting game area.
  • Game Container (#game): Centered container with white background.
  • Controls and Stats: Bordered sections to visually separate them from the game narrative (textbox).
  • Buttons: Styled with a gradient background and border to make them visually appealing and interactive.

JavaScript Functionality

The JavaScript file (script.js) contains the game logic, including event handlers for buttons, game state management, and updates to the game display.

Game Variables

The let keyword is used to assign the variables in Dragon Slayer RPG because the values are dynamically reassigned as the game goes on.

js-game-variables

Element Selectors

js-element-selectors

Game Data

Weapons

js-weapons

Monsters

js-monsters

Locations

Arrays that contain elements specific for each location.

js-locations-1-2-3-4

js-locations-5-6-7-8

Functions

Navigation and Updates

Function for navigating between locations (town square, shop, cave, dragon).

js-update-location

Shop Functions

Functions for buying health in the shop.

js-buy-health

Function for buying weapons in the shop.

js-buy-weapon

Function for selling weapons in the shop; available only if player has bought the most powerful weapon.

js-sell-weapon

Combat Functions

Function for initializing combat sequences with monsters.

js-fight-functions

Functions for calculating if player attack hits monster and the amount of damage dealt based on currently equipped weapon.

js-attack-functions

Function for calculating monster’s attack damage based on their level.

js-monster-attack-value

Function for determining if monster is hit and damaged by player attack.

js-monster-hit

Function for evading a monster attack when choosing the “Dodge” button.

js-dodge

Function for calculating gold and XP rewards from defeating a monster.

js-defeat-monster

Functions that updated player location when winning or losing a fight.

js-lose-function

If the player reaches zero Health, the game ends with a “You die” text message and the only buttons available are bound to the restart function.

js-win-function

If the player defeats the dragon, the game ends with the message "You defeat the dragon! YOU WIN THE GAME”. All the buttons available will then be bound to the restart function.

Utility Functions

Function for resetting the game to their default values.

js-restart-function

Functions for initializing the secret (easter egg) event where the player can either win gold or lose health.

The easter egg activates when you select one of the three “Return to town” buttons after defeating a monster. You will get to choose between two numbers, each one giving you either (a) 20 gold or (b) -10 health points. If you have exactly 10 health and receive the 10 damage from the easter egg, death condition applies and you lose. Otherwise, the game continues as usual.

js-easter-egg-function