Made the Snake Game!

Added Snake Game demo.
This commit is contained in:
rzmk 2021-08-12 17:45:13 -04:00
parent 735ff97d0d
commit 9e21e4dbdd
5 changed files with 165 additions and 0 deletions

15
Day 21/README.md Normal file
View file

@ -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)

18
Day 21/food.py Normal file
View file

@ -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)

51
Day 21/main.py Normal file
View file

@ -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()

28
Day 21/scoreboard.py Normal file
View file

@ -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)

53
Day 21/snake.py Normal file
View file

@ -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)