TIL: Python Basics Day 20 - Snake Game (first 3 steps)
Project: Snake Game (3 steps)
Create the body
Move the snake
Control the snake
from turtle import Screen, Turtle
import time
from snake import Snake
screen = Screen()
screen.setup(width= 600, height=600)
screen.bgcolor("black")
screen.title("Snaky Game")
screen.tracer(0)
segments = []
for turtle_index in range(3):
new_segment = Turtle(shape="square")
new_segment.color("white")
new_segment.penup()
new_segment.goto(x=0 + turtle_index * -20, y=0)
segments.append(new_segment)
#2. move the snake
game_is_on = True
while game_is_on:
screen.update()
time.sleep(1)
#snake move its body overlaping itself(2nd part's position becomes 3rd part's new position)
for seg_num in range(len(segments) - 1, 0 , -1):
new_x = segments[seg_num - 1].xcor()
new_y = segments[seg_num - 1].ycor()
segments[seg_num].goto(new_x, new_y)
segments[0].forward(20)
#3. Control the snake
snake = Snake()
snake.move()
screen.exitonclick()
Author And Source
이 문제에 관하여(TIL: Python Basics Day 20 - Snake Game (first 3 steps)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@daylee/TIL-Python-Basics-Day-20저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)