├── assets ├── eat.wav ├── food.png ├── preview.png └── game over.wav ├── README.md ├── LICENSE ├── index.html └── mein.js /assets/eat.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hazrat-Ali9/Cold-Game/HEAD/assets/eat.wav -------------------------------------------------------------------------------- /assets/food.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hazrat-Ali9/Cold-Game/HEAD/assets/food.png -------------------------------------------------------------------------------- /assets/preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hazrat-Ali9/Cold-Game/HEAD/assets/preview.png -------------------------------------------------------------------------------- /assets/game over.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hazrat-Ali9/Cold-Game/HEAD/assets/game over.wav -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cold_snake 2 | This classic Snake game is developed using plain JavaScript, HTML5, and CSS. In "Cold Zone Snake," players control a snake that grows in length as it eats the food items. The goal is to navigate the snake using arrow keys, consume the food to increase your score, and avoid collisions with the walls or the snake's own body. The game includes sound effects for eating food and game over scenarios to enhance the interactive experience. 3 | 4 | Created by Hazrat Ali, this game demonstrates fundamental JavaScript programming concepts, including game loops, collision detection, and canvas rendering. 5 | 6 | ## game preview 7 | 8 | - [play game]() 9 | game img preview 10 | 11 | 12 | ## Author 13 | - [Hazrat Ali]() 14 | 15 | ## LICENSE 16 | this project under the mit [LICENSE](./LICENSE) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Hazrat Ali 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Snake Game 8 | 36 | 37 | 38 | 39 |
40 | 48 |
49 |
50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /mein.js: -------------------------------------------------------------------------------- 1 | // Calling canvas 2 | const canvas = document.getElementById('gameCanvas'); 3 | const scoreDisplay = document.getElementById('score'); 4 | // Main js 5 | // calling audio sound effect and food img 6 | const eatSound = new Audio('./assets/eat.wav'); 7 | const gameOverSound = new Audio('./assets/game over.wav'); 8 | 9 | /* 10 | const foodImg = new Image(); 11 | foodImg.src = './assets/food.png'; 12 | */ 13 | 14 | const ctx = canvas.getContext('2d'); 15 | 16 | // Size of snake and food 17 | const grid = 20; 18 | const canvasSize = 900; 19 | 20 | // Position of snake and food 21 | let snake = [{ 22 | x: 160, 23 | y: 160 24 | }]; 25 | 26 | let food = { 27 | x: 160, 28 | y: 160 29 | }; // Changed to an object 30 | 31 | // Snake speed 32 | let dx = grid; 33 | let dy = 0; 34 | 35 | // Ensure the snake doesn't reverse direction 36 | let changingDirection = false; 37 | 38 | // Game score 39 | let scoreCount = 0; 40 | scoreDisplay.innerHTML = `score: ${scoreCount}`; 41 | 42 | // Control the game progression 43 | function gameLoop() { 44 | if (changingDirection) return; 45 | changingDirection = true; 46 | moveSnake(); 47 | if (isCollision()) { 48 | gameOverSound.play() 49 | alert(`Game Over! Your score: ${scoreCount}`); 50 | document.location.reload(); 51 | return; 52 | } 53 | // Clear the canvas 54 | clearCanvas(); 55 | drawFood(); 56 | drawSnake(); 57 | 58 | 59 | // Adjust the timeout for game speed 60 | // setTimeout(gameLoop, 100); 61 | 62 | // next gear of game speed, the game level will be hard when you hit 10 score 63 | if (scoreCount < 10) { 64 | setTimeout(gameLoop, 100); 65 | } else{6 66 | setTimeout(gameLoop, 60); 67 | } 68 | } 69 | 70 | // Move the snake function 71 | function moveSnake() { 72 | // Position of the snake's head 73 | const head = { 74 | x: snake[0].x + dx, 75 | y: snake[0].y + dy 76 | }; 77 | // Add new snake head 78 | snake.unshift(head); 79 | 80 | if (head.x === food.x && head.y === food.y) { 81 | // Increase score 82 | scoreCount++; 83 | scoreDisplay.innerHTML = `score: ${scoreCount}`; 84 | // sound effect 85 | eatSound.play() 86 | // Place new food 87 | placeFood(); 88 | } else { 89 | // Remove the tail if not eating food 90 | snake.pop(); 91 | } 92 | changingDirection = false; 93 | } 94 | 95 | // Draw new snake function 96 | function drawSnake() { 97 | // Snake color 98 | ctx.fillStyle = 'white'; 99 | snake.forEach(segment => ctx.fillRect(segment.x, segment.y, grid, grid)); 100 | } 101 | 102 | // Draw new food function 103 | function drawFood() { 104 | // Food color 105 | ctx.fillStyle = 'red'; 106 | ctx.fillRect(food.x, food.y, grid, grid); 107 | } 108 | 109 | // Clear canvas function 110 | function clearCanvas() { 111 | ctx.clearRect(0, 0, canvasSize, canvasSize); 112 | } 113 | 114 | // Detect collision with walls or itself 115 | function isCollision() { 116 | const head = snake[0]; 117 | // Check if the head hits the walls 118 | if (head.x < 0 || head.x >= canvasSize || head.y < 0 || head.y >= canvasSize) { 119 | return true; 120 | } 121 | // Check if the head collides with itself 122 | for (let i = 1; i < snake.length; i++) { 123 | if (head.x === snake[i].x && head.y === snake[i].y) return true; 124 | } 125 | return false; 126 | } 127 | 128 | // Direction control with keyboard 129 | document.addEventListener('keydown', e => { 130 | if (e.key === 'ArrowUp' && dy === 0) { 131 | dx = 0; 132 | dy = -grid; 133 | } 134 | if (e.key === 'ArrowDown' && dy === 0) { 135 | dx = 0; 136 | dy = grid; 137 | } 138 | if (e.key === 'ArrowLeft' && dx === 0) { 139 | dx = -grid; 140 | dy = 0; 141 | } 142 | if (e.key === 'ArrowRight' && dx === 0) { 143 | dx = grid; 144 | dy = 0; 145 | } 146 | }); 147 | 148 | // Place food at a random position 149 | function placeFood() { 150 | food.x = Math.floor(Math.random() * canvasSize / grid) * grid; 151 | food.y = Math.floor(Math.random() * canvasSize / grid) * grid; 152 | } 153 | 154 | // Start the game loop 155 | document.addEventListener('keypress', () => { 156 | gameLoop(); 157 | }) 158 | --------------------------------------------------------------------------------