gpt4 book ai didi

python - 变量值不更新

转载 作者:太空宇宙 更新时间:2023-11-03 19:57:54 25 4
gpt4 key购买 nike

基本上,我正在为乒乓球比赛构建评分系统。全局变量 s_a 为 0,并且应该在球失去弹跳后发生变化。

import turtle
import time

win = turtle.Screen()
win.title("Pong")
win.bgcolor("black")
win.setup(width=800, height=600)
win.tracer(0)

ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("white")
ball.penup()
ball.goto(0, 0)
ball.dx = 2

s_a = 0

s_s = turtle.Turtle()
s_s.speed(0)
s_s.hideturtle()
s_s.goto(0, 0)
s_s.penup()
s_s.color("white")
current_score = "Player A: {} | Player B: 0".format(s_a)
s_s.write(current_score, align="center", font=("Courier", 16, "normal"))

while True:
win.update()
time.sleep( 1 / 60 )
ball.setx(ball.xcor() + ball.dx)
if ball.xcor() > 380:
s_a += 1
ball.goto(0,0)

球会自行重置并按预期改变方向,但变量不会更新。我错过了什么吗?

最佳答案

你的代码没问题。您只需在屏幕上重新打印分数即可。我的意思是添加

current_score = "Player A: {} | Player B: 0".format(s_a)
s_s.clear()
s_s.write(current_score, align="center", font=("Courier", 16, "normal"))

在最后一行之前更新 s_a 后。

关于python - 变量值不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59444411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com