1.0.1 • Published 1 year ago

@acm-97/react-snake-game v1.0.1

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

@acm-97/react-snake-game

NPM version Build npm-typescriptLicense]github-license-url

This repo is based on the idea of the classic snake games, with the exception that this component accepts some options, which makes it more customizable, something I wanted for my project and I have not found in others.

You can clone it and step by step create your own NPM package and publish it.

It is simple React counter.

Live Demo

Installation

npm install @acm-97/react-snake-game

or

yarn add @acm-97/react-snake-game

Available props

PropsDescription
onGameOveryou can pass a function here and recive the number of points collected
onEatFooddo anything after eat
onMoveyou can pass a function here and recive the current positions of snake's head and food
stylesyou can pass in plain object styles here
statethis is like the initial state for the component

State props

PropsDefaultDescription
snakeCoordinates[[0,0],[2,0]]initial position for the snake
foodCoordinatesrandominitial position for the food
directionRIGHTinitial direction for the snake to move
speed200speed of the snake
isRunningfalsetells the component if the snake can move or not
generateObstaclesfalseto add another level of difficulty you can add obstacles along the way
numObstacles4number of obstacles to add

Styles props

PropsDescription
gameAreaplain object styles for the game area container
snakeplain object styles for the snake element
foodplain object styles for the food element
obstaclesplain object styles for obstacle element

Styles can also be accessed through the .class selector

  • for gameArea: .game-area
  • for snake: .snake-body and .snake-body-part-"specific part(number) of the snake"
  • for food: .food-wrapper and .food
  • for obstacles: .obstacle

Some usage examples

For a basic usage add GameArea to your component:

import GameArea from '@acm-97/react-snake-game';

const Component = () => (
  <GameArea state={{ isRunning }} />
);

If you want some obstacles in the way add the option generateObstacles to your component's state:

import GameArea from '@acm-97/react-snake-game';

const Component = () => (
  <GameArea state={{ isRunning, generateObstacles: true }}
);

If you want to display the score for every game, add onGameOver to your component:

import React, { useState } from 'react';
import GameArea from '@acm-97/react-snake-game';

const Component = () => {
  const [score, setScore] = useState(0);

  const stopGame =(value: number) => {
      setScore(value);
  };

  return  (
    <div>
      <GameArea state={{ isRunning }} onGameOver={stopGame} />
      <div>
        SCORE: {score}
      </div>
    </div>
)};
1.0.1

1 year ago

1.0.0

1 year ago

0.7.17

1 year ago

0.7.16

1 year ago

0.7.15

1 year ago

0.7.14

1 year ago

0.7.13

1 year ago

0.7.12

1 year ago

0.7.11

1 year ago

0.7.10

1 year ago

0.7.9

1 year ago

0.7.8

1 year ago

0.7.7

1 year ago

0.7.6

1 year ago

0.7.5

1 year ago

0.7.4

1 year ago

0.7.3

1 year ago

0.7.2

1 year ago

0.7.1

1 year ago

0.7.0

1 year ago

0.6.6

1 year ago

0.6.5

1 year ago

0.6.4

1 year ago

0.6.3

1 year ago

0.6.2

1 year ago

0.6.1

1 year ago

0.6.0

1 year ago

0.5.6

1 year ago

0.5.5

1 year ago

0.5.4

1 year ago

0.5.3

1 year ago

0.5.2

1 year ago

0.5.1

1 year ago

0.5.0

1 year ago

0.4.3

1 year ago

0.4.2

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.3.9

1 year ago

0.3.8

1 year ago

0.3.7

1 year ago

0.3.6

1 year ago

0.3.5

1 year ago

0.3.4

1 year ago

0.3.3

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.2.3

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.0

1 year ago