Skip to content

snake_game #275

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 7 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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 @@
15
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reset high score to 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)
47 changes: 47 additions & 0 deletions Projects/snake_game/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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()
38 changes: 38 additions & 0 deletions Projects/snake_game/scoreboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 game_over(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unused code

# self.goto(0,0)
# self.write(f"GAME OVER", align=ALIGNMENT, font=FONT)

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

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
Loading