Last active
September 11, 2021 18:57
-
-
Save kubilaykaand/a452d7d4ebda27445e343b320389972c to your computer and use it in GitHub Desktop.
Classic snake game, built with the Turtle( ) module, inside the library of "turtle" in Python.
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.refresh() | |
self.shape("circle") | |
self.penup() | |
self.shapesize(stretch_len=0.5, stretch_wid=0.5) | |
self.color("blue") | |
self.speed("fastest") | |
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
from turtle import Screen | |
from snake import Snake | |
from food import Food | |
from scoreboard import Scoreboard | |
import time | |
screen = Screen() | |
screen.setup(width=600, height=600) | |
screen.bgcolor("black") | |
screen.title("My 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.right, "Right") | |
screen.onkey(snake.left, "Left") | |
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 the wall. | |
if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor() > 280 or snake.head.ycor() < -280: | |
game_is_on = False | |
scoreboard.game_over() | |
# Detect collision with tail. | |
for segment in snake.segments[1:]: | |
if snake.head.d istance < 10: | |
game_is_on = False | |
scoreboard.game_over() | |
# if head collides with any segment in the tail: | |
# trigger game_over | |
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 Scoreboard(Turtle): | |
def __init__(self): | |
super().__init__() | |
self.score = 0 | |
self.color("white") | |
self.hideturtle() | |
self.penup() | |
self.goto(0,260) | |
self.update_scoreboard() | |
def update_scoreboard(self): | |
self.write(f"Score: {self.score}", align="center", font=("Arial", 24, "normal")) | |
def increase_score(self): | |
self.score += 1 | |
self.clear() | |
self.update_scoreboard() | |
def game_over(self): | |
self.goto(0,0) | |
self.write("GAME OVER", align=ALIGNMENT,font=FONT) |
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 time | |
STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)] | |
MOVE_DISTANCE = 20 | |
UP = 90 | |
DOWN = 270 | |
RIGHT = 0 | |
LEFT = 180 | |
class Snake(Turtle): | |
def __init__(self): | |
super().__init__() | |
self.segments = [] | |
self.create_snake() | |
self.head = self.segments[0] | |
def create_snake(self): | |
for position in STARTING_POSITIONS: | |
self.add_segment(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(20) | |
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 extend(self): | |
self.add_segment(self.segments[-1].position()) | |
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 right(self): | |
if self.head.heading() != LEFT: | |
self.head.setheading(RIGHT) | |
def left(self): | |
if self.head.heading() != RIGHT: | |
self.head.setheading(LEFT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment