turtle 모듈을 이용하여 스네이크 게임 만들기

  1. 몸체 만들기
import turtle
from turtle import Turtle, Screen

screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("My Snake Game")

turtle.hideturtle()

all_segment = []
starting_point = [(0,0),(-20,0),(-40,0)]
for segment in range(3) :
    new_segment = Turtle("square")
    new_segment.color("white")
    new_segment.goto(starting_point[segment])

screen.exitonclick()
  1. 몸체 움직이기
import turtle
import time
from turtle import Turtle, Screen

screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("My Snake Game")
screen.tracer(0)
turtle.hideturtle()
turtle.speed(1)

all_segment = []
starting_point = [(0,0),(-20,0),(-40,0)]
for segment in range(3) :
    new_segment = Turtle("square")
    new_segment.color("white")
    new_segment.penup()
    new_segment.goto(starting_point[segment])
    all_segment.append(new_segment)

game_is_on = True
while game_is_on :
    screen.update()
    time.sleep(0.1)
    for seg_num in range(len(all_segment)-1,0,-1) :
        new_x = all_segment[seg_num - 1].xcor()
        new_y = all_segment[seg_num - 1].ycor()
        all_segment[seg_num].goto(new_x,new_y)
    all_segment[0].forward(10)
    all_segment[0].left(90)

screen.exitonclick()

1번부터 2번까지 객체지향으로 재프로그래밍하기

메인 파일

import turtle
import time
from turtle import Turtle, Screen
from snake import Snake

screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("My Snake Game")
screen.tracer(0)
turtle.hideturtle()
turtle.speed(1)

snake = Snake()

game_is_on = True
while game_is_on :
    screen.update()
    time.sleep(0.1)
    snake.move_snake()

screen.exitonclick()

뱀과 관련된 객체

from turtle import Turtle

STARTING_POINT = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20

class Snake() :
    def __init__(self):
        self.all_segment=[]
        self.create_snake()

    def create_snake(self):
        for segment in range(3):
            new_segment = Turtle("square")
            new_segment.color("white")
            new_segment.penup()
            new_segment.goto(STARTING_POINT[segment])
            self.all_segment.append(new_segment)

    def move_snake(self):
        for seg_num in range(len(self.all_segment) - 1, 0, -1):
            new_x = self.all_segment[seg_num - 1].xcor()
            new_y = self.all_segment[seg_num - 1].ycor()
            self.all_segment[seg_num].goto(new_x, new_y)
        self.all_segment[0].forward(MOVE_DISTANCE)
  1. 키 입력으로 뱀의 방향 바꾸기
import turtle
import time
from turtle import Turtle, Screen
from snake import Snake

screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("My Snake Game")
screen.tracer(0)
turtle.hideturtle()
turtle.speed(1)

snake = Snake()

screen.listen()
screen.onkey(fun=snake.move_up, key="Up")
screen.onkey(fun=snake.move_down, key="Down")
screen.onkey(fun=snake.move_left, key="Left")
screen.onkey(fun=snake.move_right, key="Right")

game_is_on = True
while game_is_on :
    screen.update()
    time.sleep(0.1)
    snake.move_snake()

screen.exitonclick()
from turtle import Turtle

STARTING_POINT = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
class Snake() :
    def __init__(self):
        self.all_segment=[]
        self.create_snake()
        self.head = self.all_segment[0]

    def create_snake(self):
        for segment in range(3):
            new_segment = Turtle("square")
            new_segment.color("white")
            new_segment.penup()
            new_segment.goto(STARTING_POINT[segment])
            self.all_segment.append(new_segment)

    def move_snake(self):
        for seg_num in range(len(self.all_segment) - 1, 0, -1):
            new_x = self.all_segment[seg_num - 1].xcor()
            new_y = self.all_segment[seg_num - 1].ycor()
            self.all_segment[seg_num].goto(new_x, new_y)
        self.head.forward(MOVE_DISTANCE)

    def move_up(self):
        if self.head.heading() != DOWN :
            self.head.setheading(UP)

    def move_down(self):
        if self.head.heading() != UP :
          self.head.setheading(DOWN)

    def move_left(self):
        if self.head.heading() != RIGHT :
          self.head.setheading(LEFT)

    def move_right(self):
        if self.head.heading() != LEFT :
         self.head.setheading(RIGHT)