Skip to content

Added Python Snake Game with Turtle Graphics #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Snake-Game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Snake Game 🐍

A classic Snake Game built using Python. Control the snake, collect food, and try to achieve the highest score without colliding with the walls or yourself.

## Features
- Interactive gameplay.
- Score tracking.
- Simple and user-friendly interface.

## Installation
1. Clone this repository:
```bash
git clone <your-repository-url>
2. Navigate the project folder
```bash
cd snake-game
3. Run the game
```bash
python snake_game.py

# How to Play
1. Use Arrow Keys to control the direction of the snake:
1. Up: Move up.
2. Down: Move down.
3. Left: Move left.
4. Right: Move right.
2. Collect food to increase your score.
3. Avoid colliding with:
1. Walls
2. The snake’s body.
4. Goal: Achieve the highest score possible!

# Technologies Used
Python

60 changes: 60 additions & 0 deletions Snake-Game/Snake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from turtle import Turtle, Screen
STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
RIGHT = 0
LEFT = 180


class Snake:
def __init__(self):
self.segments = []
self.x = 0
self.create_snake()
self.head = self.segments[0]

def create_snake(self):
for position in STARTING_POSITIONS:
self.add_segment(position)
def add_segment(self, position):
new_segment = Turtle("square")
new_segment.color("white")
new_segment.penup()
new_segment.goto(position)
self.segments.append(new_segment)
def reset(self):
for seg in self.segments:
seg.goto(1000, 1000)
self.segments.clear()
self.create_snake()
self.head = self.segments[0]
def extend(self):
self.add_segment(self.segments[-1].position())
def move(self):

for seg_num in range(len(self.segments)-1, 0,-1):
new_x = self.segments[seg_num-1].xcor()
new_y = self.segments[seg_num - 1].ycor()
self.segments[seg_num].goto(new_x, new_y)

self.head.forward(MOVE_DISTANCE)

def up(self):
if self.head.heading() != DOWN:
self.head.setheading(UP)

def down(self):
if self.head.heading() != UP:
self.head.setheading(DOWN)

def left(self):
if self.head.heading() != RIGHT:
self.head.setheading(LEFT)

def right(self):
if self.head.heading() != LEFT:
self.head.setheading(RIGHT)



Binary file added Snake-Game/__pycache__/Snake.cpython-312.pyc
Binary file not shown.
Binary file added Snake-Game/__pycache__/food.cpython-312.pyc
Binary file not shown.
Binary file added Snake-Game/__pycache__/scoreboard.cpython-312.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions Snake-Game/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from scoreboard import Score
score = Score()
HighScore = score.high_score
1 change: 1 addition & 0 deletions Snake-Game/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5
19 changes: 19 additions & 0 deletions Snake-Game/food.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from turtle import Turtle
import random


class Food(Turtle):

def __init__(self):
super().__init__()
self.shape("circle")
self.penup()
self.shapesize(stretch_wid=0.5, stretch_len=0.5)
self.color("blue")
self.speed("fastest")
self.refresh()

def refresh(self):
random_x = random.randrange(-270,270)
random_y = random.randrange(-270, 270)
self.goto(random_x, random_y)
53 changes: 53 additions & 0 deletions Snake-Game/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from turtle import Turtle, Screen
import time
from food import Food
from Snake import Snake
from scoreboard import Score
screen = Screen()
screen.setup(width=600, height=600)
screen.title("My Snake Game")
screen.bgcolor("black")
screen.tracer(0)
snake = Snake()
food = Food()
score = Score()
screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.right, "Right")
screen.onkey(snake.left, "Left")

is_game_on = True
while is_game_on:
screen.update()
time.sleep(0.1)
snake.move()
# collision with food
if snake.head.distance(food) < 15:
food.refresh()
snake.extend()
score.increase_score()
# collision with wall
if snake.head.xcor() < -280 or snake.head.xcor() > 280 or snake.head.ycor() < -280 or snake.head.ycor() > 280:
score.reset()
data = str(score.high_score)
with open("data.txt", mode="w") as file:
file.write(data)
snake.reset()
# collision with wall
for segment in snake.segments[1:]:
if snake.head.distance(segment) < 10:
score.reset()
data = str(score.high_score)
with open("data.txt", mode="w") as file:
file.write(data)
snake.reset()








screen.exitonclick()
31 changes: 31 additions & 0 deletions Snake-Game/scoreboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from turtle import Turtle

class Score(Turtle):


def __init__(self):
self.score = 0
with open("data.txt") as file:
self.HighScore = int(file.read())
self.high_score = self.HighScore
super().__init__()
self.color("white")
self.penup()
self.goto(0, 265)
self.hideturtle()
self.update_score()
def update_score(self):
self.clear()
self.write(f"Score: {self.score} High Score:{self.high_score}", align='center', font=('Arial', 24, 'normal'))
def reset(self):
if self.score > self.high_score:
self.high_score = self.score
self.score = 0
self.update_score()
def increase_score(self):
self.score += 1
self.update_score()

# def game_over(self):
# self.goto(0, 0)
# self.write("GAME OVER", align='center', font=('Arial', 24, 'normal'))
Loading