井字棋 小游戏
本帖最后由 电脑时间 于 2022-9-27 21:24 编辑改进版已推出,前往
使用PyCharm制作
使用Pyinstaller打包
属性:
玩家1胜利:
平局:(玩家2就不举了,跟玩家一的一模一样)
源代码:
from tkinter import *
import tkinter.messagebox as msg
root = Tk()
root.title('井字棋-By 萝卜头IT论坛 电脑时间')
# labels
Label(root, text="玩家1 : X", font="times 15").grid(row=0, column=1)
Label(root, text="玩家2 : O", font="times 15").grid(row=0, column=2)
digits =
# for player1 sign = X and for player2 sign= Y
mark = ''
# counting the no. of click
count = 0
panels = ["panel"] * 10
def win(panels, sign):
return ((panels == panels == panels == sign)
or (panels == panels == panels == sign)
or (panels == panels == panels == sign)
or (panels == panels == panels == sign)
or (panels == panels == panels == sign)
or (panels == panels == panels == sign)
or (panels == panels == panels == sign)
or (panels == panels == panels == sign))
def checker(digit):
global count, mark, digits
# Check which button clicked
if digit == 1 and digit in digits:
digits.remove(digit)
##player1 will play if the value of count is even and for odd player2 will play
if count % 2 == 0:
mark = 'X'
panels = mark
elif count % 2 != 0:
mark = 'O'
panels = mark
button1.config(text=mark)
count = count + 1
sign = mark
if (win(panels, sign) and sign == 'X'):
msg.showinfo("提示", "玩家1 胜利")
root.destroy()
elif (win(panels, sign) and sign == 'O'):
msg.showinfo("提示", "玩家2 胜利")
root.destroy()
if digit == 2 and digit in digits:
digits.remove(digit)
if count % 2 == 0:
mark = 'X'
panels = mark
elif count % 2 != 0:
mark = 'O'
panels = mark
button2.config(text=mark)
count = count + 1
sign = mark
if (win(panels, sign) and sign == 'X'):
msg.showinfo("提示", "玩家1 胜利")
root.destroy()
elif (win(panels, sign) and sign == 'O'):
msg.showinfo("提示", "玩家2 胜利")
root.destroy()
if digit == 3 and digit in digits:
digits.remove(digit)
if count % 2 == 0:
mark = 'X'
panels = mark
elif count % 2 != 0:
mark = 'O'
panels = mark
button3.config(text=mark)
count = count + 1
sign = mark
if (win(panels, sign) and sign == 'X'):
msg.showinfo("提示", "玩家1 胜利")
root.destroy()
elif (win(panels, sign) and sign == 'O'):
msg.showinfo("提示", "玩家2 胜利")
root.destroy()
if digit == 4 and digit in digits:
digits.remove(digit)
if count % 2 == 0:
mark = 'X'
panels = mark
elif count % 2 != 0:
mark = 'O'
panels = mark
button4.config(text=mark)
count = count + 1
sign = mark
if (win(panels, sign) and sign == 'X'):
msg.showinfo("提示", "玩家1 胜利")
root.destroy()
elif (win(panels, sign) and sign == 'O'):
msg.showinfo("提示", "玩家2 胜利")
root.destroy()
if digit == 5 and digit in digits:
digits.remove(digit)
if count % 2 == 0:
mark = 'X'
panels = mark
elif count % 2 != 0:
mark = 'O'
panels = mark
button5.config(text=mark)
count = count + 1
sign = mark
if (win(panels, sign) and sign == 'X'):
msg.showinfo("提示", "玩家1 胜利")
root.destroy()
elif (win(panels, sign) and sign == 'O'):
msg.showinfo("提示", "玩家2 胜利")
root.destroy()
if digit == 6 and digit in digits:
digits.remove(digit)
if count % 2 == 0:
mark = 'X'
panels = mark
elif count % 2 != 0:
mark = 'O'
panels = mark
button6.config(text=mark)
count = count + 1
sign = mark
if (win(panels, sign) and sign == 'X'):
msg.showinfo("提示", "玩家1 胜利")
root.destroy()
elif (win(panels, sign) and sign == 'O'):
msg.showinfo("提示", "玩家2 胜利")
root.destroy()
if digit == 7 and digit in digits:
digits.remove(digit)
if count % 2 == 0:
mark = 'X'
panels = mark
elif count % 2 != 0:
mark = 'O'
panels = mark
button7.config(text=mark)
count = count + 1
sign = mark
if (win(panels, sign) and sign == 'X'):
msg.showinfo("提示", "玩家1 胜利")
root.destroy()
elif (win(panels, sign) and sign == 'O'):
msg.showinfo("提示", "玩家2 胜利")
root.destroy()
if digit == 8 and digit in digits:
digits.remove(digit)
if count % 2 == 0:
mark = 'X'
panels = mark
elif count % 2 != 0:
mark = 'O'
panels = mark
button8.config(text=mark)
count = count + 1
sign = mark
if (win(panels, sign) and sign == 'X'):
msg.showinfo("提示", "玩家1 胜利")
root.destroy()
elif (win(panels, sign) and sign == 'O'):
msg.showinfo("提示", "玩家2 胜利")
root.destroy()
if digit == 9 and digit in digits:
digits.remove(digit)
if count % 2 == 0:
mark = 'X'
panels = mark
elif count % 2 != 0:
mark = 'O'
panels = mark
button9.config(text=mark)
count = count + 1
sign = mark
if (win(panels, sign) and sign == 'X'):
msg.showinfo("提示", "玩家1 胜利")
root.destroy()
elif (win(panels, sign) and sign == 'O'):
msg.showinfo("提示", "玩家2 胜利")
root.destroy()
###if count is greater then 8 then the match has been tied
if (count > 8 and win(panels, 'X') == False and win(panels, 'O') == False):
msg.showinfo("提示", "平局")
root.destroy()
####define buttons
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()
这个代码敲了我两个小时 不知道挨了男女混合双打多少次{:04:}
传不了EXE
{:16:}
爱电脑的昕宇 发表于 2022-9-22 06:55
这个真的好厉害,你是自学的吗?
虽然我的水平无法完全读懂你的代码,但有一个小建议:按钮可以用place方法 ...
已经是我的最高水平了{:16:} 很厉害了 我都不会没那个脑子 hhh 这个真的好厉害,你是自学的吗?
虽然我的水平无法完全读懂你的代码,但有一个小建议:按钮可以用place方法打包,或者可以加一句root.resizable(0, 0),可以避免出现这种情况
咱们论坛的小朋友都是敲代码高手,佩服 小朋友诚会玩,加油!!
页:
[1]