Skip to content

Commit

Permalink
Merge pull request #275 from Anmol-Sharma21/snake_game
Browse files Browse the repository at this point in the history
snake_game
  • Loading branch information
Punit-Choudhary authored Apr 2, 2024
2 parents a851807 + 88554bf commit 703df23
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Projects/snake_game/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
venv/q
15 changes: 15 additions & 0 deletions Projects/snake_game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Snake Game using Python

## Description
This is a classic Snake game implemented in Python using the turtle library for GUI. The game allows the player to control a snake which moves around the screen, eating food pellets and growing longer. The objective is to avoid colliding with the walls or the snake's own body while trying to eat as much food as possible to score points.

## Features
- Classic Snake gameplay
- Simple and intuitive GUI using Turtle
- Score tracking
- Responsive controls

## Requirements
- Python 3.x
- Turtle library

1 change: 1 addition & 0 deletions Projects/snake_game/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
19 changes: 19 additions & 0 deletions Projects/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_len=0.5, stretch_wid=0.5)
self.color("blue")
self.speed("fastest")
self.refresh()

def refresh(self):
random_x = random.randint(-280, 280)
random_y = random.randint(-280, 280)
self.goto(random_x, random_y)
49 changes: 49 additions & 0 deletions Projects/snake_game/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import time
from turtle import Screen
from snake import Snake
from food import Food
from scoreboard import Scoreboard

# Set up the screen
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Snake Game")
screen.tracer(0)

snake = Snake()
food = Food()
scoreboard = Scoreboard()

screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")

game_is_on = True
while game_is_on:
screen.update()
time.sleep(0.1)
snake.move()

if snake.head.distance(food) < 15:
food.refresh()
snake.extend()
scoreboard.inc_score()

if (
snake.head.xcor() > 280
or snake.head.xcor() < -280
or snake.head.ycor() > 280
or snake.head.ycor() < -280
):
scoreboard.reset()
snake.reset()

for segment in snake.segments[1:]:
if snake.head.distance(segment) < 15:
scoreboard.reset()
snake.reset()

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

FONT = ("courier", 24, "normal")
ALIGNMENT = "center"


class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.score = 0
with open("data.txt") as data:
self.high_score = int(data.read())
self.color("white")
self.penup()
self.goto(0, 270)
self.write(
f"Score : {self.score} High Score : {self.high_score}",
align=ALIGNMENT,
font=FONT,
)
self.hideturtle()

def update_scorebaord(self):
self.clear()
self.write(
f"Score : {self.score} High Score : {self.high_score}",
align=ALIGNMENT,
font=FONT,
)

def reset(self):
if self.score > self.high_score:
self.high_score = self.score
with open("data.txt", mode="w") as data:
data.write(f"{self.high_score}")
self.score = 0
self.update_scorebaord()

def inc_score(self):
self.score += 1
self.clear()
self.update_scorebaord()
64 changes: 64 additions & 0 deletions Projects/snake_game/snake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from turtle import Turtle

STARTING_POSITION = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0


class Snake:

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

def create_snake(self):
for position in STARTING_POSITION:
self.add_seg(position)

def add_seg(self, position):
seg_new = Turtle("square")
seg_new.color("white")
seg_new.penup()
seg_new.goto(position)
self.segments.append(seg_new)

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_seg(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)
pass

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

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

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

0 comments on commit 703df23

Please sign in to comment.