資源簡介
python實現的GUI井字棋游戲,精美的開始界面及開始按鈕
游戲功能:
機器人:簡單的AI人工智能算法
玩家:鼠標操作

代碼片段和文件信息
import?tkinter?as?tk
from?tkinter?import?messagebox
class?Window(object):
????def?__init__(self?master):
????????self.master?=?master
????????self.game?=?Game()
????????self.can?=?None
????????self.start_game()
????def?start_game(self):
????????global?imag1?imag2
????????self.lf?=?tk.Labelframe(self.master)
????????self.lbl?=?tk.Label(self.lf?image=imag1)
????????self.lbl.pack()
????????self.btn?=?tk.Button(self.lf?image=imag2?command=self.create_board)
????????self.btn.place(x=95?y=140)
????????self.lf.pack()
????def?create_board(self):
????????global?imag3
????????if?self.lf:
????????????self.lf.destroy()
????????if?self.can:
????????????self.can.destroy()
????????self.game.over?=?False
????????self.game.board?=?[i?for?i?in?range(9)]
????????self.can?=?tk.Canvas(self.master?width=300?height=300)
????????self.can.create_line((30?30)?(270?30)?width=2)
????????self.can.create_line((270?30)?(270?270)?width=2)
????????self.can.create_line((270?270)?(30?270)?width=2)
????????self.can.create_line((30?270)?(30?30)?width=2)
????????for?i?in?range(3):
????????????self.can.create_line((30?(i?+?1)?*?80?+?30)?(270?(i?+?1)?*?80?+?30)?width=2)
????????for?i?in?range(3):
????????????self.can.create_line(((i?+?1)?*?80?+?30?30)?((i?+?1)?*?80?+?30?270)?width=2)
????????self.can.pack(expand=‘YES‘?fill=‘both‘)
????????self.can.bind(““?self.func)
????????self.btn1?=?tk.Button(self.master?text=‘重新開始‘?command=self.create_board)
????????self.btn1.place(x=120?y=273)
????def?func(self?event):
????????if?self.game.over:
????????????tk.messagebox.showinfo(‘提示‘?‘游戲已結束!‘)
????????????return
????????sum?=?0
????????flag?=?False
????????for?i?in?range(30?191?80):
????????????for?j?in?range(30?191?80):
????????????????sum?+=?1
????????????????if?j?<=?event.x?????????????????????self.can.create_oval((j?i)?(j?+?80?i?+?80)?width=2?outline=‘red‘)
????????????????????self.game.board[sum?-?1]?=?‘O‘
????????????????????flag?=?True
????????????????????break
????????????if?flag:
????????????????break
????????if?flag:
????????????if?self.game.isWinner(self.game.board):
????????????????tk.messagebox.showinfo(‘提示‘?‘玩家勝利!‘)
????????????????self.game.over?=?True
????????????elif?self.game.is_Tie():
????????????????tk.messagebox.showinfo(‘提示‘?‘平局!游戲結束‘)
????????????????self.game.over?=?True
????????????else:
????????????????self.robot_draw()
????def?robot_draw(self):
????????move?=?self.game.Rob_play()?+?1
????????sum?=?0
????????flag?=?False
????????for?i?in?range(30?191?80):
????????????for?j?in?range(30?191?80):
????????????????sum?+=?1
????????????????if?sum?==?move:
????????????????????self.can.create_line((j?i)?(j?+?80?i?+?80)?width=2?fill=‘blue‘)
????????????????????self.can.create_line((j?i?+?80)?(j?+?80?i)?width=2?fill=‘blue‘)
????????????????????self.game.board[sum?-?1]?=?‘X‘
??????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????11377??2020-07-07?19:02??GUI-井字棋\image\井字棋-開始游戲.png
?????文件??????23435??2020-07-07?18:59??GUI-井字棋\image\井字棋界面.png
?????文件???????5331??2020-07-20?10:23??GUI-井字棋\Window.py
?????文件???????4823??2020-07-07?20:18??GUI-井字棋\__pycache__\Window.cpython-38.pyc
?????目錄??????????0??2020-07-20?10:18??GUI-井字棋\image
?????目錄??????????0??2020-07-07?20:18??GUI-井字棋\__pycache__
?????目錄??????????0??2020-07-20?10:23??GUI-井字棋
-----------?---------??----------?-----??----
????????????????44966????????????????????7
評論
共有 條評論