Skip to content

Commit 35f5bc0

Browse files
committed
Addition of snake game using python
1 parent b176557 commit 35f5bc0

File tree

7 files changed

+157
-4
lines changed

7 files changed

+157
-4
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ More information on contributing and the general code of conduct for discussion
8383
| Image Watermarker | [Image Watermarker](https://github.com/DhanushNehru/Python-Scripts/tree/master/Image%20Watermarker) | Adds a watermark to an image. |
8484
| Image to ASCII | [Image to ASCII](https://github.com/DhanushNehru/Python-Scripts/tree/master/Image%20to%20ASCII) | Converts an image into ASCII art. |
8585
| Image to Gif | [Image to Gif](https://github.com/DhanushNehru/Python-Scripts/tree/master/Image%20to%20GIF) | Generate gif from images. |
86-
| Images to WebP Converter | [Images to WebP Converter] (https://github.com/DhanushNehru/Python-Scripts/tree/master/Images%20to%20WebP%20Converter) |Converts images to WebP vie cmd or GUI |
86+
| Images to WebP Converter | [Images to WebP Converter] (https://github.com/DhanushNehru/Python-Scripts/tree/master/Images%20to%20WebP%20Converter) | Converts images to WebP vie cmd or GUI |
8787
| Interactive Dictionary | [Interactive Dictionary](https://github.com/DhanushNehru/Python-Scripts/tree/master/Image%20InteractiveDictionary) | finding out meanings of words |
8888
| IP Geolocator | [IP Geolocator](https://github.com/DhanushNehru/Python-Scripts/tree/master/IP%20Geolocator) | Uses an IP address to geolocate a location on Earth. |
8989
| Jokes Generator | [Jokes generator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Jokes%20Generator) | Generates jokes. |
@@ -97,7 +97,7 @@ More information on contributing and the general code of conduct for discussion
9797
| Longitude & Latitude to conical coverter | [Longitude Latitude conical converter](master/Longitude%20Latitude%20conical%20converter) | Converts Longitude and Latitude to Lambert conformal conic projection. |
9898
| Mail Sender | [Mail Sender](https://github.com/DhanushNehru/Python-Scripts/tree/master/Mail%20Sender) | Sends an email. |
9999
| Merge Two Images | [Merge Two Images](https://github.com/DhanushNehru/Python-Scripts/tree/master/Merge%20Two%20Images) | Merges two images horizontally or vertically. |
100-
| Mood based youtube song generator | [Mood based youtube song generator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Mood%20based%20youtube%20song%20generator) | This Python script fetches a random song from YouTube based on your mood input and opens it in your default web browser. |
100+
| Mood based youtube song generator | [Mood based youtube song generator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Mood%20based%20youtube%20song%20generator) | This Python script fetches a random song from YouTube based on your mood input and opens it in your default web browser. |
101101
| Mouse mover | [Mouse mover](https://github.com/DhanushNehru/Python-Scripts/tree/master/Mouse%20Mover) | Moves your mouse every 15 seconds. |
102102
| Morse Code | [Mose Code](https://github.com/DhanushNehru/Python-Scripts/tree/master/Morse%20Code) | Encodes and decodes Morse code. |
103103
| No Screensaver | [No Screensaver](https://github.com/DhanushNehru/Python-Scripts/tree/master/No%20Screensaver) | Prevents screensaver from turning on. |
@@ -120,14 +120,15 @@ More information on contributing and the general code of conduct for discussion
120120
| QR Code with logo | [QR code with Logo](https://github.com/DhanushNehru/Python-Scripts/tree/master/QR%20with%20Logo) | QR Code Customization Feature |
121121
| Random Color Generator | [Random Color Generator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Random%20Color%20Generator) | A random color generator that will show you the color and values! |
122122
| Remove Background | [Remove Background](https://github.com/DhanushNehru/Python-Scripts/tree/master/Remove%20Background) | Removes the background of images. |
123-
| Road-Lane-Detection | [Road-Lane-Detection](https://github.com/NotIncorecc/Python-Scripts/tree/master/Road-Lane-Detection) | Detects the lanes of the road |
123+
| Road-Lane-Detection | [Road-Lane-Detection](https://github.com/NotIncorecc/Python-Scripts/tree/master/Road-Lane-Detection) | Detects the lanes of the road |
124124
| Rock Paper Scissor 1 | [Rock Paper Scissor 1](https://github.com/DhanushNehru/Python-Scripts/tree/master/Rock%20Paper%20Scissor%201) | A game of Rock Paper Scissors. |
125125
| Rock Paper Scissor 2 | [Rock Paper Scissor 2](https://github.com/DhanushNehru/Python-Scripts/tree/master/Rock%20Paper%20Scissor%202) | A new version game of Rock Paper Scissors. |
126126
| Run Then Notify | [Run Then Notify](https://github.com/DhanushNehru/Python-Scripts/tree/master/Run%20Then%20Notify) | Runs a slow command and emails you when it completes execution. |
127127
| Selfie with Python | [Selfie with Python](https://github.com/DhanushNehru/Python-Scripts/tree/master/Selfie%20with%20Python) | Take your selfie with python . |
128128
| Simple DDOS | [Simple DDOS](https://github.com/VanshajR/Python-Scripts/tree/master/Simple%20DDOS) | The code allows you to send multiple HTTP requests concurrently for a specified duration. |
129129
| Simple TCP Chat Server | [Simple TCP Chat Server](https://github.com/DhanushNehru/Python-Scripts/tree/master/TCP%20Chat%20Server) | Creates a local server on your LAN for receiving and sending messages! |
130130
| Smart Attendance System | [Smart Attendance System](https://github.com/DhanushNehru/Python-Scripts/tree/master/Smart%20Attendance%20System) | This OpenCV framework is for Smart Attendance by actively decoding a student's QR Code. |
131+
| Snake Game | [Snake Game](https://github.com/DhanushNehru/Python-Scripts/tree/master/Snake%20Game) | Classic snake game using python. |
131132
| Snake Water Gun | [Snake Water Gun](https://github.com/DhanushNehru/Python-Scripts/tree/master/Snake%20Water%20Gun) | A game similar to Rock Paper Scissors. |
132133
| Sorting | [Sorting](https://github.com/DhanushNehru/Python-Scripts/tree/master/Sorting) | Algorithm for bubble sorting. |
133134
| Star Pattern | [Star Pattern](https://github.com/DhanushNehru/Python-Scripts/tree/master/Star%20Pattern) | Creates a star pattern pyramid. |
@@ -156,7 +157,6 @@ More information on contributing and the general code of conduct for discussion
156157
| Youtube Downloader | [Youtube Downloader](https://github.com/DhanushNehru/Python-Scripts/tree/master/Youtube%20Downloader) | Downloads any video from [YouTube](https://youtube.com) in video or audio format! |
157158
| Youtube Playlist Info Scraper | [Youtube Playlist Info Scraper](https://github.com/DhanushNehru/Python-Scripts/tree/master/Youtube%20Playlist%20Info%20Scraper) | This python module retrieve information about a YouTube playlist in json format using playlist link. |
158159

159-
160160
## Gitpod
161161

162162
Use the cloud-free development environment where you can directly start coding.

Snake Game/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Snake-Game
2+
3+
![snake game demo](https://github.com/user-attachments/assets/a88cd856-a477-4f02-ac50-eb09a801cd8a)

Snake Game/data.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
18

Snake Game/food.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from turtle import Turtle
2+
import random
3+
4+
class Food(Turtle):
5+
def __init__(self):
6+
super().__init__()
7+
self.shape("circle")
8+
self.penup()
9+
self.shapesize(stretch_len= 0.5, stretch_wid= 0.5)
10+
self.color("blue")
11+
self.speed("fastest")
12+
self.refresh()
13+
14+
def refresh(self):
15+
random_x = random.randint(-280, 280)
16+
random_y = random.randint(-280, 280)
17+
self.goto(random_x, random_y)
18+

Snake Game/main.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from turtle import Screen
2+
3+
from snake import Snake
4+
from food import Food
5+
from scoreboard import Scoreboard
6+
import time
7+
8+
screen = Screen()
9+
screen.setup(width= 600, height= 600)
10+
screen.bgcolor("black")
11+
screen.title("Snake Game")
12+
screen.tracer(0)
13+
14+
snake = Snake()
15+
food = Food()
16+
scoreboard = Scoreboard()
17+
18+
screen.listen()
19+
screen.onkey(snake.up, "Up")
20+
screen.onkey(snake.down,"Down")
21+
screen.onkey(snake.left,"Left")
22+
screen.onkey(snake.right,"Right")
23+
24+
is_game_on = True
25+
while is_game_on:
26+
screen.update()
27+
time.sleep(0.15)
28+
snake.move()
29+
#detect collision with the food
30+
if snake.head.distance(food) < 15:
31+
food.refresh()
32+
snake.extend()
33+
scoreboard.increase_score()
34+
35+
#detect collision with the wall
36+
if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor() > 280 or snake.head.ycor() < -280:
37+
scoreboard.reset()
38+
snake.reset()
39+
40+
#detect collision with the tail
41+
for segment in snake.segments[1:]:
42+
if snake.head.distance(segment) < 10:
43+
scoreboard.reset()
44+
snake.reset()
45+
46+
screen.exitonclick()

Snake Game/scoreboard.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from turtle import Turtle
2+
ALIGNMENT = "center"
3+
FONT = ("Courier", 25, "normal")
4+
class Scoreboard(Turtle):
5+
def __init__(self):
6+
super().__init__()
7+
self.score = 0
8+
with open("data.txt") as data:
9+
self.highscore = int(data.read())
10+
self.color("white")
11+
self.penup()
12+
self.goto(0, 260)
13+
self.hideturtle()
14+
self.update_score()
15+
16+
def update_score(self):
17+
self.clear()
18+
self.write(f"Score: {self.score} HighScore: {self.highscore}", align= ALIGNMENT, font= FONT)
19+
20+
def reset(self):
21+
if self.score > self.highscore:
22+
self.highscore = self.score
23+
with open("data.txt", "w") as data:
24+
data.write(f"{self.highscore}")
25+
self.score = 0
26+
self.update_score()
27+
28+
def increase_score(self):
29+
self.score += 1
30+
self.update_score()
31+

Snake Game/snake.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from turtle import Turtle
2+
STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)]
3+
MOVE_DISTANCE = 20
4+
UP = 90
5+
DOWN = 270
6+
RIGHT = 0
7+
LEFT = 180
8+
9+
class Snake:
10+
def __init__(self):
11+
self.segments = []
12+
self.create_snake()
13+
self.head = self.segments[0]
14+
15+
def create_snake(self):
16+
for position in STARTING_POSITIONS:
17+
self.add_segment(position)
18+
19+
def reset(self):
20+
for seg in self.segments:
21+
seg.goto(1000, 1000)
22+
self.segments.clear()
23+
self.create_snake()
24+
self.head = self.segments[0]
25+
26+
def add_segment(self, position):
27+
new_segment = Turtle("square")
28+
new_segment.penup()
29+
new_segment.color("white")
30+
new_segment.goto(position)
31+
self.segments.append(new_segment)
32+
33+
def extend(self):
34+
self.add_segment(self.segments[-1].position())
35+
36+
def move(self):
37+
for seg_num in range(len(self.segments) - 1, 0, -1):
38+
new_x = self.segments[seg_num - 1].xcor()
39+
new_y = self.segments[seg_num - 1].ycor()
40+
self.segments[seg_num].goto(new_x, new_y)
41+
self.segments[0].forward(MOVE_DISTANCE)
42+
43+
def up(self):
44+
if self.head.heading() != DOWN:
45+
self.head.setheading(UP)
46+
def down(self):
47+
if self.head.heading() != UP:
48+
self.head.setheading(DOWN)
49+
def left(self):
50+
if self.head.heading() != RIGHT:
51+
self.head.setheading(LEFT)
52+
def right(self):
53+
if self.head.heading() != LEFT:
54+
self.head.setheading(RIGHT)

0 commit comments

Comments
 (0)