萝卜头IT论坛

搜索
查看: 1405|回复: 5
收起左侧

井字棋 小游戏

[复制链接]
发表于 2022-9-21 20:50:18 | 显示全部楼层 |阅读模式
本帖最后由 电脑时间 于 2022-9-27 21:24 编辑

改进版已推出,前往
使用PyCharm制作
使用Pyinstaller打包
属性:
Snipaste_2022-09-21_20-29-59.png
玩家1胜利:
Snipaste_2022-09-21_20-32-03.png
平局:(玩家2就不举了,跟玩家一的一模一样)

源代码:
  1. from tkinter import *
  2. import tkinter.messagebox as msg

  3. root = Tk()
  4. root.title('井字棋-By 萝卜头IT论坛 电脑时间')
  5. # labels
  6. Label(root, text="玩家1 : X", font="times 15").grid(row=0, column=1)
  7. Label(root, text="玩家2 : O", font="times 15").grid(row=0, column=2)

  8. digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]

  9. # for player1 sign = X and for player2 sign= Y
  10. mark = ''

  11. # counting the no. of click
  12. count = 0

  13. panels = ["panel"] * 10


  14. def win(panels, sign):
  15.     return ((panels[1] == panels[2] == panels[3] == sign)
  16.             or (panels[1] == panels[4] == panels[7] == sign)
  17.             or (panels[1] == panels[5] == panels[9] == sign)
  18.             or (panels[2] == panels[5] == panels[8] == sign)
  19.             or (panels[3] == panels[6] == panels[9] == sign)
  20.             or (panels[3] == panels[5] == panels[7] == sign)
  21.             or (panels[4] == panels[5] == panels[6] == sign)
  22.             or (panels[7] == panels[8] == panels[9] == sign))


  23. def checker(digit):
  24.     global count, mark, digits

  25.     # Check which button clicked

  26.     if digit == 1 and digit in digits:
  27.         digits.remove(digit)
  28.         ##player1 will play if the value of count is even and for odd player2 will play
  29.         if count % 2 == 0:
  30.             mark = 'X'
  31.             panels[digit] = mark
  32.         elif count % 2 != 0:
  33.             mark = 'O'
  34.             panels[digit] = mark

  35.         button1.config(text=mark)
  36.         count = count + 1
  37.         sign = mark

  38.         if (win(panels, sign) and sign == 'X'):
  39.             msg.showinfo("提示", "玩家1 胜利")
  40.             root.destroy()
  41.         elif (win(panels, sign) and sign == 'O'):
  42.             msg.showinfo("提示", "玩家2 胜利")
  43.             root.destroy()

  44.     if digit == 2 and digit in digits:
  45.         digits.remove(digit)

  46.         if count % 2 == 0:
  47.             mark = 'X'
  48.             panels[digit] = mark
  49.         elif count % 2 != 0:
  50.             mark = 'O'
  51.             panels[digit] = mark

  52.         button2.config(text=mark)
  53.         count = count + 1
  54.         sign = mark

  55.         if (win(panels, sign) and sign == 'X'):
  56.             msg.showinfo("提示", "玩家1 胜利")
  57.             root.destroy()
  58.         elif (win(panels, sign) and sign == 'O'):
  59.             msg.showinfo("提示", "玩家2 胜利")
  60.             root.destroy()

  61.     if digit == 3 and digit in digits:
  62.         digits.remove(digit)

  63.         if count % 2 == 0:
  64.             mark = 'X'
  65.             panels[digit] = mark
  66.         elif count % 2 != 0:
  67.             mark = 'O'
  68.             panels[digit] = mark

  69.         button3.config(text=mark)
  70.         count = count + 1
  71.         sign = mark

  72.         if (win(panels, sign) and sign == 'X'):
  73.             msg.showinfo("提示", "玩家1 胜利")
  74.             root.destroy()
  75.         elif (win(panels, sign) and sign == 'O'):
  76.             msg.showinfo("提示", "玩家2 胜利")
  77.             root.destroy()

  78.     if digit == 4 and digit in digits:
  79.         digits.remove(digit)

  80.         if count % 2 == 0:
  81.             mark = 'X'
  82.             panels[digit] = mark
  83.         elif count % 2 != 0:
  84.             mark = 'O'
  85.             panels[digit] = mark

  86.         button4.config(text=mark)
  87.         count = count + 1
  88.         sign = mark

  89.         if (win(panels, sign) and sign == 'X'):
  90.             msg.showinfo("提示", "玩家1 胜利")
  91.             root.destroy()
  92.         elif (win(panels, sign) and sign == 'O'):
  93.             msg.showinfo("提示", "玩家2 胜利")
  94.             root.destroy()

  95.     if digit == 5 and digit in digits:
  96.         digits.remove(digit)

  97.         if count % 2 == 0:
  98.             mark = 'X'
  99.             panels[digit] = mark
  100.         elif count % 2 != 0:
  101.             mark = 'O'
  102.             panels[digit] = mark

  103.         button5.config(text=mark)
  104.         count = count + 1
  105.         sign = mark

  106.         if (win(panels, sign) and sign == 'X'):
  107.             msg.showinfo("提示", "玩家1 胜利")
  108.             root.destroy()
  109.         elif (win(panels, sign) and sign == 'O'):
  110.             msg.showinfo("提示", "玩家2 胜利")
  111.             root.destroy()

  112.     if digit == 6 and digit in digits:
  113.         digits.remove(digit)

  114.         if count % 2 == 0:
  115.             mark = 'X'
  116.             panels[digit] = mark
  117.         elif count % 2 != 0:
  118.             mark = 'O'
  119.             panels[digit] = mark

  120.         button6.config(text=mark)
  121.         count = count + 1
  122.         sign = mark

  123.         if (win(panels, sign) and sign == 'X'):
  124.             msg.showinfo("提示", "玩家1 胜利")
  125.             root.destroy()
  126.         elif (win(panels, sign) and sign == 'O'):
  127.             msg.showinfo("提示", "玩家2 胜利")
  128.             root.destroy()

  129.     if digit == 7 and digit in digits:
  130.         digits.remove(digit)

  131.         if count % 2 == 0:
  132.             mark = 'X'
  133.             panels[digit] = mark
  134.         elif count % 2 != 0:
  135.             mark = 'O'
  136.             panels[digit] = mark

  137.         button7.config(text=mark)
  138.         count = count + 1
  139.         sign = mark

  140.         if (win(panels, sign) and sign == 'X'):
  141.             msg.showinfo("提示", "玩家1 胜利")
  142.             root.destroy()
  143.         elif (win(panels, sign) and sign == 'O'):
  144.             msg.showinfo("提示", "玩家2 胜利")
  145.             root.destroy()

  146.     if digit == 8 and digit in digits:
  147.         digits.remove(digit)

  148.         if count % 2 == 0:
  149.             mark = 'X'
  150.             panels[digit] = mark
  151.         elif count % 2 != 0:
  152.             mark = 'O'
  153.             panels[digit] = mark

  154.         button8.config(text=mark)
  155.         count = count + 1
  156.         sign = mark

  157.         if (win(panels, sign) and sign == 'X'):
  158.             msg.showinfo("提示", "玩家1 胜利")
  159.             root.destroy()
  160.         elif (win(panels, sign) and sign == 'O'):
  161.             msg.showinfo("提示", "玩家2 胜利")
  162.             root.destroy()

  163.     if digit == 9 and digit in digits:
  164.         digits.remove(digit)

  165.         if count % 2 == 0:
  166.             mark = 'X'
  167.             panels[digit] = mark
  168.         elif count % 2 != 0:
  169.             mark = 'O'
  170.             panels[digit] = mark

  171.         button9.config(text=mark)
  172.         count = count + 1
  173.         sign = mark

  174.         if (win(panels, sign) and sign == 'X'):
  175.             msg.showinfo("提示", "玩家1 胜利")
  176.             root.destroy()
  177.         elif (win(panels, sign) and sign == 'O'):
  178.             msg.showinfo("提示", "玩家2 胜利")
  179.             root.destroy()

  180.     ###if count is greater then 8 then the match has been tied
  181.     if (count > 8 and win(panels, 'X') == False and win(panels, 'O') == False):
  182.         msg.showinfo("提示", "平局")
  183.         root.destroy()


  184. ####define buttons
  185. button1 = Button(root, width=15, font=('Times 16 bold'), height=7, command=lambda: checker(1))
  186. button1.grid(row=1, column=1)
  187. button2 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(2))
  188. button2.grid(row=1, column=2)
  189. button3 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(3))
  190. button3.grid(row=1, column=3)
  191. button4 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(4))
  192. button4.grid(row=2, column=1)
  193. button5 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(5))
  194. button5.grid(row=2, column=2)
  195. button6 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(6))
  196. button6.grid(row=2, column=3)
  197. button7 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(7))
  198. button7.grid(row=3, column=1)
  199. button8 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(8))
  200. button8.grid(row=3, column=2)
  201. button9 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(9))
  202. button9.grid(row=3, column=3)

  203. root.mainloop()
