Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { colors } from './colors';
- class SnakeBoard {
- constructor(width = 20, height = 20, cellSize = 20, color = colors.BLACK ) {
- this.width = width;
- this.height = height;
- this.cellSize = cellSize;
- this.color = color;
- }
- draw() {
- const canvas = document.getElementById("can");
- if (canvas.getContext) {
- let ctx = canvas.getContext("2d");
- ctx.canvas.width = this.width * this.cellSize;
- ctx.canvas.height = this.height * this.cellSize;
- ctx.fillStyle = this.color;
- ctx.fillRect(0, 0, this.width * this.cellSize, this.height * this.cellSize);
- }
- }
- }
- class Snake {
- constructor(speed = 2, posX = 10, posY = 10, color = colors.YELLOW) {
- this.speed = speed;
- this.posX = posX;
- this.posY = posY;
- this.arrayLength = new Array({
- posX,
- posY
- });
- this.color = color;
- this.currentDirection = null;
- }
- getSpeed() {
- return this.speed;
- }
- getPosX() {
- return this.posX;
- }
- getPosY() {
- return this.posY;
- }
- getArrayLength() {
- return this.arrayLength;
- }
- setArrayLength(arr) {
- this.arrayLength = new Array(...arr);
- }
- getColor() {
- return this.color;
- }
- setColor(color) {
- this.color = color;
- }
- getCurrentDirection() {
- return this.currentDirection;
- }
- setCurrentDirection(direction) {
- this.currentDirection = direction;
- }
- increaseSpeed() {
- this.speed++;
- }
- }
- const myNewSnakeBoard = new SnakeBoard();
- myNewSnakeBoard.draw();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement