Skip to content

Commit

Permalink
Day 23
Browse files Browse the repository at this point in the history
  • Loading branch information
neubrom committed Sep 19, 2021
1 parent 1858ec9 commit 6e15d54
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 7 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions 24/Snake+Project+Code+from+Day+21/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10
19 changes: 19 additions & 0 deletions 24/Snake+Project+Code+from+Day+21/food.py
Original file line number Diff line number Diff line change
@@ -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)
53 changes: 53 additions & 0 deletions 24/Snake+Project+Code+from+Day+21/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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.left, "Left")
screen.onkey(snake.right, "Right")

game_is_on = True
while game_is_on:
screen.update()
time.sleep(0.2)
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:
if segment == snake.head:
pass
elif snake.head.distance(segment) < 10:
scoreboard.reset()
snake.reset()






screen.exitonclick()
33 changes: 33 additions & 0 deletions 24/Snake+Project+Code+from+Day+21/scoreboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from turtle import Turtle
ALIGNMENT = "center"
FONT = ("Courier", 18, "normal")


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.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 file:
file.write(f"{self.high_score}")
self.score = 0

def increase_score(self):
self.score += 1
self.clear()
self.update_scoreboard()
59 changes: 59 additions & 0 deletions 24/Snake+Project+Code+from+Day+21/snake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from turtle import Turtle
STARTING_POSITIONS = [(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_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)
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ Starting from : August 22, 2021

[Day 14 - Beginner - Higher Lower Game Project](https://github.com/neubrom/100day/tree/master/14)

[Abschnitt 15: Day 15 - Intermediate - Local Development Environment Setup & the Coffee Machine](https://github.com/neubrom/100day/tree/master/15)
[Day 15 - Intermediate - Local Development Environment Setup & the Coffee Machine](https://github.com/neubrom/100day/tree/master/15)

[Abschnitt 16: Day 16 - Intermediate - Object Oriented Programming (OOP)](https://github.com/neubrom/100day/tree/master/16)
[Day 16 - Intermediate - Object Oriented Programming (OOP)](https://github.com/neubrom/100day/tree/master/16)

[Abschnitt 17: Day 17 - Intermediate - The Quiz Project & the Benefits of OOP](https://github.com/neubrom/100day/tree/master/17)
[Day 17 - Intermediate - The Quiz Project & the Benefits of OOP](https://github.com/neubrom/100day/tree/master/17)

[Abschnitt 18: Day 18 - Intermediate - Turtle & the Graphical User Interface (GUI)](https://github.com/neubrom/100day/tree/master/18)
[Day 18 - Intermediate - Turtle & the Graphical User Interface (GUI)](https://github.com/neubrom/100day/tree/master/18)

[Abschnitt 19: Day 19 - Intermediate - Instances, State and Higher Order Functions](https://github.com/neubrom/100day/tree/master/19)
[Day 19 - Intermediate - Instances, State and Higher Order Functions](https://github.com/neubrom/100day/tree/master/19)

[Abschnitt 20: Day 20 - Intermediate - Build the Snake Game Part 1: Animation & Coordinates](https://github.com/neubrom/100day/tree/master/20)
[Day 20 - Intermediate - Build the Snake Game Part 1: Animation & Coordinates](https://github.com/neubrom/100day/tree/master/20)

[Abschnitt [21: Day 21 - Intermediate - Build the Snake Game Part 2: Inheritance & List Slicing](https://github.com/neubrom/100day/tree/master/21))
[Day 21 - Intermediate - Build the Snake Game Part 2: Inheritance & List Slicing](https://github.com/neubrom/100day/tree/master/21))

0 comments on commit 6e15d54

Please sign in to comment.