复制代码

这个代码敲了我两个小时 不知道挨了男女混合双打多少次
井字棋-By 萝卜头IT论坛 电脑时间.py (7.76 KB, 下载次数: 72)
传不了EXE

回复

使用道具 举报

 楼主| 发表于 2022-9-22 06:58:45 | 显示全部楼层
爱电脑的昕宇 发表于 2022-9-22 06:55
这个真的好厉害,你是自学的吗?
虽然我的水平无法完全读懂你的代码,但有一个小建议:按钮可以用place方法 ...

已经是我的最高水平了
回复

使用道具 举报

发表于 2022-9-21 23:31:10 来自手机 | 显示全部楼层
很厉害了 我都不会没那个脑子 hhh
回复

使用道具 举报

发表于 2022-9-22 06:55:03 | 显示全部楼层
这个真的好厉害,你是自学的吗?
虽然我的水平无法完全读懂你的代码,但有一个小建议:按钮可以用place方法打包,或者可以加一句root.resizable(0, 0),可以避免出现这种情况
image.png
回复

使用道具 举报

发表于 2022-9-22 08:45:28 | 显示全部楼层
咱们论坛的小朋友都是敲代码高手,佩服
回复

使用道具 举报

发表于 2022-9-22 19:43:54 | 显示全部楼层
小朋友诚会玩,加油!!
回复

使用道具 举报

联系我们(Contact)|手机版|萝卜头IT论坛 ( 苏ICP备15050961号-1 )

GMT+8, 2024-4-20 11:38 , Processed in 0.090734 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表