티스토리 뷰

Test/Python(20220101~)

Days19. Turtle race

kiostory 2022. 2. 21. 23:48
import turtle
import random
from turtle import Turtle, Screen
screen = Screen()
screen.setup(width=500,height=400)
guess = screen.textinput(title="Turtle race by kio, 21Feb2022", prompt="Guess what color of turtle wins? Make your bet ! : ")
turtle_num = 7
color = ["red", "orange", "yellow", "green", "blue", "navy", "purple"]
all_turtles = []
for num in range(turtle_num):                # 같은 이름이라도 object는 다르구나!!! 알게됨 
    new_turtle = Turtle(shape="turtle")
    new_turtle.color(color[num])
    new_turtle.pu()
    new_turtle.setpos(x=-240, y=(400 / 2) - 20 - ((int(380 / turtle_num)) * num))
    all_turtles.append(new_turtle)
if guess:
    is_race_on = True
while is_race_on:
    pick_turtle = random.randint(0,turtle_num-1)
    all_turtles[pick_turtle].forward(random.randint(0,5))
    if all_turtles[pick_turtle].xcor() >= (500/2-10):
        is_race_on = False
        winner_turtle = all_turtles[pick_turtle].pencolor()
if guess == winner_turtle:
    print(f"You win! the winner turtle is {winner_turtle}.")
else:
    print(f"You lose! the winner turtle is {winner_turtle}.")
screen.exitonclick()

 

'Test > Python(20220101~)' 카테고리의 다른 글

Days20. 뱀게임2 - 프로그램의 클래스화  (0) 2022.02.28
Days20. 뱀게임1  (0) 2022.02.24
Days19. Make an Etch-a-Sketch  (0) 2022.02.21
Days18. Hirst painting project  (0) 2022.02.20
Days18. Spirograph  (0) 2022.02.20
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함