py用gui界面实现21点数字游戏
时间: 2024-02-01 20:17:04 浏览: 56
好的,下面是一个简单的21点数字游戏的 GUI 界面的实现:
```python
import tkinter as tk
import random
class TwentyOneGame:
def __init__(self):
self.deck = self.get_new_deck()
self.player_cards = []
self.dealer_cards = []
self.player_score = 0
self.dealer_score = 0
def get_new_deck(self):
deck = []
for i in range(1, 14):
for j in range(4):
deck.append(i)
random.shuffle(deck)
return deck
def deal_cards(self):
self.player_cards = [self.deck.pop(), self.deck.pop()]
self.dealer_cards = [self.deck.pop(), self.deck.pop()]
self.calculate_scores()
def hit(self, player):
if player == 'player':
self.player_cards.append(self.deck.pop())
else:
self.dealer_cards.append(self.deck.pop())
self.calculate_scores()
def calculate_scores(self):
self.player_score = sum(self.player_cards)
self.dealer_score = sum(self.dealer_cards)
if self.player_score > 21:
for i in range(len(self.player_cards)):
if self.player_cards[i] == 11:
self.player_cards[i] = 1
self.player_score = sum(self.player_cards)
if self.player_score <= 21:
break
if self.dealer_score > 21:
for i in range(len(self.dealer_cards)):
if self.dealer_cards[i] == 11:
self.dealer_cards[i] = 1
self.dealer_score = sum(self.dealer_cards)
if self.dealer_score <= 21:
break
def get_winner(self):
if self.player_score > 21:
return 'dealer'
elif self.dealer_score > 21:
return 'player'
elif self.player_score > self.dealer_score:
return 'player'
elif self.dealer_score > self.player_score:
return 'dealer'
else:
return 'tie'
class TwentyOneGUI:
def __init__(self):
self.game = TwentyOneGame()
self.window = tk.Tk()
self.window.title('21点数字游戏')
self.window.geometry('400x300')
self.player_cards_label = tk.Label(self.window, text='玩家手牌: ')
self.player_cards_label.grid(row=0, column=0, padx=10, pady=10)
self.player_cards_value_label = tk.Label(self.window, text='')
self.player_cards_value_label.grid(row=0, column=1, padx=10, pady=10)
self.dealer_cards_label = tk.Label(self.window, text='庄家手牌: ')
self.dealer_cards_label.grid(row=1, column=0, padx=10, pady=10)
self.dealer_cards_value_label = tk.Label(self.window, text='')
self.dealer_cards_value_label.grid(row=1, column=1, padx=10, pady=10)
self.result_label = tk.Label(self.window, text='')
self.result_label.grid(row=3, column=0, columnspan=2, padx=10, pady=10)
self.hit_button = tk.Button(self.window, text='要牌', command=self.hit_player)
self.hit_button.grid(row=2, column=0, padx=10, pady=10)
self.stand_button = tk.Button(self.window, text='停牌', command=self.stand)
self.stand_button.grid(row=2, column=1, padx=10, pady=10)
self.new_game_button = tk.Button(self.window, text='新游戏', command=self.new_game)
self.new_game_button.grid(row=4, column=0, columnspan=2, padx=10, pady=10)
self.new_game()
def show_player_cards(self):
cards = ' '.join(str(card) for card in self.game.player_cards)
self.player_cards_value_label.config(text=cards)
def show_dealer_cards(self):
cards = ' '.join(str(card) for card in self.game.dealer_cards)
self.dealer_cards_value_label.config(text=cards)
def hit_player(self):
self.game.hit('player')
self.show_player_cards()
self.check_game_status()
def stand(self):
while self.game.dealer_score < 17:
self.game.hit('dealer')
self.show_dealer_cards()
self.check_game_status()
winner = self.game.get_winner()
if winner == 'player':
self.result_label.config(text='你赢了!')
elif winner == 'dealer':
self.result_label.config(text='庄家赢了!')
else:
self.result_label.config(text='平局!')
def new_game(self):
self.game = TwentyOneGame()
self.game.deal_cards()
self.show_player_cards()
self.show_dealer_cards()
self.result_label.config(text='')
def check_game_status(self):
if self.game.player_score > 21:
self.result_label.config(text='你输了!')
elif self.game.dealer_score > 21:
self.result_label.config(text='你赢了!')
TwentyOneGUI().window.mainloop()
```
这个 GUI 界面使用了 tkinter 模块,实现了玩家和庄家的牌的显示,以及要牌、停牌和新游戏等按钮的功能。你可以直接运行这个代码来查看效果。
阅读全文