Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snake Game Completion #12

Merged
merged 3 commits into from Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added day-20/__pycache__/food.cpython-311.pyc
Binary file not shown.
Binary file added day-20/__pycache__/scoreboard.cpython-311.pyc
Binary file not shown.
Binary file added day-20/__pycache__/snake.cpython-311.pyc
Binary file not shown.
18 changes: 18 additions & 0 deletions day-20/food.py
@@ -0,0 +1,18 @@
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.speed(0)
self.color("red")
self.refresh()

def refresh(self):
random_x = random.randint(-280, 280)
random_y = random.randint(-280, 280)
self.goto(random_x, random_y)
50 changes: 50 additions & 0 deletions day-20/main.py
@@ -0,0 +1,50 @@
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("white")
screen.title("Big Black Snake")
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.05)
snake.move_snake()

#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:
game_is_on = False
scoreboard.game_over()

#Detect collision with tail.
for full_snake in snake.full_snake:
if full_snake == snake.head:
pass
elif snake.head.distance(full_snake) < 10:
game_is_on = False
scoreboard.game_over()


screen.exitonclick()
26 changes: 26 additions & 0 deletions day-20/scoreboard.py
@@ -0,0 +1,26 @@
from turtle import Turtle
ALIGNMENT = "center"
FONT = ("Courier", 24, "normal")


class Scoreboard(Turtle):

def __init__(self):
super().__init__()
self.score = 0
self.penup()
self.goto(0, 270)
self.hideturtle()
self.update_scoreboard()

def update_scoreboard(self):
self.write(f"Score: {self.score}", align=ALIGNMENT, font=FONT)

def game_over(self):
self.goto(0, 0)
self.write("GAME OVER", align=ALIGNMENT, font=FONT)

def increase_score(self):
self.score += 1
self.clear()
self.update_scoreboard()
52 changes: 52 additions & 0 deletions day-20/snake.py
@@ -0,0 +1,52 @@
from turtle import Turtle

STARTING_POSITION = [(0, 0), (-20, 0), (-40, 0)]
MOVE_SNAKE = 10

#DIRECTION IN DEGREES
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0

class Snake:
def __init__(self):
self.full_snake = []
self.create_snake()
self.head = self.full_snake[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.penup()
new_segment.goto(position)
self.full_snake.append(new_segment)

def extend(self):
self.add_segment(self.full_snake[-1].position())

def move_snake(self):
for snake_piece in range(len(self.full_snake) - 1, 0, -1):
new_x = self.full_snake[snake_piece - 1].xcor()
new_y = self.full_snake[snake_piece - 1].ycor()
self.full_snake[snake_piece].goto(new_x, new_y)
self.head.forward(MOVE_SNAKE)

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)