본문 바로가기

python study

파이썬 turtle로 거북이 경주시키기

from turtle import Turtle, Screen
import random
raceon = False
screen = Screen()
screen.setup(500,400)
userbet = screen.textinput("Bet your turtle", "누가 이길까요?")
print(userbet)
yposition = [-100, -70, -40, -10, 20, 50]
mtcolor = ["red", "orange", "yellow", "green", "blue", "purple"]
all_turtles = []
for i in range(0,6):
    mt = Turtle(shape="turtle")
    mt.color(mtcolor[i])
    mt.penup()
    all_turtles.append(mt)
    
    mt.setposition(x=-230, y=yposition[i])

if userbet:
    raceon = True
while raceon:
    for turtles in all_turtles:
        if turtles.xcor() > 230:
            raceon = False
            win_color = turtles.pencolor()
            if win_color == userbet:
                print(f"당신이 예측한 {win_color}가 승자입니다!")
            else:
                print(f"아쉽네요, {win_color}가 승자입니다.")
        random_move = random.randint(1,10)
        turtles.forward(random_move)

screen.exitonclick()

 

 

'python study' 카테고리의 다른 글

파이썬 간단한 해시값 무결성 검사  (0) 2025.09.21
파이썬으로 블랙잭 만들기  (2) 2024.09.08
파이썬으로 계산기 만들기  (0) 2024.09.07
Python으로 행맨게임 만들기  (0) 2024.09.05
파이썬 공부 1  (0) 2024.05.21