最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python tkinter制作單機五子棋游戲

 更新時間:2020年09月14日 09:15:25   作者:松鼠愛出餅干  
這篇文章主要介紹了Python tkinter制作單機五子棋游戲,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

本文的文字及圖片來源于網(wǎng)絡(luò),僅供學(xué)習(xí)、交流使用,不具有任何商業(yè)用途,版權(quán)歸原作者所有,如有問題請及時聯(lián)系我們以作處理。

以下文章來源于Python家庭,作者Python家庭

實戰(zhàn)項目:使用Python編寫一個能夠完成基本對戰(zhàn)的五子棋游戲。面向新手。

程序主要包括兩個部分,圖形創(chuàng)建與邏輯編寫兩部分。

程序的運行結(jié)果:

樣式創(chuàng)建

老規(guī)矩,先把用到的包導(dǎo)入進來。

from tkinter import *
import math

然后建立一個樣式的類,類名稱chessBoard。這里加了很多注釋,避免新手看不懂函數(shù)的作用,說實話我覺得挺別扭的。

#定義棋盤類
class chessBoard() :
  def __init__(self) :
   #創(chuàng)建一個tk對象,即窗口
    self.window = Tk()
    #窗口命名
    self.window.title("五子棋游戲")
    #定義窗口大小
    self.window.geometry("660x470")
    #定義窗口不可放縮
    self.window.resizable(0,0)
    #定義窗口里的畫布
    self.canvas=Canvas(self.window , bg="#EEE8AC" , width=470, height=470)
    #畫出畫布內(nèi)容
    self.paint_board()
    #定義畫布所在的網(wǎng)格
    self.canvas.grid(row = 0 , column = 0)

  def paint_board(self) :
   #畫橫線
    for row in range(0,15) :
      if row == 0 or row == 14 :
        self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 2)
      else :
        self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 1)
    
    #畫豎線
    for column in range(0,15) :
      if column == 0 or column == 14 :
        self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 ,width = 2)
      else :
        self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 , width = 1)
    
    #畫圓
    self.canvas.create_oval(112, 112, 118, 118, fill="black")
    self.canvas.create_oval(352, 112, 358, 118, fill="black")
    self.canvas.create_oval(112, 352, 118, 358, fill="black")
    self.canvas.create_oval(232, 232, 238, 238, fill="black")
    self.canvas.create_oval(352, 352, 358, 358, fill="black")

邏輯編寫

這里的主要看每個函數(shù)的功能就好了。

if __name__ == "__main__":  game = Gobang()

最后,main函數(shù)

if __name__ == "__main__":
  game = Gobang()

將以上的所有程序復(fù)制粘貼,即為完整的程序了,可以運行。
最后來一個完整程序,一個一個復(fù)制粘貼簡直不要太麻煩。

from tkinter import *
import math

#定義棋盤類
class chessBoard() :
  def __init__(self) :
    self.window = Tk()
    self.window.title("五子棋游戲")
    self.window.geometry("660x470")
    self.window.resizable(0,0)
    self.canvas=Canvas(self.window , bg="#EEE8AC" , width=470, height=470)
    self.paint_board()
    self.canvas.grid(row = 0 , column = 0)

  def paint_board(self) :
    for row in range(0,15) :
      if row == 0 or row == 14 :
        self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 2)
      else :
        self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 1)
    for column in range(0,15) :
      if column == 0 or column == 14 :
        self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 ,width = 2)
      else :
        self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 , width = 1)
      
    self.canvas.create_oval(112, 112, 118, 118, fill="black")
    self.canvas.create_oval(352, 112, 358, 118, fill="black")
    self.canvas.create_oval(112, 352, 118, 358, fill="black")
    self.canvas.create_oval(232, 232, 238, 238, fill="black")
    self.canvas.create_oval(352, 352, 358, 358, fill="black")

