MCPcopy Create free account
hub / github.com/Vishal-raj-1/Awesome-JavaScript-Projects / draw

Function draw

assets/js/AwesomeSnakeGame.js:61–124  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

59}
60
61function draw() {
62 ctx.clearRect(0,0,myCanvas.width,myCanvas.height)
63 if (mode==0) {
64 ctx.fillStyle = 'white';
65 ctx.textBaseline = 'middle';
66 ctx.textAlign = 'center';
67 ctx.font = 'normal bold 30px serif';
68 ctx.fillText('pleas press Enter to start', myCanvas.width/2, myCanvas.height/2);
69 }
70else if (mode==1) {
71 var snakeHeadX=snake[0].x,
72 snakeHeadY=snake[0].y;
73 for (var i =0 ; i < snake.length; i++) {
74 var snakeX=snake[i].x,
75 snakeY=snake[i].y;
76 drawSnake(snakeX,snakeY);
77 }
78 drawFood(food.x,food.y);
79///////////
80if (dir=="right"){
81 snakeHeadX++;
82}
83else if(dir=="left"){
84 snakeHeadX--;
85}
86else if(dir=="up"){
87 snakeHeadY--;
88}
89else if(dir=="down"){
90 snakeHeadY++;
91}
92if (snakeHeadX==food.x&&snakeHeadY==food.y) {
93 eat.play();
94score++
95 food ={
96 x:Math.round(Math.random()*(myCanvas.width/snakeWidth-1)),
97 y:Math.round(Math.random()*(myCanvas.height/snakeHeight-1))
98
99 }
100}else {
101 snake.pop();
102}
103document.getElementById("score-count").innerText = score;
104for (var i = 0; i < snake.length; i++) {
105 if(snakeHeadX==snake[i].x&& snakeHeadY==snake[i].y)
106 {
107 f=true;
108 console.log(f);
109gameOver();
110 }
111 else{
112 f=false;
113
114 }
115}
116let newHead={
117 x:snakeHeadX,
118 y:snakeHeadY

Callers

nothing calls this directly

Calls 3

drawSnakeFunction · 0.85
drawFoodFunction · 0.85
gameOverFunction · 0.70

Tested by

no test coverage detected