Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python to realize tic-tac-toe chess Mini Game with interface

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use python to achieve tic-tac-toe chess Mini Game with interface, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Without saying much, follow the editor to have a look.

Today we use python+tkinter to install tic-tac-toe with an interface, and the effect is shown in the figure.

Tkinter is the standard GUI library for Python. Python uses Tkinter to quickly create GUI applications. Because Tkinter is built into the python installation package, as long as you install Python, you can import Tkinter libraries, and IDLE is also written in Tkinter, for a simple graphical interface Tkinter can still handle well.

Pip install tkinter1, first install tkinterroot= Tk () root.title ('tic-tac-toe') digits =] mark =''"count = 0panels = [" panel "] * 102,initialization window Label (root,text=" player1: X ", font=" times 15 ") .grid (row=0,column=1) Label (root,text=" player2: O ", font=" times 15 ") .grid (row=0,column=2) button1=Button (root,width=15,font= ('Times 16 bold'), height=7) Command=lambda:checker (1)) button1.grid (row=1,column=1) button2=Button (root,width=15,height=7,font= ('Times 16 bold'), command=lambda:checker (2)) button2.grid (row=1,column=2) button3=Button (root,width=15,height=7,font= (' Times 16 bold'), command=lambda:checker (3) button3.grid (row=1,column=3) button4=Button (root,width=15,height=7,font= ('Times 16 bold'), command=lambda:checker (4) button4.grid (row=2,column=1) button5=Button (root,width=15,height=7,font= (' Times 16 bold')) Command=lambda: checker (5) button5.grid (row=2,column=2) button6=Button (root,width=15,height=7,font= ('Times 16 bold'), command=lambda: checker (6) button6.grid (row=2,column=3) button7=Button (root,width=15,height=7,font= (' Times 16 bold'), command=lambda: checker (7) button7.grid (row=3,column=1) button8=Button (root,width=15,height=7,font= ('Times 16 bold'), command=lambda: checker (8) button8.grid (row=3,column=2) button9=Button (root,width=15,height=7,font= (' Times 16 bold')) Command=lambda: checker (9) button9.grid (row=3,column=3) root.mainloop () 3, define button

It is obvious that the tic-tac-toe chess above is nine buttons. Nine buttons are defined here.

Def win (panels Sign): return ((panels [1] = = panels [2] = = panels [3] = = sign) or (panels [1] = = panels [4] = panels [7] = = sign) or (panels [1] = panels [5] = = panels [9] = sign) or (panels [2] = = panels [5] = panels [8] = sign) or (panels [3] = panels [6] = panels [9] = sign) or (panels [3] = panels [5] = = panels [7]) = = sign) or (panels [4] = = panels [5] = = panels [6] = = sign) or (panels [7] = = panels [8] = = panels [9] = = sign) 4, Check the conditions for winning

Check the conditions for winning. One of the above situations is the winner.

Def checker (digit): global count, mark, digits if digit==1 and digit in digits: digits.remove (digit) if count%2==0: mark ='X' panels [digit] = mark elif count% 2 accounts 0: mark = 'O'panels [digit] = mar button1.config (text = mark) count = count+1 sign= mark if (win (panels,sign) and sign=='X'): msg.showinfo ("Result", "Player1 wins") root.destroy () elif (win (panels) Sign) and sign=='O'): msg.showinfo ("Result", "Player2 wins") root.destroy ()

Finally, here is to click to play chess, and then use the win function to determine whether to end the game and win the game.

The above is how to use python to achieve tic-tac-toe chess Mini Game with interface, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report