Test/Python(20220101~)
Days20. 뱀게임1
kiostory
2022. 2. 24. 23:03
from turtle import Screen, Turtle
import time
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.tracer(0) # 화면 깜빡임을 없애기 위해 자동 업데이트를 off
screen.title("kio's snake game - ver.20220223(the first one)")
color = ["red", "orange", "yellow", "green", "blue", "navy", "purple"]
color_index = 0
turtle_num = 3 # 뱀길이는 3개로 시작
starting_position = [(0,0),(-20,0),(-40,0)]
# for num in range(turtle_num): # 같은 이름이라도 object는 다르구나!!! 알게됨
# new_turtle = Turtle(shape="square")
# new_turtle.color(color[num])
# new_turtle.pu()
# new_turtle.setpos(x=0-(num*20), y=0)
# all_turtles.append(new_turtle)
turtles = []
for pos in starting_position: # 같은 이름이라도 object는 다르구나!!! 알게됨
new_turtle = Turtle(shape="square") # 뱀모양은 사각형
new_turtle.color(color[color_index]) # 각 블럭의 색깔은 무지개로 하고싶었다
new_turtle.pu() # 펜을 내려놓으면 줄이생겨서 올린다
new_turtle.setpos(pos) # 창의 중간에서 각 블럭의 위치에 내려놓는다
color_index += 1
# all_turtles.append(new_turtle)
turtles.append(new_turtle) # 각 터틀 오브젝트를 리스트에 저장
game_is_on = True
while game_is_on: # 게임이 on일때까지
screen.update() # 화면 업데이트
time.sleep(0.2)
for tut_num in range(len(turtles)-1, 0, -1): #(start=2, stop=0, step=-1)
new_x = turtles[tut_num - 1].xcor() # 현재 터틀 위치는 바로 앞 터틀의 x,y로
new_y = turtles[tut_num - 1].ycor()
turtles[tut_num].goto(new_x,new_y)
turtles[0].fd(20) # 20만큼 전진하고
#turtles[0].left(90)
screen.exitonclick()