It's a 2d platformer implemented using pure JavaScript and DOM API (without Canvas and special libs).
At the moment this game has the following functionality:
- The player's physics
- Obstacle and glitch areas handling
- Collision handlers for coins and player
- Game over and finish event handlers
- The observer pattern for the global state control and game scrolling
npm run serve
starts DevServer (http://localhost:3000/
)npm run build
builds the app for production to thedist
folder
Note: it is necessary to specify publicPath: '/'
in webpack.config.js
for the DevServer to work correctly. If you are planning to upload the project to a subdirectory of your site then change this property to publicPath: './'
when building for production.
- A character sprite is inside a rectangular block element. The player has two sprites for left and right movement animation. To add sprites into the player's
div
block I used the tag<img src="">
. This tag allows you to load images immediately and then move sprites relative to the player'sdiv
block. Using thebackground: url(/img_path)
property has a drawback associated with the time lag for switching sprites (the functionurl()
takes time to load the image). Therefore the sprite may freeze when switching - The coordinates of the game elements are calculated relative to the block
game
without taking into account the scrolling of the block (calculations only relative to the visible area) setInterval()
insidesrc/components/game/gameManager.js
after clicking on the menu button calls the animation of characters and passes the state of motion keys (right, left and up)- Each call of the
hero.animate()
method checks if a character is on the ground - A character has three points at the base to check the ground and seven points to check obstacles in the default case (different characters may have their own set of obstacle points). If a character is on the ground then alignment to the ground level occurs
- Before moving a character checks for an obstacle at the next coordinate according to its step (vertical or horizontal). If an obstacle is in the path of movement then a character shortens its step in order to get as close as possible to an obstacle (through the loop
while
) - The folder
src/shared
contains general functions for working with dom elements, coordinates, alignment, obstacle check and ground check. The animation of movements is implemented at the level of character logic (seesrc/components/heroes
). The foldersrc/components/game
contains the game control interface. All supporting functions forsrc/components/game/gameManager.js
are moved to a separate modulegameFuncs.js
in the same folder. The dynamic game menu is implemented insrc/components/menu.js
- The game components communicate with each other using a single state (see
src/store
). The components may change the global state without notifying each other (in such case the second parameter of thestore.emit(action, withNotify)
method takes on the valuefalse
) - The background of the game is generated from the array (see
src/settings/worldSettings.js
). Each element (string symbol) of the array converted to thediv
element with the corresponding class - The block with the game has a fixed size of 1200 x 600 pixels. Also this block has scrolling along both axes
https://freepngimg.com/png/83123-computer-sprite-figure-character-fictional-2d-animation