Skip to content

Commit 703df23

Browse files
Merge pull request #275 from Anmol-Sharma21/snake_game
snake_game
2 parents a851807 + 88554bf commit 703df23

File tree

7 files changed

+192
-0
lines changed

7 files changed

+192
-0
lines changed

Projects/snake_game/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
venv/q

Projects/snake_game/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Snake Game using Python
2+
3+
## Description
4+
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.
5+
6+
## Features
7+
- Classic Snake gameplay
8+
- Simple and intuitive GUI using Turtle
9+
- Score tracking
10+
- Responsive controls
11+
12+
## Requirements
13+
- Python 3.x
14+
- Turtle library
15+

Projects/snake_game/data.txt

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

Projects/snake_game/food.py

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

Projects/snake_game/main.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import time
2+
from turtle import Screen
3+
from snake import Snake
4+
from food import Food
5+
from scoreboard import Scoreboard
6+
7+
# Set up the screen
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+
game_is_on = True
25+
while game_is_on:
26+
screen.update()
27+
time.sleep(0.1)
28+
snake.move()
29+
30+
if snake.head.distance(food) < 15:
31+
food.refresh()
32+
snake.extend()
33+
scoreboard.inc_score()
34+
35+
if (
36+
snake.head.xcor() > 280
37+
or snake.head.xcor() < -280
38+
or snake.head.ycor() > 280
39+
or snake.head.ycor() < -280
40+
):
41+
scoreboard.reset()
42+
snake.reset()
43+
44+
for segment in snake.segments[1:]:
45+
if snake.head.distance(segment) < 15:
46+
scoreboard.reset()
47+
snake.reset()
48+
49+
screen.exitonclick()

Projects/snake_game/scoreboard.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from turtle import Turtle
2+
3+
FONT = ("courier", 24, "normal")
4+
ALIGNMENT = "center"
5+
6+
7+
class Scoreboard(Turtle):
8+
def __init__(self):
9+
super().__init__()
10+
self.score = 0
11+
with open("data.txt") as data:
12+
self.high_score = int(data.read())
13+
self.color("white")
14+
self.penup()
15+
self.goto(0, 270)
16+
self.write(
17+
f"Score : {self.score} High Score : {self.high_score}",
18+
align=ALIGNMENT,
19+
font=FONT,
20+
)
21+
self.hideturtle()
22+
23+
def update_scorebaord(self):
24+
self.clear()
25+
self.write(
26+
f"Score : {self.score} High Score : {self.high_score}",
27+
align=ALIGNMENT,
28+
font=FONT,
29+
)
30+
31+
def reset(self):
32+
if self.score > self.high_score:
33+
self.high_score = self.score
34+
with open("data.txt", mode="w") as data:
35+
data.write(f"{self.high_score}")
36+
self.score = 0
37+
self.update_scorebaord()
38+
39+
def inc_score(self):
40+
self.score += 1
41+
self.clear()
42+
self.update_scorebaord()

Projects/snake_game/snake.py

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

0 commit comments

Comments
 (0)