【问题标题】:Python Game Using Turtle Not PyGame: Error Message使用 Turtle Not PyGame 的 Python 游戏:错误消息
【发布时间】:2020-11-16 23:00:20
【问题描述】:

我正处于使用海龟为 Python 制作一种碰碰车游戏的初期阶段。到目前为止,我已经创建了以下代码,这段代码只是为了创建“保险杠”海龟。我试图让这些海龟在列出的范围内随机生成在屏幕上(它们都出现在中间,我不知道为什么),我试图让它们随机移动并停留在我列出的边界内(如果不是,返回 0,0)。但是,它们不会移动,而是在中间生成。这是迄今为止我创建的唯一代码,我也不确定如何创建障碍类型代码,以便当它们遇到主海龟时用户将能够控制(尚未实现或尝试),他们从屏幕上消失了。任何对我当前问题的帮助或对我的目标的提示将不胜感激!但是,当我运行此代码时,我收到错误消息:

TypeError: check_boundary() 接受 0 个位置参数,但给出了 1 个

无论如何,这是我的代码:

import turtle
import random
from random import randint

class BumperTurtles():
    def __init__(bumper, name, color):
        bumper.color = color
        bumper.turtle = turtle.Turtle()
        bumper.turtle.shape("classic")
        bumper.turtle.color(bumper.color)
        bumper.turtle.speed(100)
        bumper.turtle.pd()
#Having them spawn within the desired range:
        x = random.randint (-150, 150)
        y = random.randint (-150, 150)

#Making sure they don't move out of bounds
    def check_boundary():
        if (-300 <= prey.xcor() <= 300 and -300 <= prey.ycor() <=300):
            return
        prey.goto(0, 0)

    def move(bumper):
        bumper.turtle.penup()
        turn = randint(0,2)
        if turn == 1:
            bumper.turtle.lt(45)
        elif turn == 2:
            bumper.turtle.rt(45)
        bumper.turtle.forward(5)

#Creating my bumper turtles
turtles = []
turtles.append(BumperTurtles('bump1', 'blue'))
turtles.append(BumperTurtles('bump2', 'blue'))
turtles.append(BumperTurtles('bump3', 'blue'))
turtles.append(BumperTurtles('bump4', 'blue'))
turtles.append(BumperTurtles('bump5', 'blue'))
turtles.append(BumperTurtles('bump6', 'blue'))
turtles.append(BumperTurtles('bump7', 'blue'))
turtles.append(BumperTurtles('bump8', 'blue'))

#Calling for them to move
ok = True
while ok:
    for t in turtles:
        t.move()
        if t.check_boundary():
            t.goto(0,0)
            ok = False

【问题讨论】:

  • 是的!现在它说“NameError: name 'bumper' is not defined”,但我不知道该怎么办,因为我认为我已经定义了它。但是将 (self) 添加到我的 check_boundary() 确实适用于其他错误消息!知道如何解决这个问题吗?
  • 好吧,BumperTurtles.move() 接受一个参数,bumper,但您将其称为 t.move(),而无需将任何内容传递为 bumper。如果您使用调试器,那么很容易弄清楚这一点。如果您不使用带有调试器的 IDE,那么您绝对应该这样做。

标签: python python-turtle


【解决方案1】:

您的代码永远不会按照您的方式工作。你测试check_boundary() 的结果,它没有返回任何东西;您引用了一个不存在的 prey 变量(即使您将其作为参数,它也会在 xcor() 调用中失败);您将随机值传递给speed() 方法;您计算了保险杠海龟的 x 和 y 位置但没有使用它们;等等

让我们把你的程序拆开,然后把它重新组合起来,让你可以实际运行并观察碰碰龟的随机进程:

from turtle import Screen, Turtle
from random import choice, randint

class BumperTurtle(Turtle):
    def __init__(self, name, color):
        super().__init__(shape='classic', visible=False)

        self.color(color)
        self.speed('fastest')
        self.penup()

        x = randint(-150, 150)
        y = randint(-150, 150)

        self.goto(x, y)
        self.showturtle()

    def inside_boundary(self):
        return -300 <= self.xcor() <= 300 and -300 <= self.ycor() <= 300

    def move(self):
        angle = choice([-45, 0, 45])
        self.right(angle)
        self.forward(5)

def bump():
    for bumper in bumpers:
        bumper.move()
        if not bumper.inside_boundary():
            bumper.goto(0, 0)

    screen.update()
    screen.ontimer(bump, 100)

bumpers = []
bumpers.append(BumperTurtle('bump1', 'blue'))
bumpers.append(BumperTurtle('bump2', 'blue'))
bumpers.append(BumperTurtle('bump3', 'blue'))
bumpers.append(BumperTurtle('bump4', 'blue'))
bumpers.append(BumperTurtle('bump5', 'blue'))
bumpers.append(BumperTurtle('bump6', 'blue'))
bumpers.append(BumperTurtle('bump7', 'blue'))
bumpers.append(BumperTurtle('bump8', 'blue'))

screen = Screen()
screen.tracer(False)

bump()

screen.mainloop()

请注意,我已将 BumperTurtle 从包含海龟更改为海龟子类。我用适当的计时器事件替换了您的 while True: 循环。我为您的方法切换到传统的self 参数。我关闭了跟踪并添加了update() 以提高图形性能,使其具有真正的碰碰车感觉——您可以通过将第二个参数调整为ontimer() 来调整速度,这是调用之间的时间(以毫秒为单位)。

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 2015-06-01
    相关资源
    最近更新 更多