Tkinter Canvas Applying random numbers to the movement of multiple spheres
In Las Vegas or Macau casinos, you can often see the machine horse racing gambling equipment, in fact, if we change the ball into a horse race its meaning is the same.
Concept 1: Casino cheating methods
Suppose you want the yellow ball to run faster, the probability of it winning is 70%, you can use randint( ) to generate a random number from 1 to 100, so that the random number moves the yellow ball between 1 and 70, and the aqua blue ball between 71 and 100, so you can cheat.
Concept 2: Casino cheating now
When playing horse racing bets must be placed, the best way to cheat in the casino is to let the horse with the least bets have a higher probability of moving, so that the money rolls in.
Concept 3: No Cheating
We can design random numbers to move the yellow ball between 1 and 50 and the aqua blue ball between 51 and 100.
Example#1
Run the cycle 100 times to see which ball runs faster, giving the yellow ball a 70% chance of winning.
from tkinter import *
import time
from random import *
tk = Tk()
tk.title("apidemos.com")
canvas = Canvas(tk,width=500, height=250) # Create Canvas
canvas.pack()
id1 = canvas.create_oval(10,50,60,100,fill="yellow")
id2 = canvas.create_oval(10,150,60,200,fill="aqua")
for x in range(0,100):
if randint(1,100) > 70:
canvas.move(id2,5,0)
else:
canvas.move(id1,5,0) # ID=1 Shift 5 pixels on the x-axis and 2 pixels on the y-axis
tk.update() # Forced tkinter redraw
time.sleep(0.05)
# tk.mainloop()
Output: