程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Animation: simulating dice with Tkinter

編輯:Python

Dice are essential props in many games , Although it is relatively simple to simulate the number of dice generated by code , But in graphic games , If you can simulate the effect of rolling dice , It will certainly add color to the game . It happens that the dice are needed in several small games that I've been thinking about recently , So I wrote a separate article to take out the implementation method of the small function of dice .


effect

The code implementation is also relatively simple , Because it's used python The built-in modules tkinter Realized GUI, So the code also uses tkinter To write .


Code implementation

from tkinter import *
import random
root = Tk()
root.geometry('200x250')
root.title(' Dice ')
dice_rotate = [PhotoImage(file=r'images\donghua.gif', format=f'gif -index {i}') for i in range(13)]
dice = [PhotoImage(file=f'images/{i}.png') for i in range(1,7)]
i = 0
def rotate():
global i
if i<13:
cv.itemconfig(image1,image=dice_rotate[i])
i += 1
root.after(50,rotate)
else:
cv.itemconfig(image1,image=dice[random.randint(0,5)])
i = 0
cv = Canvas(root, bg='white')
img = PhotoImage(file=r'images\1.png')
image1 = cv.create_image(100,100,image=img)
btn = Button(root, text=' Dice ',command=rotate)
cv.create_window(100,200,window=btn)
cv.pack()
root.mainloop()

analysis

The difficulty of implementation mainly lies in how to tkinter Play on GIF Animation .

We all know GIF It is actually a combination of static pictures frame by frame , And then show it at a certain playback speed , It becomes an animation that people can see with the naked eye . But use tkinter Of PhotoImage Method , It can only be a static picture of a certain frame . So we have to use index Calling a GIF Every picture in the , Then repeat the calling procedure at a certain speed .

First , Find what we want to play GIF picture , For example, in this case, the dice roll GIF, By editing software , You can see that it is actually made of 13 Composed of static pictures , The index of the picture is from 0 Start , from 0 To 12.

So we can first create a list using a derivation , Used to save static pictures of each frame :

dice_rotate = [PhotoImage(file=r'images\donghua.gif', format=f'gif -index {i}') for i in range(13)]

How to be in tkinter Create canvas on Canvas And buttons Button Not much here , You can refer to the article before asking brother .

Since we want to achieve the automatic rotation of dice by pressing the button , You must create a callback function , And tie it to Button On . In this callback function rotate() in , We have to put... In order 13 Zhang GIF After playing the still picture of , Then show the dice at random 1 To 6 Pictures of the , Indicates the number of points after the dice stop .

We use counters and root.after() Function to implement GIF Play in sequence .root.after(50, rotate) Two arguments in the function , The first one means 50 millisecond , The second means rotate function . This code means to set 50 Execute again in milliseconds rotate function ( It's not recursion ). So we define a global variable i As a counter , from 0 Start . Each run rotate When , Check i Is it less than 13, Because we only have 0 To 12 this 13 A still picture . Then, while showing the picture , Counter accumulation .rotate perform 13 All over , That is the GIF After all the pictures are shown ,i >=12, It doesn't work anymore after function , But show a static picture of dice , Used to indicate the result of rolling dice , Finally, set the counter to zero .

i = 0
def rotate():
global i # Global variable counter
if i<12:
cv.itemconfig(image1,image=dice_rotate[i]) # loop “ Play ”GIF Pictures of the
i += 1
root.after(50,rotate) # 50 Execute again in milliseconds rotate function
else:
cv.itemconfig(image1,image=dice[random.randint(0,5)]) # The result of rolling dice
i = 0 # The counter goes to zero 

Of course , Finally, you need to show a static diagram of the dice , Indicates the result of the dice ,1 To 6 The dice static graph of is defined in another picture list at the beginning .

dice = [PhotoImage(file=f'images/{i}.png') for i in range(1,7)]

So at the end of the program, you only need to use a random number to generate 0 To 5 The number of , Represents the sequence of pictures in the list .

such , Can be realized in tkinter The effect of rolling dice .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved