Skip to content

Commit 6a2ff2c

Browse files
authored
Merge pull request #102 from Nandini-Sutrave/Snake_game_contribution
Added Python Snake Game with Turtle Graphics
2 parents c5b2132 + 3eea0a2 commit 6a2ff2c

File tree

10 files changed

+202
-0
lines changed

10 files changed

+202
-0
lines changed

Snake-Game/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Snake Game 🐍
2+
3+
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.
4+
5+
## Features
6+
- Interactive gameplay.
7+
- Score tracking.
8+
- Simple and user-friendly interface.
9+
10+
## Installation
11+
1. Clone this repository:
12+
```bash
13+
git clone <your-repository-url>
14+
2. Navigate the project folder
15+
```bash
16+
cd snake-game
17+
3. Run the game
18+
```bash
19+
python snake_game.py
20+
21+
# How to Play
22+
1. Use Arrow Keys to control the direction of the snake:
23+
1. Up: Move up.
24+
2. Down: Move down.
25+
3. Left: Move left.
26+
4. Right: Move right.
27+
2. Collect food to increase your score.
28+
3. Avoid colliding with:
29+
1. Walls
30+
2. The snake’s body.
31+
4. Goal: Achieve the highest score possible!
32+
33+
# Technologies Used
34+
Python
35+

Snake-Game/Snake.py

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

Snake-Game/data.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from scoreboard import Score
2+
score = Score()
3+
HighScore = score.high_score

Snake-Game/data.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5

Snake-Game/food.py

Lines changed: 19 additions & 0 deletions
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_wid=0.5, stretch_len=0.5)
12+
self.color("blue")
13+
self.speed("fastest")
14+
self.refresh()
15+
16+
def refresh(self):
17+
random_x = random.randrange(-270,270)
18+
random_y = random.randrange(-270, 270)
19+
self.goto(random_x, random_y)

Snake-Game/main.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from turtle import Turtle, Screen
2+
import time
3+
from food import Food
4+
from Snake import Snake
5+
from scoreboard import Score
6+
screen = Screen()
7+
screen.setup(width=600, height=600)
8+
screen.title("My Snake Game")
9+
screen.bgcolor("black")
10+
screen.tracer(0)
11+
snake = Snake()
12+
food = Food()
13+
score = Score()
14+
screen.listen()
15+
screen.onkey(snake.up, "Up")
16+
screen.onkey(snake.down, "Down")
17+
screen.onkey(snake.right, "Right")
18+
screen.onkey(snake.left, "Left")
19+
20+
is_game_on = True
21+
while is_game_on:
22+
screen.update()
23+
time.sleep(0.1)
24+
snake.move()
25+
# collision with food
26+
if snake.head.distance(food) < 15:
27+
food.refresh()
28+
snake.extend()
29+
score.increase_score()
30+
# collision with wall
31+
if snake.head.xcor() < -280 or snake.head.xcor() > 280 or snake.head.ycor() < -280 or snake.head.ycor() > 280:
32+
score.reset()
33+
data = str(score.high_score)
34+
with open("data.txt", mode="w") as file:
35+
file.write(data)
36+
snake.reset()
37+
# collision with wall
38+
for segment in snake.segments[1:]:
39+
if snake.head.distance(segment) < 10:
40+
score.reset()
41+
data = str(score.high_score)
42+
with open("data.txt", mode="w") as file:
43+
file.write(data)
44+
snake.reset()
45+
46+
47+
48+
49+
50+
51+
52+
53+
screen.exitonclick()

Snake-Game/scoreboard.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from turtle import Turtle
2+
3+
class Score(Turtle):
4+
5+
6+
def __init__(self):
7+
self.score = 0
8+
with open("data.txt") as file:
9+
self.HighScore = int(file.read())
10+
self.high_score = self.HighScore
11+
super().__init__()
12+
self.color("white")
13+
self.penup()
14+
self.goto(0, 265)
15+
self.hideturtle()
16+
self.update_score()
17+
def update_score(self):
18+
self.clear()
19+
self.write(f"Score: {self.score} High Score:{self.high_score}", align='center', font=('Arial', 24, 'normal'))
20+
def reset(self):
21+
if self.score > self.high_score:
22+
self.high_score = self.score
23+
self.score = 0
24+
self.update_score()
25+
def increase_score(self):
26+
self.score += 1
27+
self.update_score()
28+
29+
# def game_over(self):
30+
# self.goto(0, 0)
31+
# self.write("GAME OVER", align='center', font=('Arial', 24, 'normal'))

0 commit comments

Comments
 (0)