0
点赞
收藏
分享

微信扫一扫

Python实现“剪刀石头布”小游戏:从初级到高级

在程序设计的世界中,简单的游戏也能带来无限乐趣。剪刀石头布这个经典的游戏无处不在,现在我们将通过Python为其赋予生命。本文将带你从初级到高级逐步完善“剪刀石头布”小游戏,让你深入了解Python的逻辑控制、随机数生成和函数应用。

第一步:初级版游戏

让我们从一个简单的版本开始。在这个初级版本中,我们将直接在控制台上进行“剪刀石头布”游戏的交互。

import random

def game():
    choices = ["剪刀", "石头", "布"]
    computer_choice = random.choice(choices)
    
    player_choice = input("请选择:剪刀、石头或布:")
    
    print(f"计算机选择了:{computer_choice}")
    
    if player_choice in choices:
        if player_choice == computer_choice:
            print("平局!")
        elif (player_choice == "剪刀" and computer_choice == "布") or \
             (player_choice == "石头" and computer_choice == "剪刀") or \
             (player_choice == "布" and computer_choice == "石头"):
            print("你赢了!")
        else:
            print("你输了!")
    else:
        print("请做出有效选择!")

game()

第二步:进阶版游戏

在进阶版游戏中,我们将为游戏添加计分功能。玩家和计算机的得分将被记录下来,并在游戏结束时显示。

import random

def game():
    choices = ["剪刀", "石头", "布"]
    computer_choice = random.choice(choices)
    
    player_choice = input("请选择:剪刀、石头或布:")
    
    print(f"计算机选择了:{computer_choice}")
    
    if player_choice in choices:
        if player_choice == computer_choice:
            print("平局!")
        elif (player_choice == "剪刀" and computer_choice == "布") or \
             (player_choice == "石头" and computer_choice == "剪刀") or \
             (player_choice == "布" and computer_choice == "石头"):
            print("你赢了!")
        else:
            print("你输了!")
    else:
        print("请做出有效选择!")

def advanced_game():
    player_score = 0
    computer_score = 0
    rounds = 0
    
    while rounds < 3:  # 设定游戏回合数
        result = game()
        if "赢" in result:
            player_score += 1
        elif "输" in result:
            computer_score += 1
        rounds += 1
    
    print(f"游戏结束!你的得分:{player_score},计算机得分:{computer_score}")
    if player_score > computer_score:
        print("恭喜你获胜!")
    elif player_score < computer_score:
        print("很遗憾,你输了。")
    else:
        print("平局!")

advanced_game()

第三步:高级版游戏

在高级版游戏中,我们将引入图形用户界面(GUI)来增强游戏体验。使用Python的Tkinter模块,我们可以创建一个简单的GUI界面,让玩家通过点击按钮来选择。

import random
import tkinter as tk

def game(player_choice):
    choices = ["剪刀", "石头", "布"]
    computer_choice = random.choice(choices)
    
    result = ""
    
    if player_choice in choices:
        if player_choice == computer_choice:
            result = "平局!"
        elif (player_choice == "剪刀" and computer_choice == "布") or \
             (player_choice == "石头" and computer_choice == "剪刀") or \
             (player_choice == "布" and computer_choice == "石头"):
            result = "你赢了!"
        else:
            result = "你输了!"
    else:
        result = "请做出有效选择!"
    
    return result

def create_gui():
    window = tk.Tk()
    window.title("剪刀石头布游戏")
    
    choices = ["剪刀", "石头", "布"]
    
    for choice in choices:
        btn = tk.Button(window, text=choice, command=lambda c=choice: play(c))
        btn.pack()
    
    result_label = tk.Label(window, text="")
    result_label.pack()
    
    def play(choice):
        result = game(choice)
        result_label.config(text=result)
    
    window.mainloop()

create_gui()

总结

通过以上内容,我们实现了从初级到高级的“剪刀石头布”小游戏。从简单的控制台游戏到带有计分功能的版本,再到具有图形用户界面的高级版本,每个版本都展示了Python在游戏开发中的应用。希望本文能够帮助你提升Python编程技能,并为你带来乐趣!

举报

相关推荐

0 条评论