Created
August 26, 2024 18:07
-
-
Save Josip5521/d2360782802a3e368a60d56563a3af0c to your computer and use it in GitHub Desktop.
This is snake game we all play when we were child's.
This file contains hidden or 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
from turtle import Turtle | |
import random | |
class Food(Turtle): | |
def __init__(self): | |
super().__init__() | |
self.shape("circle") | |
self.penup() | |
self.shapesize(0.5, 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 hidden or 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
import time | |
from snake import Snake | |
from turtle import Screen | |
from food import Food | |
from scoreboard import Score | |
screen = Screen() | |
screen.setup(600, 600) | |
screen.bgcolor("black") | |
screen.title("My Snake Game") | |
screen.tracer(0) | |
snake = Snake() | |
food = Food() | |
scoreboard = Score() | |
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() | |
# Detect collision with food. | |
if snake.head.distance(food) < 15: | |
food.refresh() | |
snake.extend() | |
scoreboard.increase_score() | |
# Detect collision with wall. | |
if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor() > 280 or snake.head.ycor() < -280: | |
scoreboard.reset() | |
snake.reset() | |
# Detect collision with tail. | |
for segment in snake.segments[1:]: | |
if snake.head.distance(segment) < 10: | |
scoreboard.reset() | |
snake.reset() | |
screen.exitonclick() |
This file contains hidden or 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
from turtle import Turtle | |
ALIGNMENT = "center" | |
FONT = ("Courier", 24, "normal") | |
class Score(Turtle): | |
def __init__(self): | |
super().__init__() | |
with open("data.txt") as data: | |
self.high_score = int(data.read()) | |
self.score = 0 | |
self.color("white") | |
self.penup() | |
self.goto(0, 260) | |
self.hideturtle() | |
self.update_scoreboard() | |
def update_scoreboard(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_scoreboard() | |
def increase_score(self): | |
self.score += 1 | |
self.update_scoreboard() |
This file contains hidden or 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
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_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.segments[0].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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment