gpt4 book ai didi

python - 在Python中为游戏添加计分时钟

转载 作者:行者123 更新时间:2023-12-01 03:55:20 25 4
gpt4 key购买 nike

我在这里有一个游戏的代码。我已经注释掉了 main 函数中的 displayScore(score) 调用,以允许程序运行。当取消注释该调用时,程序窗口在打开后立即关闭。

函数displayScore的目的是在左上角显示游戏得分。对方玩家的得分也需要显示在右上角。

这是游戏的代码,在主函数中注释掉了 displayScore,这样您就可以运行游戏,一切都会正常。取消注释看看问题出在哪里:

        ball = ballmovement(ball, ballDirX, ballDirY) 
ballDirX, ballDirY = collisionwithedges(ball, ballDirX, ballDirY)
score = checkscore(paddle1, ball, score, ballDirX)
ballDirX = ballDirX * collisionwithpaddles(ball, paddle1, paddle2, ballDirX)

pygame.display.update() #updates the display to clear surface per the frame rate
FRAMECLOCK.tick(FRAMERATE) #Sets the Frames of program to defined rate

if __name__=='__main__':
main()

最佳答案

只需更换线路即可

displayScore(score) 

作者:

displayScore(str(score)) 

您正在尝试使用数字而不是字符串作为 render 的参数;)Score 是一个 int 且 BASICFONT.render((score), True, WHITE)
要求分数是字符串或字节数组:)

我仅通过阅读控制台输出找到了解决方案,这是一个很好的指示^^

    Traceback (most recent call last):
File "test.py", line 130, in <module>
main()
File "test.py", line 118, in main
displayScore(score)
File "test.py", line 71, in displayScore
resultSurf = BASICFONT.render((score), True, WHITE)
TypeError: text must be a unicode or bytes

关于python - 在Python中为游戏添加计分时钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37576161/

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