#定義五子棋游戲類
#0為黑子 , 1為白子 , 2為空位
class Gobang() :
  #初始化
  def __init__(self) :
    self.board = chessBoard()
    self.game_print = StringVar()
    self.game_print.set("")
    #16*16的二維列表,保證不會out of index
    self.db = [([2] * 16) for i in range(16)]
    #悔棋用的順序列表
    self.order = []
    #棋子顏色
    self.color_count = 0
    self.color = 'black'
    #清空與贏的初始化,已贏為1,已清空為1
    self.flag_win = 1
    self.flag_empty = 1
    self.options()
    
   
  #黑白互換
  def change_color(self) :
    self.color_count = (self.color_count + 1 ) % 2
    if self.color_count == 0 :
      self.color = "black"
    elif self.color_count ==1 :
      self.color = "white"
  
  #落子
  def chess_moving(self ,event) :
    #不點擊“開始”與“清空”無法再次開始落子
    if self.flag_win ==1 or self.flag_empty ==0 :
      return
    #坐標轉(zhuǎn)化為下標
    x,y = event.x-25 , event.y-25
    x = round(x/30)
    y = round(y/30)
    #點擊位置沒用落子,且沒有在棋盤線外,可以落子
    while self.db[y][x] == 2 and self.limit_boarder(y,x):
      self.db[y][x] = self.color_count
      self.order.append(x+15*y)
      self.board.canvas.create_oval(25+30*x-12 , 25+30*y-12 , 25+30*x+12 , 25+30*y+12 , fill = self.color,tags = "chessman")
      if self.game_win(y,x,self.color_count) :
        print(self.color,"獲勝")
        self.game_print.set(self.color+"獲勝")
      else :
        self.change_color()
        self.game_print.set("請"+self.color+"落子")
  

  #保證棋子落在棋盤上
  def limit_boarder(self , y , x) :
    if x<0 or x>14 or y<0 or y>14 :
      return False
    else :
      return True

  #計算連子的數(shù)目,并返回最大連子數(shù)目
  def chessman_count(self , y , x , color_count ) :
    count1,count2,count3,count4 = 1,1,1,1
    #橫計算
    for i in range(-1 , -5 , -1) :
      if self.db[y][x+i] == color_count :
        count1 += 1
      else:
        break
    for i in range(1 , 5 ,1 ) :
      if self.db[y][x+i] == color_count :
        count1 += 1
      else:
        break
    #豎計算
    for i in range(-1 , -5 , -1) :
      if self.db[y+i][x] == color_count :
        count2 += 1
      else:
        break
    for i in range(1 , 5 ,1 ) :
      if self.db[y+i][x] == color_count :
        count2 += 1
      else:
        break
    #/計算
    for i in range(-1 , -5 , -1) :
      if self.db[y+i][x+i] == color_count :
        count3 += 1
      else:
        break
    for i in range(1 , 5 ,1 ) :
      if self.db[y+i][x+i] == color_count :
        count3 += 1
      else:
        break
    #\計算
    for i in range(-1 , -5 , -1) :
      if self.db[y+i][x-i] == color_count :
        count4 += 1
      else:
        break
    for i in range(1 , 5 ,1 ) :
      if self.db[y+i][x-i] == color_count :
        count4 += 1
      else:
        break
      
    return max(count1 , count2 , count3 , count4)

  #判斷輸贏
  def game_win(self , y , x , color_count ) :
    if self.chessman_count(y,x,color_count) >= 5 :
      self.flag_win = 1
      self.flag_empty = 0
      return True
    else :
      return False

  #悔棋,清空棋盤,再畫剩下的n-1個棋子
  def withdraw(self ) :
    if len(self.order)==0 or self.flag_win == 1:
      return
    self.board.canvas.delete("chessman")
    z = self.order.pop()
    x = z%15
    y = z//15
    self.db[y][x] = 2
    self.color_count = 1
    for i in self.order :
      ix = i%15
      iy = i//15
      self.change_color()
      self.board.canvas.create_oval(25+30*ix-12 , 25+30*iy-12 , 25+30*ix+12 , 25+30*iy+12 , fill = self.color,tags = "chessman")
    self.change_color()
    self.game_print.set("請"+self.color+"落子")
  
  #清空
  def empty_all(self) :
    self.board.canvas.delete("chessman")
    #還原初始化
    self.db = [([2] * 16) for i in range(16)]
    self.order = []
    self.color_count = 0
    self.color = 'black'
    self.flag_win = 1
    self.flag_empty = 1
    self.game_print.set("")

  #將self.flag_win置0才能在棋盤上落子
  def game_start(self) :
    #沒有清空棋子不能置0開始
    if self.flag_empty == 0:
      return
    self.flag_win = 0
    self.game_print.set("請"+self.color+"落子")

  def options(self) :
    self.board.canvas.bind("<Button-1>",self.chess_moving)
    Label(self.board.window , textvariable = self.game_print , font = ("Arial", 20) ).place(relx = 0, rely = 0 ,x = 495 , y = 200)
    Button(self.board.window , text= "開始游戲" ,command = self.game_start,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=15)
    Button(self.board.window , text= "我要悔棋" ,command = self.withdraw,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=60)
    Button(self.board.window , text= "清空棋局" ,command = self.empty_all,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=105)
    Button(self.board.window , text= "結(jié)束游戲" ,command = self.board.window.destroy,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=420)
    self.board.window.mainloop()
  
if __name__ == "__main__":
  game = Gobang()

到此這篇關(guān)于Python tkinter制作單機五子棋游戲的文章就介紹到這了,更多相關(guān)Python tkinter單機五子制作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python中mediapipe庫踩過的坑實戰(zhàn)記錄

    python中mediapipe庫踩過的坑實戰(zhàn)記錄

    MediaPipe是由google制作的開源的、跨平臺的機器學(xué)習(xí)框架,可以將一些模型部署到不同的平臺和設(shè)備上使用的同時,也能保住檢測速度,下面這篇文章主要給大家介紹了關(guān)于python中mediapipe庫踩過的坑的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • Python函數(shù)參數(shù)和注解的使用

    Python函數(shù)參數(shù)和注解的使用

    本文介紹了Python函數(shù)的四種參數(shù):定位參數(shù)、可變參數(shù)、默認值參數(shù)、關(guān)鍵字參數(shù),和第五種Python3新特性參數(shù):僅限關(guān)鍵字參數(shù)。函數(shù)注解是一種元數(shù)據(jù),存在__annotations__屬性中,備注函數(shù)的參數(shù)和返回值的類型,它只是個注解,Python不會做任何強制檢查。
    2021-06-06
  • Anaconda(miniconda)入門使用完全指南

    Anaconda(miniconda)入門使用完全指南

    Conda是一個管理版本和Python環(huán)境的工具,它使用起來非常容易,下面這篇文章主要給大家介紹了關(guān)于Anaconda(miniconda)入門使用的相關(guān)資料,文中介紹的非常詳細,需要的朋友可以參考下
    2023-02-02
  • Python??reduce()函數(shù)的用法示例代碼

    Python??reduce()函數(shù)的用法示例代碼

    reduce函數(shù)原本在python2中也是個內(nèi)置函數(shù),不過在python3中被移到functools模塊中,這篇文章主要介紹了Python reduce()函數(shù)的用法,需要的朋友可以參考下
    2023-05-05
  • jupyter notebook 使用過程中python莫名崩潰的原因及解決方式

    jupyter notebook 使用過程中python莫名崩潰的原因及解決方式

    這篇文章主要介紹了jupyter notebook 使用過程中python莫名崩潰的原因及解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Python+folium繪制精美地圖的示例詳解

    Python+folium繪制精美地圖的示例詳解

    folium是一個基于leaflet.js的python地圖庫,可以通過folium來操縱數(shù)據(jù),并將其可視化。本文將通過各種示例詳細講解如何利用folium繪制精美地圖,需要的可以參考一下
    2022-03-03
  • python使用Image處理圖片常用技巧分析

    python使用Image處理圖片常用技巧分析

    這篇文章主要介紹了python使用Image處理圖片的常用技巧,實例分析了Python使用image處理圖片過程中改變圖片大小、圖片類型及遠程圖片中常見問題與解決方法,需要的朋友可以參考下
    2015-06-06
  • python中struct模塊之字節(jié)型數(shù)據(jù)的處理方法

    python中struct模塊之字節(jié)型數(shù)據(jù)的處理方法

    今天小編就為大家分享一篇python中struct模塊之字節(jié)型數(shù)據(jù)的處理方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • keras的get_value運行越來越慢的解決方案

    keras的get_value運行越來越慢的解決方案

    這篇文章主要介紹了keras的get_value運行越來越慢的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python使用execute_script模擬鼠標滾動、鼠標點擊等示例

    Python使用execute_script模擬鼠標滾動、鼠標點擊等示例

    文章介紹了Python使用Selenium執(zhí)行JavaScript來繞過網(wǎng)站對爬蟲的限制,包括模擬點擊、攔截彈出窗口、創(chuàng)建并派發(fā)點擊事件、模擬鼠標懸停后點擊和滾動到元素并點擊等方法
    2025-02-02

最新評論

海林市| 大余县| 嘉义县| 湖南省| 青浦区| 安乡县| 介休市| 彝良县| 武胜县| 老河口市| 合江县| 金乡县| 宜良县| 新河县| 苏尼特左旗| 秀山| 石屏县| 南城县| 临泉县| 杭州市| 普宁市| 德清县| 建始县| 嘉峪关市| 仁寿县| 射洪县| 兴仁县| 梅州市| 绵竹市| 永昌县| 永修县| 济宁市| 嘉荫县| 衡阳县| 绍兴县| 北川| 开江县| 福清市| 哈密市| 溆浦县| 石家庄市|