【问题标题】:how can I add to the score?我怎样才能增加分数?
【发布时间】:2025-12-09 01:25:01
【问题描述】:

我对编程很陌生,并且正在努力变得更好。我已经在 tkinter 中编写了这个游戏,但我不知道如何在我的代码中添加一个 while 循环或类似的东西,所以我不必每次都将分数重置为零。我也知道我的代码不是最干净的,也不是最好的,但就像我说的我对编程很陌生,想知道是否有人可以帮助我弄清楚我应该怎么做?

from tkinter import *
import random


m = Tk()
blank_space = " " * 65
m.title(blank_space + "Rock Paper Scissors Game")
m.config(width=600, height=400, bg='grey')


# Frames
button_frame = Frame(m, width=300, height=200)
button_frame.grid_propagate(False)
button_frame.grid(row=0, column=0, padx=150)
text_frame = Frame(m, width=500, height=200)
text_frame.grid_propagate(False)
text_frame.grid(row=1, column=0, padx=50)

# Functions


def rock():
    user_choice_label.config(text="your choice is : Rock")
    computer_score = 0
    user_score = 0
    user_choice = "Rock"
    available_text = ["Rock", "Paper", "Scissors"]
    computer_choice = random.choice(available_text)
    if computer_choice == user_choice:
        computer_score = 0
        user_score = 0
        computer_choice_label.config(text=f"computer choice is : {computer_choice}")
        computer_score_label.config(text=f"computer score is : {computer_score}")
        user_score_label.config(text=f"your score is : {user_score}")
    if computer_choice == "Paper":
        computer_choice_label.config(text=f"computer choice is : {computer_choice}")
        computer_score += 1
        user_score = 0
        computer_score_label.config(text=f"computer score is : {computer_score}")
        user_score_label.config(text=f"your score is : {user_score}")
    if computer_choice == "Scissors":
        computer_choice_label.config(text=f"computer choice is : {computer_choice}")
        user_score += 1
        computer_score = 0
        user_score_label.config(text=f"your score is : {user_score}")
        computer_score_label.config(text=f"computer score is : {computer_score}")


def paper():
    user_choice_label.config(text="your choice is : Paper")
    user_score = 0
    computer_score = 0
    user_choice = "Paper"
    available_text = ["Rock", "Paper", "Scissors"]
    computer_choice = random.choice(available_text)
    if computer_choice == user_choice:
        computer_score = 0
        user_score = 0
        computer_choice_label.config(text=f"computer choice is : {computer_choice}")
        computer_score_label.config(text=f"computer score is : {computer_score}")
        user_score_label.config(text=f"your score is : {user_score}")
    if computer_choice == "Scissors":
        computer_choice_label.config(text=f"computer choice is : {computer_choice}")
        computer_score += 1
        user_score = 0
        computer_score_label.config(text=f"computer score is : {computer_score}")
        user_score_label.config(text=f"your score is : {user_score}")
    if computer_choice == "Rock":
        computer_choice_label.config(text=f"computer choice is : {computer_choice}")
        user_score += 1
        computer_score = 0
        user_score_label.config(text=f"your score is : {user_score}")
        computer_score_label.config(text=f"computer score is : {computer_score}")


def scissors():
    user_choice_label.config(text="your choice is : Scissors")
    user_score = 0
    computer_score = 0
    user_choice = "Scissors"
    available_text = ["Rock", "Paper", "Scissors"]
    computer_choice = random.choice(available_text)
    if computer_choice == user_choice:
        computer_score = 0
        user_score = 0
        computer_choice_label.config(text=f"computer choice is : {computer_choice}")
        computer_score_label.config(text=f"computer score is : {computer_score}")
        user_score_label.config(text=f"your score is : {user_score}")
    if computer_choice == "Rock":
        computer_choice_label.config(text=f"computer choice is : {computer_choice}")
        computer_score += 1
        user_score = 0
        computer_score_label.config(text=f"computer score is : {computer_score}")
        user_score_label.config(text=f"your score is : {user_score}")
    if computer_choice == "Paper":
        computer_choice_label.config(text=f"computer choice is : {computer_choice}")
        user_score += 1
        computer_score = 0
        user_score_label.config(text=f"your score is : {user_score}")
        computer_score_label.config(text=f"computer score is : {computer_score}")


# User Buttons
rock_button = Button(button_frame, text="Rock", activebackground='red', command=rock, width=15, bg='white')
rock_button.grid(row=0, column=0, padx=90, pady=15)

paper_button = Button(button_frame, text="Paper", command=paper, activebackground='red', width=15, bg='white')
paper_button.grid(row=1, column=0, padx=90, pady=15)

scissors_button = Button(button_frame, text="Scissors", command=scissors, activebackground='red', width=15, bg='white')
scissors_button.grid(row=2, column=0, padx=90, pady=15)

# Contents for the text frame
user_choice_label = Label(text_frame, text="your choice is : ")
user_choice_label.grid(row=1, column=0, padx=20, pady=10)

computer_choice_label = Label(text_frame, text="computer choice is : ")
computer_choice_label.grid(row=1, column=2, padx=120, pady=10)

computer_score_label = Label(text_frame, text="computer score is : ")
computer_score_label.grid(row=3, column=2)

user_score_label = Label(text_frame, text="your score is : ")
user_score_label.grid(row=3, column=0, padx=20)

# Exit button
exit_button = Button(text_frame, text="Exit", command=m.destroy, width =30, activebackground='red')
exit_button.grid(row=5, column=0, columnspan=3, pady= 20, padx=40)

m.mainloop()

【问题讨论】:

    标签: python tkinter while-loop


    【解决方案1】:

    增加分数的想法是您需要将user_scorecomputer_score 声明为global 变量。

    global user_score, computer_score
    user_score = 0
    computer_score = 0
    
    m = Tk()
    blank_space = " " * 65
    
    .....
    .....
    

    另外,在每个函数中,添加global user_score, computer_score 并将分数分配更新为递增(例如computer_score += 0)。

    例如,

    def rock():
        global user_score, computer_score
        ....
        ....
        if computer_choice == user_choice:
            computer_score += 0
            user_score += 0
            ....
            ....
        if computer_choice == "Paper":
            computer_score += 1
            user_score += 0
            ....
            ....
        if computer_choice == "Scissors":
            user_score += 1
            computer_score += 0
            ....
            ....
    

    预期结果:

    【讨论】:

    • 太棒了。谢谢你,我的朋友。