TIL: Python Basics Day 20 - Snake Game (first 3 steps)

1054 단어 pythonpython

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

좋은 웹페이지 즐겨찾기