2048 Game - v1.0.0

npm version npm downloads

Introduction

This is a simple logic and rendering module for the game 2048, it is mainly intended for users of discord.js etc. The module uses @hitomihiumi/lazy-canvas to render the image.

  1. Install the module by using npm i @hitomihiumi/2048-game
  2. Enjoy!

Initialize class to create a new game.

const { Game } = require('@hitomihiumi/2048-game');

const game = new Game()
game.setUser('1111111111')

Used just for identify game, not used in module

game.setSize(6)

Set the size of the game board. The default value is 4.

await game.startGame(); 

Returns object with 'status' and canvas buffer. You can use this buffer in AttachmentBuilder (for discord.js), or save it to file by 'fs'.

await game.move('up'); // Move the tiles up
await game.move('down'); // Move the tiles down
await game.move('left'); // Move the tiles left
await game.move('right'); // Move the tiles right

Returns object with 'status' and 'canvas' properties. 'status' is a string, 'canvas' is a canvas buffer. 'status' can be 'start', 'win', 'gameover', 'nochange' or 'move'.

Status Meaning
start Signals the start of the game
win Signals a player's victory
gameover The end of the game, use 'startGame()' to start a new one
nochange Status in which it is impossible to move to the selected side
move A move in the chosen direction has been made
game.reset();

Resets the game, use 'startGame()' to start a new one.

game.step();

Returns the number of steps taken in the game.

game.score();

Returns the current score in the game.

game.userId();

Returns the user id.

game.exportData();

Returns the game data in JSON format.

game.importData(data);

Imports the game data in JSON format.

You can customize the game by changing the following properties:

game.setColors({
'0': '#191919',
'2': '#A151DD',
'4': '#A045BF',
'8': '#9F39A1',
'16': '#9E2D83',
'32': '#9D2165',
'64': '#9C1547',
'128': '#A71D42',
'256': '#BD3854',
'512': '#D35366',
'1024': '#E96E78',
'2048': '#FF8A8A',
});

Set the colors of the tiles. '0' is the background color.

const { Font } = require('@hitomihiumi/lazy-canvas');

game.setFont(
new Font()
.setFamily('Koulen')
.setWeight('regular')
.setPath('./fonts/Koulen-Regular.ttf')
);

Set the font of the text. Use @hitomihiumi/lazy-canvas to create a new font.

game.setOffsets({
'2': { x: 0, y: 0 },
'4': { x: 0, y: 0 },
'8': { x: 0, y: 0 },
'16': { x: 0, y: 0 },
'32': { x: 0, y: 0 },
'64': { x: 0, y: 0 },
'128': { x: 0, y: 0 },
'256': { x: 0, y: 0 },
'512': { x: 0, y: 0 },
'1024': { x: 0, y: 0 },
'2048': { x: 0, y: 0 },
});

Sets the x- and y-axis offsets for the numbers individually. If you need to set the offset for all digits at once, use setGlobalOffset().

game.setGlobalOffset({ x: 0, y: 0 });

Sets the x- and y-axis offsets for all numbers at once.

game.setLineThickness(5);

Sets the thickness of the cell number stroke line. The default value is 4.

game.setFilled(true);

Sets whether the cell number is filled or not. The default value is false.