Test/Python(20220101~)

Days21. 뱀게임 최종

kiostory 2022. 3. 2. 19:42

* main.py

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.tracer(0)
screen.title("kio's snake game - ver.20220302(the first one)")
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) < 20:
        food.refresh()
        snake.extend()
        scoreboard.clear()
        scoreboard.score()
    # Detect collision with wall.
    if snake.head.xcor() < -290 or snake.head.xcor() > 290 or snake.head.ycor() < -290 or snake.head.ycor() > 290:
        scoreboard.game_over()
        game_is_on = False
    for tut in snake.turtles[1:]:      # slicing
        if  snake.head.distance(tut) < 10:
            game_is_on = False
            scoreboard.game_over()
screen.exitonclick()

 

* snake.py

from turtle import Turtle
import random
STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)]
COLOR = ["red", "orange", "yellow", "green", "blue", "navy", "purple"]
MOVING_DISTANCE = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
class Snake:
    def __init__(self):
        self.turtles = []
        self.create_snake()
        self.head = self.turtles[0]
        self.color_index = 0
    def create_snake(self):
        for pos in STARTING_POSITIONS:
            self.add_segment(pos)
    def add_segment(self, pos):
        i = random.randint(0, 6)
        new_turtle = Turtle(shape="square")
        new_turtle.color(COLOR[i])
        new_turtle.pu()
        new_turtle.setpos(pos)
        self.turtles.append(new_turtle)
    def extend(self):
        # add a new segment to the snake.
        self.add_segment(self.turtles[-1].position())
    def move(self):
        for tut_num in range(len(self.turtles) - 1, 0, -1):  # (start=2, stop=0, step=-1)
            new_x = self.turtles[tut_num - 1].xcor()
            new_y = self.turtles[tut_num - 1].ycor()
            self.turtles[tut_num].goto(new_x, new_y)
        self.head.fd(MOVING_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)

 

* food.py

from turtle import Turtle
import random
class Food(Turtle):
    def __init__(self):
        super().__init__()
        self.shape("circle")
        self.pu()
        self.shapesize(stretch_wid=0.5, stretch_len=0.5)  # default 20by20에서 0.5를 입력하였으니, 10by10 크기가 됨
        self.color("white")
        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)

 

* scoreboard.py

from turtle import Turtle
ALIGNMENT = "center"
FONT = ("Arial", 24, "normal")
class Scoreboard(Turtle):
    def __init__(self):
        super().__init__()
        self.yumyum = 0
        self.goto(0, 265)
        self.color("white")
        self.pu()
        self.hideturtle()
        self.score()
    def game_over(self):
        self.goto(0, 0)
        self.write("GAME OVER", align=ALIGNMENT, font=FONT)
    def score(self):
        self.write(f"Score: {self.yumyum}", align=ALIGNMENT, font=FONT)
        self.yumyum += 1