Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 格式字符串的参数太多_Python_Python 3.x_Turtle Graphics_Python Turtle - Fatal编程技术网

Python 格式字符串的参数太多

Python 格式字符串的参数太多,python,python-3.x,turtle-graphics,python-turtle,Python,Python 3.x,Turtle Graphics,Python Turtle,在下面的代码中,我在第132、135、155、158、184、187行中不断遇到错误“格式字符串的参数太多”。此外,出于某种原因,我正在使用python 3.7.9作为比break turtle更新的版本 #-----import turtle----- import turtle as trtl import random import time #-----game configuration----- current_score = 0 high_score = 0 delay =

在下面的代码中,我在第132、135、155、158、184、187行中不断遇到错误“格式字符串的参数太多”。此外,出于某种原因,我正在使用python 3.7.9作为比break turtle更新的版本

#-----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()
我正在尝试用旧诺基亚手机制作蛇游戏。尽管这个错误并没有破坏代码(至少我认为是这样),但它还是会不断出现


非常感谢您的帮助

如果您想同时显示当前评分和高评分,您应该写:

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

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

它只需要一个参数。

请尝试删除您在以下行中提供的两个参数之一:

scoreboard_score.write("Score: {}".format(current_score, high_score), align="left", font=("digital-7", 24, "normal"))
而使用这样的方式:

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

请参阅,行号不会显示在代码段中。您可以通过重新复制产生错误的行来增加收到正确答案的概率。@FrontRanger对此表示抱歉,我已经继续并更新了导致问题的行噢,我的上帝,谢谢,它不仅修复了“太多参数”错误。它还修复了我遇到的另一个问题,即高分没有出现在屏幕上,正在重置。非常感谢你!
scoreboard_score.write("Score: {}".format(current_score), align="left", font=("digital-7", 24, "normal"))