From c1d0bdea6bcd38493c2641e932add3aaf88e2345 Mon Sep 17 00:00:00 2001 From: 1911aditi <146438609+1911aditi@users.noreply.github.com> Date: Thu, 6 Jun 2024 17:27:08 +0530 Subject: [PATCH 1/5] Add files via upload --- Games/snake/README.md | 23 ++++++++ Games/snake/index.html | 131 +++++++++++++++++++++++++++++++++++++++++ Games/snake/script.js | 96 ++++++++++++++++++++++++++++++ Games/snake/style.css | 17 ++++++ 4 files changed, 267 insertions(+) create mode 100644 Games/snake/README.md create mode 100644 Games/snake/index.html create mode 100644 Games/snake/script.js create mode 100644 Games/snake/style.css diff --git a/Games/snake/README.md b/Games/snake/README.md new file mode 100644 index 0000000000..d691f36bc7 --- /dev/null +++ b/Games/snake/README.md @@ -0,0 +1,23 @@ +# Snake Game + +This is a simple Snake Game implemented in HTML CSS and JavaScript. + +## How to Play + +- Use the arrow keys to control the snake's direction. +- Eat the red food blocks to grow the snake and increase your score. +- Avoid colliding with the walls or the snake itself. + + +## Development + +To run the game locally: + +1. Clone this repository. +2. Open `index.html` in your browser. + +## Acknowledgements + +- The Snake Game logic is inspired by classic snake games. +- CSS styles for the game layout are defined in `styles.css`. + diff --git a/Games/snake/index.html b/Games/snake/index.html new file mode 100644 index 0000000000..f37b812d73 --- /dev/null +++ b/Games/snake/index.html @@ -0,0 +1,131 @@ + + + + + + Snake Game + + body { + margin: 0; + padding: 0; + display: flex; + justify-content: center; + align-items: center; + height: 100vh; + background-color: #f0f0f0; + } + + .game-container { + position: relative; + } + + #gameCanvas { + border: 1px solid #000; + } + + + +
+ +
+ + + diff --git a/Games/snake/script.js b/Games/snake/script.js new file mode 100644 index 0000000000..c459740d25 --- /dev/null +++ b/Games/snake/script.js @@ -0,0 +1,96 @@ +const canvas = document.getElementById("gameCanvas"); +const ctx = canvas.getContext("2d"); +const gridSize = 20; +let snake = [{x: 10, y: 10}]; +let food = {x: 15, y: 15}; +let dx = 0; +let dy = 0; +let score = 0; +let gameInterval; + +document.addEventListener("keydown", changeDirection); + +function changeDirection(event) { + const keyPressed = event.key; + if (keyPressed === "ArrowUp" && dy === 0) { + dx = 0; + dy = -1; + } else if (keyPressed === "ArrowDown" && dy === 0) { + dx = 0; + dy = 1; + } else if (keyPressed === "ArrowLeft" && dx === 0) { + dx = -1; + dy = 0; + } else if (keyPressed === "ArrowRight" && dx === 0) { + dx = 1; + dy = 0; + } +} + +function drawSnake() { + ctx.fillStyle = "#000"; + snake.forEach((segment) => { + ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize); + }); +} + +function drawFood() { + ctx.fillStyle = "#ff0000"; + ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize); +} + +function moveSnake() { + const head = {x: snake[0].x + dx, y: snake[0].y + dy}; + snake.unshift(head); + if (head.x === food.x && head.y === food.y) { + score++; + createFood(); + } else { + snake.pop(); + } +} + +function createFood() { + food.x = Math.floor(Math.random() * (canvas.width / gridSize)); + food.y = Math.floor(Math.random() * (canvas.height / gridSize)); +} + +function drawScore() { + ctx.fillStyle = "#000"; + ctx.font = "20px Arial"; + ctx.fillText("Score: " + score, 10, 25); +} + +function draw() { + ctx.clearRect(0, 0, canvas.width, canvas.height); + drawSnake(); + drawFood(); + drawScore(); + moveSnake(); + + if (checkCollision()) { + gameOver(); + return; + } +} + +function checkCollision() { + const head = snake[0]; + return ( + head.x < 0 || + head.x >= canvas.width / gridSize || + head.y < 0 || + head.y >= canvas.height / gridSize || + snake.slice(1).some(segment => segment.x === head.x && segment.y === head.y) + ); +} + +function gameOver() { + clearInterval(gameInterval); + ctx.clearRect(0, 0, canvas.width, canvas.height); + ctx.fillStyle = "#000"; + ctx.font = "30px Arial"; + ctx.fillText("Game Over!", canvas.width / 2 - 100, canvas.height / 2); +} + +gameInterval = setInterval(draw, 100); diff --git a/Games/snake/style.css b/Games/snake/style.css new file mode 100644 index 0000000000..2228ef5329 --- /dev/null +++ b/Games/snake/style.css @@ -0,0 +1,17 @@ +body { + margin: 0; + padding: 0; + display: flex; + justify-content: center; + align-items: center; + height: 100vh; + background-color: #f0f0f0; +} + +.game-container { + position: relative; +} + +#gameCanvas { + border: 1px solid #000; +} From df381ad2c8d9a10bff35d266bcf8c8f732321d84 Mon Sep 17 00:00:00 2001 From: 1911aditi <146438609+1911aditi@users.noreply.github.com> Date: Thu, 6 Jun 2024 18:35:58 +0530 Subject: [PATCH 2/5] Add files via upload --- Games/snake/index.html | 124 +---------------------------------------- 1 file changed, 3 insertions(+), 121 deletions(-) diff --git a/Games/snake/index.html b/Games/snake/index.html index f37b812d73..fa8181fdc1 100644 --- a/Games/snake/index.html +++ b/Games/snake/index.html @@ -4,128 +4,10 @@ Snake Game - - body { - margin: 0; - padding: 0; - display: flex; - justify-content: center; - align-items: center; - height: 100vh; - background-color: #f0f0f0; - } - - .game-container { - position: relative; - } - - #gameCanvas { - border: 1px solid #000; - } - + -
- -
- + + From cf71e07a9bd67d55607e9ffcd4b9b156fe05d031 Mon Sep 17 00:00:00 2001 From: 1911aditi <146438609+1911aditi@users.noreply.github.com> Date: Thu, 6 Jun 2024 18:37:49 +0530 Subject: [PATCH 3/5] Add files via upload --- assets/images/snake.png | Bin 0 -> 12164 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 assets/images/snake.png diff --git a/assets/images/snake.png b/assets/images/snake.png new file mode 100644 index 0000000000000000000000000000000000000000..98118e29900d0383961f7591b9db7b84437cdf1b GIT binary patch literal 12164 zcmeHNX;4$yx;-F>;s8peoghw)v>--AK!Jb*ie3n4GX;=A5g9_5WDX%JD2fVg;{`rOm zbxP*N-%tMpQ@-f7EZ~ZzPFYr_vV(e z=cZqtuQ`1s`L(T*YQipOZmLAEJ}>%4U+7D?#S)lwA@*9a?f%{InHdk zvun-DKl(8fD|56GM^GTFq{s@4Kyk2wkpB@DvD~hv=5j1-8vuO3dV711Zw96q{vZj0 z9iZhZL|-Z|Prlr#CeuU6oD2bg)d#Q;$%F0R3E=x4BqkQ9maPYXe*GgyzQE@h17F?x zr|Ar7zP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c7{ zUUP4ng5>FS$Y_k4%GyhkrW|l=z1oz zKw~QX;lqct%XJA9%)YK~^~u~~uxJO}+YbdYi;Q{5D_^#b=RR)&I2$WL)w{LO94a@# zwvzfQsSm}$%5jJPha(~X>uwo9f~6n=lFyv!cm>n?Q1CcHOT*eFHt5@$H-=KRD)xIk3jmP`0XLd4~ToQ@3(F3WB|rV=aHG8Yu2uR#ui4^C0M3mTcIt z;em@Ru-FD3K}ZBUJ?+pg`Hyz Date: Thu, 6 Jun 2024 18:47:03 +0530 Subject: [PATCH 4/5] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 91bad69b56..5a40785bb7 100644 --- a/README.md +++ b/README.md @@ -322,6 +322,7 @@ This repository also provides one such platforms where contributers come over an | [Find_the_ball](https://github.com/kunjgit/GameZone/tree/main/Games/Find_the_ball) | |[Color The Page](https://github.com/kunjgit/GameZone/tree/main/Games/Color_The_Page)| |[AquaSort_Game](https://github.com/kunjgit/GameZone/tree/main/Games/AquaSort_Game) | +| [Snake](https://github.com/1911aditi/GameZone/tree/cf71e07a9bd67d55607e9ffcd4b9b156fe05d031/Games/snake)                | |[Turn_on_the_light](https://github.com/kunjgit/GameZone/tree/main/Games/Turn_on_the_light) | | [Tic-Tac-Toe Game](https://github.com/kunjgit/GameZone/tree/main/Games/Tic-Tac-Toe) | | [Rapid_click_frenzy](https://github.com/kunjgit/GameZone/tree/main/Games/Rapid_click_frenzy) From 54285be409685371e1c342dbfe8215e9477e086d Mon Sep 17 00:00:00 2001 From: 1911aditi <146438609+1911aditi@users.noreply.github.com> Date: Tue, 11 Jun 2024 12:41:03 +0530 Subject: [PATCH 5/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5a40785bb7..8c50b8d7fd 100644 --- a/README.md +++ b/README.md @@ -322,7 +322,7 @@ This repository also provides one such platforms where contributers come over an | [Find_the_ball](https://github.com/kunjgit/GameZone/tree/main/Games/Find_the_ball) | |[Color The Page](https://github.com/kunjgit/GameZone/tree/main/Games/Color_The_Page)| |[AquaSort_Game](https://github.com/kunjgit/GameZone/tree/main/Games/AquaSort_Game) | -| [Snake](https://github.com/1911aditi/GameZone/tree/cf71e07a9bd67d55607e9ffcd4b9b156fe05d031/Games/snake)                | +| [Snake](https://github.com/kunjgit/GameZone/tree/main/Games/snake)                | |[Turn_on_the_light](https://github.com/kunjgit/GameZone/tree/main/Games/Turn_on_the_light) | | [Tic-Tac-Toe Game](https://github.com/kunjgit/GameZone/tree/main/Games/Tic-Tac-Toe) | | [Rapid_click_frenzy](https://github.com/kunjgit/GameZone/tree/main/Games/Rapid_click_frenzy)