100일의 코드: 2022년 완전한 Python Pro 부트캠프 - 21일차(Snake Game - 전체 코드)
from turtle import Screen
from snake import Snake
from food import Food
from scoreboard import Scoreboard
import time
# Screen setup
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("SnakeGame")
screen.tracer(0)
snake = Snake()
food = Food()
score = 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.1)
snake.move()
# Detect collision with food.
if snake.head.distance(food) < 15:
food.refresh()
snake.extend()
score.increase_score()
# Detect collision with tail using slicing
for segment in snake.all_snake[1:]:
if snake.head.distance(segment) < 10:
game_is_on = False
score.game_over()
# 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
score.game_over()
screen.exitonclick()
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.all_snake = []
self.create_snake()
self.head = self.all_snake[0]
def create_snake(self):
for position in STARTING_POSITIONS:
self.add_segment(position)
def add_segment(self, position):
new_snake = Turtle("square")
new_snake.color("white")
new_snake.penup()
new_snake.goto(position)
self.all_snake.append(new_snake)
def extend(self):
self.add_segment(self.all_snake[-1].position())
def move(self):
for seg_num in range(len(self.all_snake) - 1, 0, -1):
new_x = self.all_snake[seg_num - 1].xcor()
new_y = self.all_snake[seg_num - 1].ycor()
self.all_snake[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)
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)
from turtle import Turtle
ALIGNMENT = "center"
FONT = ("Courier", 20, "normal")
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.hideturtle()
self.score = 0
self.penup()
self.setx(0)
self.sety(260)
self.pendown()
self.color("white")
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()
Reference
이 문제에 관하여(100일의 코드: 2022년 완전한 Python Pro 부트캠프 - 21일차(Snake Game - 전체 코드)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/mike_kameta_aed62d48c2d0f/100-days-of-code-the-complete-python-pro-bootcamp-for-2022-day-21-snake-game-complete-code-5a9텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)