py用gui界面实现21点数字游戏
时间: 2024-02-01 11:15:46 浏览: 31
下面是一个使用Python的Tkinter库实现的21点数字游戏GUI界面的示例代码:
```python
import tkinter as tk
import random
class Game:
def __init__(self, master):
self.master = master
master.title("21点数字游戏")
self.cards = [i for i in range(1,11)] * 4
self.score = 0
self.dealer_score = 0
self.deal()
self.label = tk.Label(master, text="得分: {}".format(self.score))
self.label.pack()
self.hit_button = tk.Button(master, text="要牌", command=self.hit)
self.hit_button.pack(side="left")
self.stand_button = tk.Button(master, text="停牌", command=self.stand)
self.stand_button.pack(side="left")
def deal(self):
self.hand = []
self.dealer_hand = []
self.score = 0
self.dealer_score = 0
random.shuffle(self.cards)
self.hand.append(self.cards.pop())
self.dealer_hand.append(self.cards.pop())
self.hand.append(self.cards.pop())
self.dealer_hand.append(self.cards.pop())
self.score = self.calculate_score(self.hand)
self.dealer_score = self.calculate_score(self.dealer_hand)
print(self.hand)
print(self.dealer_hand)
def hit(self):
self.hand.append(self.cards.pop())
self.score = self.calculate_score(self.hand)
self.label.config(text="得分: {}".format(self.score))
if self.score > 21:
self.label.config(text="你输了!")
self.hit_button.config(state="disabled")
self.stand_button.config(state="disabled")
def stand(self):
while self.dealer_score < 17:
self.dealer_hand.append(self.cards.pop())
self.dealer_score = self.calculate_score(self.dealer_hand)
if self.dealer_score > 21 or self.score > self.dealer_score:
self.label.config(text="你赢了!")
elif self.score < self.dealer_score:
self.label.config(text="你输了!")
else:
self.label.config(text="平局!")
self.hit_button.config(state="disabled")
self.stand_button.config(state="disabled")
def calculate_score(self, hand):
score = 0
for card in hand:
if card == 1 and score + 11 <= 21:
score += 11
elif card == 1:
score += 1
elif card in [11, 12, 13]:
score += 10
else:
score += card
return score
root = tk.Tk()
game = Game(root)
root.mainloop()
```
这个代码创建了一个名为`Game`的类,它包含了游戏的逻辑和GUI界面。在类的初始化方法中,它设置了窗口标题,创建了卡牌和分数,并调用了`deal`方法来发牌。
在GUI界面中,它创建了一个标签来显示玩家的分数,并创建了两个按钮:一个是“要牌”按钮,另一个是“停牌”按钮。当玩家点击“要牌”按钮时,`hit`方法会被调用,它会从牌堆中抽取一张牌并计算分数。当玩家的分数超过21时,游戏结束。
当玩家点击“停牌”按钮时,`stand`方法会被调用。它会让庄家一直要牌直到分数达到17或更高。然后,它会比较玩家和庄家的分数,根据结果更新标签上的文本。
最后,代码创建一个Tkinter窗口并将Game类的实例添加到窗口中。当窗口被关闭时,事件循环将终止。
相关推荐
![tar](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)