-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from Anmol-Sharma21/snake_game
snake_game
- Loading branch information
Showing
7 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
venv/q |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |