From 9e21e4dbddb41e427b8f35b49f162541a75f72d3 Mon Sep 17 00:00:00 2001 From: rzmk Date: Thu, 12 Aug 2021 17:45:13 -0400 Subject: [PATCH] Made the Snake Game! Added Snake Game demo. --- Day 21/README.md | 15 +++++++++++++ Day 21/food.py | 18 +++++++++++++++ Day 21/main.py | 51 ++++++++++++++++++++++++++++++++++++++++++ Day 21/scoreboard.py | 28 +++++++++++++++++++++++ Day 21/snake.py | 53 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 165 insertions(+) create mode 100644 Day 21/README.md create mode 100644 Day 21/food.py create mode 100644 Day 21/main.py create mode 100644 Day 21/scoreboard.py create mode 100644 Day 21/snake.py diff --git a/Day 21/README.md b/Day 21/README.md new file mode 100644 index 0000000..bb84ac7 --- /dev/null +++ b/Day 21/README.md @@ -0,0 +1,15 @@ +# Day 21 - Build the Snake Game Part 2: Inheritance & List Slicing + +- Built a snake game. +- Learned about class inheritance and list slicing. + +```python +# The Fish class inherits the attributes and methods of the Animal class +class Fish(Animal): + def __init__(self): + super().__init__() +``` + +## Snake Game + +![Day_21_Snake_Game](https://user-images.githubusercontent.com/30333942/129274809-8c292e1e-28e4-4df5-a1b0-57b77155782a.gif) diff --git a/Day 21/food.py b/Day 21/food.py new file mode 100644 index 0000000..7df94ea --- /dev/null +++ b/Day 21/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.color("red") + self.speed("fastest") + self.refresh() + + def refresh(self): + random_x = random.randint(-280, 280) + random_y = random.randint(-280, 255) + self.goto(random_x, random_y) diff --git a/Day 21/main.py b/Day 21/main.py new file mode 100644 index 0000000..7b1027b --- /dev/null +++ b/Day 21/main.py @@ -0,0 +1,51 @@ +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("A 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.075) + snake.move() + + # Food collision + if snake.head.distance(food) < 15: + food.refresh() + snake.extend() + scoreboard.increase_score() + + # Wall collision + if ( + snake.head.xcor() > 280 + or snake.head.xcor() < -280 + or snake.head.ycor() > 290 + or snake.head.ycor() < -280 + ): + scoreboard.game_over() + game_is_on = False + + # Tail collision + for segment in snake.segments[1:]: + if snake.head.distance(segment) < 10: + scoreboard.game_over() + game_is_on = False + +screen.exitonclick() diff --git a/Day 21/scoreboard.py b/Day 21/scoreboard.py new file mode 100644 index 0000000..60eaa0e --- /dev/null +++ b/Day 21/scoreboard.py @@ -0,0 +1,28 @@ +from turtle import Turtle + +ALIGNMENT = "center" +FONT = ("Open Sans", 16, "normal") + + +class Scoreboard(Turtle): + def __init__(self): + super().__init__() + self.score = 0 + self.penup() + self.hideturtle() + self.goto(x=0, y=260) + self.color("white") + self.update_scoreboard() + + def update_scoreboard(self): + self.write(f"Score: {self.score}", align=ALIGNMENT, font=FONT) + + def increase_score(self): + self.score += 1 + self.clear() + self.update_scoreboard() + + def game_over(self): + self.goto(x=0, y=0) + self.color("red") + self.write("GAME OVER", align=ALIGNMENT, font=FONT) diff --git a/Day 21/snake.py b/Day 21/snake.py new file mode 100644 index 0000000..176dd21 --- /dev/null +++ b/Day 21/snake.py @@ -0,0 +1,53 @@ +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): + segment = Turtle(shape="square") + segment.color("white") + segment.penup() + segment.goto(position) + self.segments.append(segment) + + def extend(self): + # Add a new segment to the snake. + self.add_segment(self.segments[-1].position()) + + def move(self): + for segment_index in range(len(self.segments) - 1, 0, -1): + new_x = self.segments[segment_index - 1].xcor() + new_y = self.segments[segment_index - 1].ycor() + self.segments[segment_index].goto(new_x, new_y) + self.head.forward(MOVE_DISTANCE) + + def up(self): + if self.head.heading() != DOWN: + self.head.setheading(90) + + def down(self): + if self.head.heading() != UP: + self.head.setheading(270) + + def left(self): + if self.head.heading() != RIGHT: + self.head.setheading(180) + + def right(self): + if self.head.heading() != LEFT: + self.head.setheading(0)