Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. import { colors } from './colors';
  2.  
  3. class SnakeBoard {
  4. constructor(width = 20, height = 20, cellSize = 20, color = colors.BLACK ) {
  5. this.width = width;
  6. this.height = height;
  7. this.cellSize = cellSize;
  8. this.color = color;
  9. }
  10. draw() {
  11. const canvas = document.getElementById("can");
  12.  
  13. if (canvas.getContext) {
  14. let ctx = canvas.getContext("2d");
  15. ctx.canvas.width = this.width * this.cellSize;
  16. ctx.canvas.height = this.height * this.cellSize;
  17. ctx.fillStyle = this.color;
  18. ctx.fillRect(0, 0, this.width * this.cellSize, this.height * this.cellSize);
  19. }
  20. }
  21.  
  22. }
  23.  
  24. class Snake {
  25. constructor(speed = 2, posX = 10, posY = 10, color = colors.YELLOW) {
  26. this.speed = speed;
  27. this.posX = posX;
  28. this.posY = posY;
  29. this.arrayLength = new Array({
  30. posX,
  31. posY
  32. });
  33. this.color = color;
  34. this.currentDirection = null;
  35. }
  36. getSpeed() {
  37. return this.speed;
  38. }
  39. getPosX() {
  40. return this.posX;
  41. }
  42. getPosY() {
  43. return this.posY;
  44. }
  45. getArrayLength() {
  46. return this.arrayLength;
  47. }
  48. setArrayLength(arr) {
  49. this.arrayLength = new Array(...arr);
  50. }
  51. getColor() {
  52. return this.color;
  53. }
  54. setColor(color) {
  55. this.color = color;
  56. }
  57. getCurrentDirection() {
  58. return this.currentDirection;
  59. }
  60. setCurrentDirection(direction) {
  61. this.currentDirection = direction;
  62. }
  63. increaseSpeed() {
  64. this.speed++;
  65. }
  66. }
  67.  
  68. const myNewSnakeBoard = new SnakeBoard();
  69. myNewSnakeBoard.draw();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement