Python: Math racing


Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên
import random
from threading import Thread, active_count
from queue import Queue
from turtle import Screen, Turtle

screen = Screen()

# The first turtle
turtle1 = Turtle()
turtle1.color('red')
turtle1.shape('turtle')
turtle1.penup()
turtle1.goto(-300, 50)
turtle1.pendown()

# The second turtle
turtle2 = Turtle()
turtle2.color('blue')
turtle2.shape('turtle')
turtle2.penup()
turtle2.goto(-300, -50)
turtle2.pendown()

# The drawer:
drawer = Turtle()
drawer.penup()
drawer.goto(300, 150)
drawer.color('green')
drawer.right(90)
drawer.pendown()
drawer.goto(300, -150)

QUEUE_SIZE = 1


def process_queue():
    while not actions.empty():
        action, *arguments = actions.get()
        action(*arguments)

    if active_count() > 1:
        screen.ontimer(process_queue, 100)


actions = Queue(QUEUE_SIZE)  # a thread-safe data structure


def move(turt):
    while turt.xcor() < 300:
        actions.put((turt.forward, 0.2))
    print("The first turtle win!")


def turtle2run(turt):
    for turn in range(100):
        number1 = random.randint(1, 10)
        number2 = random.randint(1, 10)

        c = int(input(f"{number1} + {number2} =?"))

        if c == number1 + number2:
            actions.put((turt.forward, 50))

        if turt.xcor() >= 300:
            print("The second turtle win!")
            return


Thread(target=move, args=[turtle1], daemon=True).start()
Thread(target=turtle2run, args=[turtle2], daemon=True).start()

process_queue()
» Tiếp: Rose: Phép toán số học, so sánh, logic, điều kiện if-else
« Trước: Cách in có định dạng trong Python với bài toán in các thừa số nguyên tố
Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên
Copied !!!