Advertisement
Josif_tepe

Untitled

Jun 1st, 2023
1,178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const canvas = document.getElementById("game")
  2. const context = canvas.getContext('2d');
  3.  
  4. class Snake {
  5.     constructor(x, y) {
  6.         this.x = x
  7.         this.y = y
  8.     }
  9. }
  10. const tileCount = 20
  11. const tileSize = canvas.clientWidth / tileCount - 2
  12. let headX = 10
  13. let headY = 10
  14. let appleX = 3
  15. let appleY = 3
  16. let xVelocity = 0
  17. let yVelocity = 0
  18. let tailLength = 2
  19. let score = 0
  20. const snakeParts = []
  21. function clearScreen() {
  22.     context.fillStyle = "black"
  23.     context.fillRect(0, 0, canvas.clientWidth, canvas.clientHeight)
  24. }
  25. function drawGame() {
  26.     changeSnakePosition()
  27.  
  28.     if(isGameOver()) {
  29.         return
  30.     }
  31.     clearScreen()
  32.     drawSnake()
  33.     drawApple()
  34.     collisionDetection()
  35.     drawScore()
  36.     setTimeout(drawGame, 1000 / 7)
  37. }
  38. function drawSnake() {
  39.     context.fillStyle = "green"
  40.     for(let i = 0; i < snakeParts.length; i++) {
  41.         let partOfSnake = snakeParts[i]
  42.         context.fillRect(partOfSnake.x * tileCount, partOfSnake.y * tileCount, tileSize, tileSize)
  43.     }
  44.     snakeParts.push(new Snake(headX, headY))
  45.     if(snakeParts.length > tailLength) {
  46.         snakeParts.shift()
  47.     }
  48.     context.fillStyle = "orange"
  49.     context.fillRect(headX * tileCount, headY * tileCount, tileSize, tileSize)
  50. }
  51. function drawScore() {
  52.     context.fillStyle = "white"
  53.     context.font = "16px arial"
  54.     context.fillText("Score " + score, canvas.clientWidth - 60, 20)
  55. }
  56. // drawSnake()
  57.  
  58. function changeSnakePosition() {
  59.     headX += xVelocity
  60.     headY += yVelocity
  61.  
  62.    
  63.  
  64. }
  65. function drawApple() {
  66.     context.fillStyle = "red"
  67.     context.fillRect(appleX * tileCount, appleY * tileCount, tileSize, tileSize)
  68. }
  69. function collisionDetection() {
  70.     if(appleX === headX && appleY === headY) {
  71.        appleX = Math.floor(Math.random() * tileCount)
  72.        appleY = Math.floor(Math.random() * tileCount)
  73.        tailLength++
  74.        score++
  75.     }
  76. }
  77. function isGameOver() {
  78.     let over = false
  79.     if(xVelocity === 0 && yVelocity === 0) {
  80.         return false
  81.     }
  82.     if(headX < 0) {
  83.         over = true
  84.     }
  85.     else if(headX === tileCount) {
  86.         over = true
  87.     }
  88.     else if(headY < 0) {
  89.         over = true
  90.     }
  91.     else if(headY === tileCount) {
  92.         over = true
  93.     }
  94.    
  95.     for(let i = 0; i < snakeParts.length; i++) {
  96.         let partOfSnake = snakeParts[i]
  97.         if(headX === partOfSnake.x && headY === partOfSnake.y) {
  98.             over = true
  99.             break
  100.         }
  101.     }
  102.     if(over == true) {
  103.         context.fillStyle = "white"
  104.         context.font = "50px arial"
  105.         context.fillText("GAME OVER!", canvas.clientWidth / 7, canvas.clientHeight / 2)
  106.     }
  107.     return over
  108. }
  109. document.addEventListener('keydown', keyPressed)
  110.  
  111. function keyPressed() {
  112.     if(event.keyCode === 38) { // W
  113.         if(yVelocity === 1) {
  114.             return
  115.         }
  116.  
  117.         xVelocity = 0
  118.         yVelocity = -1
  119.     }
  120.     if(event.keyCode === 40) { //
  121.  
  122.         if(yVelocity === -1) {
  123.             return
  124.         }
  125.  
  126.         xVelocity = 0
  127.         yVelocity = 1
  128.     }
  129.     if(event.keyCode === 37) { // A
  130.         if(xVelocity === 1) {
  131.             return
  132.         }
  133.  
  134.         xVelocity = -1
  135.         yVelocity = 0
  136.     }
  137.     if(event.keyCode === 39) { // D
  138.         if(xVelocity === -1) {
  139.             return
  140.         }
  141.  
  142.         xVelocity = 1
  143.         yVelocity = 0
  144.     }
  145.  
  146. }
  147. drawGame()
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement