婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > python基于tkinter制作圖形界面的2048游戲

python基于tkinter制作圖形界面的2048游戲

熱門標簽:廣東旅游地圖標注 電話外呼系統招商代理 京華圖書館地圖標注 電話機器人貸款詐騙 打印谷歌地圖標注 淮安呼叫中心外呼系統如何 佛山通用400電話申請 蘇州人工外呼系統軟件 看懂地圖標注方法

2048游戲輸出

項目先決條件

前提條件如下:

1. Python
2. Tkinter

創建main.py

代碼:

from tkinter import *
from tkinter import messagebox
import random

class Board:
 bg_color={

 '2': '#eee4da',
 '4': '#ede0c8',
 '8': '#edc850',
 '16': '#edc53f',
 '32': '#f67c5f',
 '64': '#f65e3b',
 '128': '#edcf72',
 '256': '#edcc61',
 '512': '#f2b179',
 '1024': '#f59563',
 '2048': '#edc22e',
 }
 color={
  '2': '#776e65',
 '4': '#f9f6f2',
 '8': '#f9f6f2',
 '16': '#f9f6f2',
 '32': '#f9f6f2',
 '64': '#f9f6f2',
 '128': '#f9f6f2',
 '256': '#f9f6f2',
 '512': '#776e65',
 '1024': '#f9f6f2',
 '2048': '#f9f6f2',
 }

 def __init__(self):
 self.window=Tk()
 self.window.title('ProjectGurukul 2048 Game')
 self.gameArea=Frame(self.window,bg= 'azure3')
 self.board=[]
 self.gridCell=[[0]*4 for i in range(4)]
 self.compress=False
 self.merge=False
 self.moved=False
 self.score=0

 for i in range(4):
  rows=[]
  for j in range(4):
  l=Label(self.gameArea,text='',bg='azure4',
  font=('arial',22,'bold'),width=4,height=2)
  l.grid(row=i,column=j,padx=7,pady=7)

  rows.append(l)
  self.board.append(rows)
 self.gameArea.grid()

 def reverse(self):
 for ind in range(4):
  i=0
  j=3
  while(ij):
  self.gridCell[ind][i],self.gridCell[ind][j]=self.gridCell[ind][j],self.gridCell[ind][i]
  i+=1
  j-=1

 def transpose(self):
 self.gridCell=[list(t)for t in zip(*self.gridCell)]

 def compressGrid(self):
 self.compress=False
 temp=[[0] *4 for i in range(4)]
 for i in range(4):
  cnt=0
  for j in range(4):
  if self.gridCell[i][j]!=0:
   temp[i][cnt]=self.gridCell[i][j]
   if cnt!=j:
   self.compress=True
   cnt+=1
 self.gridCell=temp

 def mergeGrid(self):
 self.merge=False
 for i in range(4):
  for j in range(4 - 1):
  if self.gridCell[i][j] == self.gridCell[i][j + 1] and self.gridCell[i][j] != 0:
   self.gridCell[i][j] *= 2
   self.gridCell[i][j + 1] = 0
   self.score += self.gridCell[i][j]
   self.merge = True

 def random_cell(self):
 cells=[]
 for i in range(4):
  for j in range(4):
  if self.gridCell[i][j] == 0:
   cells.append((i, j))
 curr=random.choice(cells)
 i=curr[0]
 j=curr[1]
 self.gridCell[i][j]=2
 
 def can_merge(self):
 for i in range(4):
  for j in range(3):
  if self.gridCell[i][j] == self.gridCell[i][j+1]:
   return True
 
 for i in range(3):
  for j in range(4):
  if self.gridCell[i+1][j] == self.gridCell[i][j]:
   return True
 return False

 def paintGrid(self):
 for i in range(4):
  for j in range(4):
  if self.gridCell[i][j]==0:
   self.board[i][j].config(text='',bg='azure4')
  else:
   self.board[i][j].config(text=str(self.gridCell[i][j]),
   bg=self.bg_color.get(str(self.gridCell[i][j])),
   fg=self.color.get(str(self.gridCell[i][j])))


class Game:
 def __init__(self,gamepanel):
 self.gamepanel=gamepanel
 self.end=False
 self.won=False

 def start(self):
 self.gamepanel.random_cell()
 self.gamepanel.random_cell()
 self.gamepanel.paintGrid()
 self.gamepanel.window.bind('Key>', self.link_keys)
 self.gamepanel.window.mainloop()
 
 def link_keys(self,event):
 if self.end or self.won:
  return

 self.gamepanel.compress = False
 self.gamepanel.merge = False
 self.gamepanel.moved = False

 presed_key=event.keysym

 if presed_key=='Up':
  self.gamepanel.transpose()
  self.gamepanel.compressGrid()
  self.gamepanel.mergeGrid()
  self.gamepanel.moved = self.gamepanel.compress or self.gamepanel.merge
  self.gamepanel.compressGrid()
  self.gamepanel.transpose()

 elif presed_key=='Down':
  self.gamepanel.transpose()
  self.gamepanel.reverse()
  self.gamepanel.compressGrid()
  self.gamepanel.mergeGrid()
  self.gamepanel.moved = self.gamepanel.compress or self.gamepanel.merge
  self.gamepanel.compressGrid()
  self.gamepanel.reverse()
  self.gamepanel.transpose()

 elif presed_key=='Left':
  self.gamepanel.compressGrid()
  self.gamepanel.mergeGrid()
  self.gamepanel.moved = self.gamepanel.compress or self.gamepanel.merge
  self.gamepanel.compressGrid()

 elif presed_key=='Right':
  self.gamepanel.reverse()
  self.gamepanel.compressGrid()
  self.gamepanel.mergeGrid()
  self.gamepanel.moved = self.gamepanel.compress or self.gamepanel.merge
  self.gamepanel.compressGrid()
  self.gamepanel.reverse()
 else:
  pass

 self.gamepanel.paintGrid()
 print(self.gamepanel.score)

 flag=0
 for i in range(4):
  for j in range(4):
  if(self.gamepanel.gridCell[i][j]==2048):
   flag=1
   break

 if(flag==1): #found 2048
  self.won=True
  messagebox.showinfo('2048', message='You Wonnn!!')
  print("won")
  return

 for i in range(4):
  for j in range(4):
  if self.gamepanel.gridCell[i][j]==0:
   flag=1
   break

 if not (flag or self.gamepanel.can_merge()):
  self.end=True
  messagebox.showinfo('2048','Game Over!!!')
  print("Over")

 if self.gamepanel.moved:
  self.gamepanel.random_cell()
 
 self.gamepanel.paintGrid()
 

gamepanel =Board()
game2048 = Game( gamepanel)
game2048.start()

解釋:

我們在代碼中定義了兩個類:

1.Board:

變量:

  • Bg_color:這是一個字典,用于存儲每個單元格的背景色。
  • Color:這是一個字典,用于存儲每個單元的前景色。
  • Window:它是tkinter的主要窗口。
  • gameArea:這是一個tkinter框架小部件。
  • gridCell:這是一個4×4整數矩陣,存儲所有單元格的實際整數值。
  • Board:這是tkinter標簽小部件的4×4網格,它在tkinter窗口上顯示單元格的值。它還用于根據其gridCell值配置該單元格的背景和前景。
  • Score:它存儲玩家的當前分數。

其余只是標志變量。

功能:

  • __init __(self):這是構造函數。它使用適當的默認值初始化所有變量,例如gridCell的默認值為“ 0”,移動,合并的默認值為False,等等。
  • Reverse:反轉gridCell矩陣。
  • Transpose:它使用zip函數并進行gridCell矩陣的轉置。
  • CompressGrid:它將所有非空單元格向左移動,因此可以輕松完成合并。
  • mergeGrid:如果兩個相鄰單元格具有相同的gridCell值,則將它們的gridCell值相加。
  • Random_cell:首先將所有空單元格存儲在列表中,然后從創建的列表中選擇一個隨機單元格并使其gridCell值2
  • Can_merge:返回一個布爾值,表示我們可以合并任意兩個單元格。當且僅當兩個單元格具有相同的gridCell值時,我們才可以合并它們。
  • paintGrid:將前景和背景色分配給4×4網格中與其gridCell值相對應的每個單元。

2.game:

此類沒有很多變量,只有一些布爾變量指示游戲狀態。

功能:

  • __init __(self):這是構造函數。它使用適當的默認值初始化所有變量。
  • 開始:調用random_cell兩次,將'2'賦給兩個隨機單元格的gridCell值,然后繪制網格,然后,調用link_keys鏈接上,下,左和右鍵。
  • Link_keys:首先,它檢查游戲是贏還是輸,如果是,則不執行任何操作執行return語句。否則,它將繼續執行。

方法:

  • 對于左滑動,我們將先壓縮然后合并gridCell矩陣,然后如果compress或merge為true(指示矩陣的值受前兩個函數影響),那么我們需要再次壓縮網格。
  • 對于上移,我們將進行移調,然后向左輕掃,然后再次進行移調以返回原始順序。
  • 向下移動與向上移動相同,但是我們需要反轉矩陣。
  • 同樣,向右與向左+向后移動相同。
  • 每次操作后,我們需要檢查游戲狀態,如果所有單元都被占用,我們甚至不能合并任何兩個單元,即沒有動作可以改變矩陣的狀態,則游戲結束了。

如果任何一個單元格值都達到2048,則玩家將獲勝,并且屏幕上會閃爍一個消息框,宣布獲勝者。

總結

我們已經成功地用python開發了流行的2048游戲。開發游戲而不是玩別人的游戲非常有趣,現在我們將玩自己開發的游戲。

以上就是python基于tkinter制作圖形界面的2048游戲的詳細內容,更多關于python 圖形界面2048游戲的資料請關注腳本之家其它相關文章!

您可能感興趣的文章:
  • python基于tkinter制作無損音樂下載工具(附源碼)
  • python使用tkinter實現屏幕中間倒計時
  • Python使用tkinter實現小時鐘效果
  • Python tkinter實現日期選擇器
  • Python使用tkinter制作在線翻譯軟件
  • Python爬蟲+tkinter界面實現歷史天氣查詢的思路詳解
  • Python爬蟲+Tkinter制作一個翻譯軟件的示例
  • python tkinter實現下載進度條及抖音視頻去水印原理
  • 使用python tkinter開發一個爬取B站直播彈幕工具的實現代碼
  • python tkinter模塊的簡單使用

標簽:駐馬店 股票 衡水 呼和浩特 中山 畢節 湖州 江蘇

巨人網絡通訊聲明:本文標題《python基于tkinter制作圖形界面的2048游戲》,本文關鍵詞  python,基于,tkinter,制作,圖形,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《python基于tkinter制作圖形界面的2048游戲》相關的同類信息!
  • 本頁收集關于python基于tkinter制作圖形界面的2048游戲的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 武胜县| 绵竹市| 永福县| 绍兴县| 万盛区| 大石桥市| 赞皇县| 筠连县| 广元市| 麟游县| 桐梓县| 乐业县| 新昌县| 肇州县| 嫩江县| 鄂州市| 英山县| 黑龙江省| 瓦房店市| 丰顺县| 兴文县| 克拉玛依市| 福贡县| 穆棱市| 革吉县| 奎屯市| 宁河县| 邵东县| 安泽县| 江阴市| 营山县| 措勤县| 理塘县| 桂阳县| 德庆县| 富裕县| 遂宁市| 白山市| 随州市| 乾安县| 平原县|