diff --git a/Projects/snake_game/.gitignore b/Projects/snake_game/.gitignore new file mode 100644 index 0000000..5da0c2e --- /dev/null +++ b/Projects/snake_game/.gitignore @@ -0,0 +1,2 @@ +.idea +venv/q \ No newline at end of file diff --git a/Projects/snake_game/README.md b/Projects/snake_game/README.md new file mode 100644 index 0000000..f81b325 --- /dev/null +++ b/Projects/snake_game/README.md @@ -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 + diff --git a/Projects/snake_game/data.txt b/Projects/snake_game/data.txt new file mode 100755 index 0000000..c227083 --- /dev/null +++ b/Projects/snake_game/data.txt @@ -0,0 +1 @@ +0 \ No newline at end of file diff --git a/Projects/snake_game/food.py b/Projects/snake_game/food.py new file mode 100755 index 0000000..f78740d --- /dev/null +++ b/Projects/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_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) diff --git a/Projects/snake_game/main.py b/Projects/snake_game/main.py new file mode 100755 index 0000000..9bff88e --- /dev/null +++ b/Projects/snake_game/main.py @@ -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() diff --git a/Projects/snake_game/scoreboard.py b/Projects/snake_game/scoreboard.py new file mode 100755 index 0000000..041714d --- /dev/null +++ b/Projects/snake_game/scoreboard.py @@ -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() diff --git a/Projects/snake_game/snake.py b/Projects/snake_game/snake.py new file mode 100755 index 0000000..b112fb8 --- /dev/null +++ b/Projects/snake_game/snake.py @@ -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