Guest User

Untitled

a guest
Jun 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. class Game {
  2.  
  3. constructor(){
  4. //creates canvas and ctx
  5. this.xPosition = 0
  6. this.yPosition = 0
  7. this.canvas = document.getElementById("canvas");
  8. this.canvas.style.background = "black";
  9. this.ctx = canvas.getContext("2d");
  10. this.drawSquare(0,0);
  11. this.snake = new Snake();
  12. }
  13.  
  14. drawSquare(x,y){
  15. this.snake.head = [this.xPosition,this.yPosition];
  16. this.ctx.clearRect(this.xPosition, this.yPosition, 30, 30); //pop tail
  17. this.xPosition += x
  18. this.yPosition += y;
  19. if (this.xPosition < 0){
  20. this.xPosition = 600;
  21. }
  22. else if (this.xPosition > 600){
  23. this.xPosition = 0;
  24. }
  25. if (this.yPosition < 0){
  26. this.yPosition = 600;
  27. }
  28. else if (this.yPosition > 600){
  29. this.yPosition = 0;
  30. }
  31. this.ctx.fillStyle = "#FF0000";
  32. this.ctx.fillRect(this.xPosition,this.yPosition,30,30);
  33. }
  34. }
  35.  
  36. class Snake {
  37. constructor(){
  38. this.head = [];
  39. this.tail = [];
  40. this.length = 1;
  41. }
  42. }
Add Comment
Please, Sign In to add comment