简体   繁体   English

格式字符串的参数太多

[英]Too many arguments for format string

In the following code, I keep getting the error "Too many arguments for format string" in lines 132, 135, 155, 158, 184, 187. Also I'm using python 3.7.9 as using a version newer than that break turtle for some reason.在下面的代码中,我在第 132、135、155、158、184、187 行不断收到错误“格式字符串的参数太多”。此外,我使用的是 python 3.7.9,因为它使用的版本比那个 break 龟更新因为某些原因。

#-----import turtle-----

import turtle as trtl
import random
import time

#-----game configuration-----

current_score = 0
high_score = 0
delay = 0.1

#-----initialize turtle-----

setup = trtl.Screen()
setup.title("Snake Game")
setup.bgcolor('pale green')
setup.setup(width=650, height=660)
setup.tracer(0)

top = trtl.Turtle()
top.speed(0)
top.shape("square")
top.color("dim gray")
top.penup()
top.goto(0, 0)
top.direction = "stop"

blocks= trtl.Turtle()
blocks.speed(0)
blocks.shape("square")
blocks.color("black")
blocks.penup()
blocks.goto(0, 100)

segments = []

scoreboard_score = trtl.Turtle()
scoreboard_score.speed(0)
scoreboard_score.shape("square")
scoreboard_score.color("black")
scoreboard_score.penup()
scoreboard_score.hideturtle()
scoreboard_score.goto(-310, 293)
scoreboard_score.write("Score: 0", align = "left", font=("digital-7", 24, "normal"))

scoreboard_highscore = trtl.Turtle()
scoreboard_highscore.speed(0)
scoreboard_highscore.shape("square")
scoreboard_highscore.color("black")
scoreboard_highscore.penup()
scoreboard_highscore.hideturtle()
scoreboard_highscore.goto(311, 293)
scoreboard_highscore.write("High Score: 0", align = "right", font=("digital-7", 24, "normal"))

border = trtl.Turtle()
border.speed(0)
border.penup()
border.goto(-310, 291)
border.pendown()
border.width(2)
border.forward(622)
border.right(90)
border.forward(603)
border.right(90)
border.forward(623)
border.right(90)
border.forward(603)
border.right(90)
border.forward(10)
border.right(180)
border.forward(11)
border.ht()

#-----game functions-----

def go_up():
    if top.direction != "down":
        top.direction = "up"

def go_down():
    if top.direction != "up":
        top.direction = "down"

def go_left():
    if top.direction != "right":
        top.direction = "left"

def go_right():
    if top.direction != "left":
        top.direction = "right"

def move():
    if top.direction == "up":
        y = top.ycor()
        top.sety(y+20)
    if top.direction == "down":
        y = top.ycor()
        top.sety(y-20)
    if top.direction == "left":
        x = top.xcor()
        top.setx(x-20)
    if top.direction == "right":
        x = top.xcor()
        top.setx(x+20)

#-----events-----

setup.listen()
setup.onkeypress(go_up, "Up")
setup.onkeypress(go_down, "Down")
setup.onkeypress(go_left, "Left")
setup.onkeypress(go_right, "Right")

while True:
    setup.update()

    if top.xcor()>290 or top.xcor()<-290 or top.ycor()>260 or top.ycor()<-290: #TODO
        time.sleep(1)
        top.goto(0, 0)
        top.direction = "stop"

        for segment in segments:
            segment.goto(1000, 1000)
        segments.clear()

        current_score = 0000

        delay = 0.1

        scoreboard_score.clear()
        scoreboard_score.write("Score: {}".format(current_score, high_score), align="left", font=("digital-7", 24, "normal"))

        scoreboard_highscore.clear()
        scoreboard_highscore.write("High Score: {}".format(current_score, high_score), align="right", font=("digital-7", 24, "normal"))

    if top.distance(blocks) <20:
        x = random.randint(-290, 270)
        y = random.randint(-290, 270)
        blocks.goto(x, y)

        new_segment = trtl.Turtle()
        new_segment.speed(0)
        new_segment.shape("square")
        new_segment.color("gray")
        new_segment.penup()
        segments.append(new_segment)

        delay -= 0.001
        current_score += 10

        if current_score > high_score:
            high_score = current_score
        scoreboard_score.clear()
        scoreboard_score.write("Score: {}".format(current_score, high_score), align="left", font=("digital-7", 24, "normal"))

        scoreboard_highscore.clear()
        scoreboard_highscore.write("High Score: {}".format(current_score, high_score), align="right", font=("digital-7", 24, "normal"))

    for index in range(len(segments)-1, 0, -1):
        x = segments[index-1].xcor()
        y = segments[index-1].ycor()
        segments[index].goto(x, y)
    if len(segments)>0:
        x = top.xcor()
        y = top.ycor()
        segments[0].goto(x, y)

    move()

    for segment in segments:
        if segment.distance(top)<20:
            time.sleep(1)
            top.goto(0, 0)
            top.direction = "stop"

            for segment in segments:
                segment.goto(1000, 1000)
            segments.clear()
            current_score = 0000
            delay = 0.1
    
            scoreboard_score.clear()
            scoreboard_score.write("Score: {}".format(current_score, high_score), align="left", font=("digital-7", 24, "normal"))

            scoreboard_highscore.clear()
            scoreboard_highscore.write("High Score: {}".format(current_score, high_score), align="right", font=("digital-7", 24, "normal"))
    time.sleep(delay)
setup.mainloop()

It's these two lines causing the problem every time they are in the code:每次出现在代码中时,都是这两行导致问题:

scoreboard_score.write("Score: {}".format(current_score, high_score), align="left", font=("digital-7", 24, "normal"))

scoreboard_highscore.write("High Score: {}".format(current_score, high_score), align="right", font=("digital-7", 24, "normal"))

I'm trying to make the snake game from the old Nokia phones.我正在尝试用旧的诺基亚手机制作贪吃蛇游戏。 Although this error doesn't break the code (or at least I think), It keeps popping up.尽管此错误不会破坏代码(或至少我认为),但它不断弹出。

All help is greatly appreciated!非常感谢所有帮助!

If you want to display both current_score and high_score, you should write :如果你想同时显示 current_score 和 high_score,你应该写:

"Score: {} {}".format(current_score, high_score)

Instead of :代替 :

"Score: {}".format(current_score, high_score)

which expects only one argument.它只需要一个参数。

Try removing one of the two arguments you provide on lines like these:尝试删除您在以下行中提供的两个参数之一:

scoreboard_score.write("Score: {}".format(current_score, high_score), align="left", font=("digital-7", 24, "normal"))

and using ones like this instead:并使用这样的:

scoreboard_score.write("Score: {}".format(current_score), align="left", font=("digital-7", 24, "normal"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM