diff --git a/Snake-Game/README.md b/Snake-Game/README.md new file mode 100644 index 0000000..c6af891 --- /dev/null +++ b/Snake-Game/README.md @@ -0,0 +1,35 @@ +# Snake Game 🐍 + +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. + +## Features +- Interactive gameplay. +- Score tracking. +- Simple and user-friendly interface. + +## Installation +1. Clone this repository: + ```bash + git clone +2. Navigate the project folder + ```bash + cd snake-game +3. Run the game + ```bash + python snake_game.py + +# How to Play +1. Use Arrow Keys to control the direction of the snake: + 1. Up: Move up. + 2. Down: Move down. + 3. Left: Move left. + 4. Right: Move right. +2. Collect food to increase your score. +3. Avoid colliding with: + 1. Walls + 2. The snake’s body. +4. Goal: Achieve the highest score possible! + +# Technologies Used +Python + diff --git a/Snake-Game/Snake.py b/Snake-Game/Snake.py new file mode 100644 index 0000000..384b841 --- /dev/null +++ b/Snake-Game/Snake.py @@ -0,0 +1,60 @@ +from turtle import Turtle, Screen +STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)] +MOVE_DISTANCE = 20 +UP = 90 +DOWN = 270 +RIGHT = 0 +LEFT = 180 + + +class Snake: + def __init__(self): + self.segments = [] + self.x = 0 + self.create_snake() + self.head = self.segments[0] + + def create_snake(self): + for position in STARTING_POSITIONS: + self.add_segment(position) + def add_segment(self, position): + new_segment = Turtle("square") + new_segment.color("white") + new_segment.penup() + new_segment.goto(position) + self.segments.append(new_segment) + 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_segment(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) + + def down(self): + if self.head.heading() != UP: + self.head.setheading(DOWN) + + def left(self): + if self.head.heading() != RIGHT: + self.head.setheading(LEFT) + + def right(self): + if self.head.heading() != LEFT: + self.head.setheading(RIGHT) + + + diff --git a/Snake-Game/__pycache__/Snake.cpython-312.pyc b/Snake-Game/__pycache__/Snake.cpython-312.pyc new file mode 100644 index 0000000..bc7e380 Binary files /dev/null and b/Snake-Game/__pycache__/Snake.cpython-312.pyc differ diff --git a/Snake-Game/__pycache__/food.cpython-312.pyc b/Snake-Game/__pycache__/food.cpython-312.pyc new file mode 100644 index 0000000..d891ad2 Binary files /dev/null and b/Snake-Game/__pycache__/food.cpython-312.pyc differ diff --git a/Snake-Game/__pycache__/scoreboard.cpython-312.pyc b/Snake-Game/__pycache__/scoreboard.cpython-312.pyc new file mode 100644 index 0000000..8442a8e Binary files /dev/null and b/Snake-Game/__pycache__/scoreboard.cpython-312.pyc differ diff --git a/Snake-Game/data.py b/Snake-Game/data.py new file mode 100644 index 0000000..bafa81b --- /dev/null +++ b/Snake-Game/data.py @@ -0,0 +1,3 @@ +from scoreboard import Score +score = Score() +HighScore = score.high_score \ No newline at end of file diff --git a/Snake-Game/data.txt b/Snake-Game/data.txt new file mode 100644 index 0000000..7813681 --- /dev/null +++ b/Snake-Game/data.txt @@ -0,0 +1 @@ +5 \ No newline at end of file diff --git a/Snake-Game/food.py b/Snake-Game/food.py new file mode 100644 index 0000000..b01a2f9 --- /dev/null +++ b/Snake-Game/food.py @@ -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_wid=0.5, stretch_len=0.5) + self.color("blue") + self.speed("fastest") + self.refresh() + + def refresh(self): + random_x = random.randrange(-270,270) + random_y = random.randrange(-270, 270) + self.goto(random_x, random_y) diff --git a/Snake-Game/main.py b/Snake-Game/main.py new file mode 100644 index 0000000..4a7fd65 --- /dev/null +++ b/Snake-Game/main.py @@ -0,0 +1,53 @@ +from turtle import Turtle, Screen +import time +from food import Food +from Snake import Snake +from scoreboard import Score +screen = Screen() +screen.setup(width=600, height=600) +screen.title("My Snake Game") +screen.bgcolor("black") +screen.tracer(0) +snake = Snake() +food = Food() +score = Score() +screen.listen() +screen.onkey(snake.up, "Up") +screen.onkey(snake.down, "Down") +screen.onkey(snake.right, "Right") +screen.onkey(snake.left, "Left") + +is_game_on = True +while is_game_on: + screen.update() + time.sleep(0.1) + snake.move() + # collision with food + if snake.head.distance(food) < 15: + food.refresh() + snake.extend() + score.increase_score() + # collision with wall + if snake.head.xcor() < -280 or snake.head.xcor() > 280 or snake.head.ycor() < -280 or snake.head.ycor() > 280: + score.reset() + data = str(score.high_score) + with open("data.txt", mode="w") as file: + file.write(data) + snake.reset() + # collision with wall + for segment in snake.segments[1:]: + if snake.head.distance(segment) < 10: + score.reset() + data = str(score.high_score) + with open("data.txt", mode="w") as file: + file.write(data) + snake.reset() + + + + + + + + +screen.exitonclick() diff --git a/Snake-Game/scoreboard.py b/Snake-Game/scoreboard.py new file mode 100644 index 0000000..9510404 --- /dev/null +++ b/Snake-Game/scoreboard.py @@ -0,0 +1,31 @@ +from turtle import Turtle + +class Score(Turtle): + + + def __init__(self): + self.score = 0 + with open("data.txt") as file: + self.HighScore = int(file.read()) + self.high_score = self.HighScore + super().__init__() + self.color("white") + self.penup() + self.goto(0, 265) + self.hideturtle() + self.update_score() + def update_score(self): + self.clear() + self.write(f"Score: {self.score} High Score:{self.high_score}", align='center', font=('Arial', 24, 'normal')) + def reset(self): + if self.score > self.high_score: + self.high_score = self.score + self.score = 0 + self.update_score() + def increase_score(self): + self.score += 1 + self.update_score() + + # def game_over(self): + # self.goto(0, 0) + # self.write("GAME OVER", align='center', font=('Arial', 24, 'normal')) \ No newline at end of file