You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Turtle开发太空入侵者遇TypeError及外星人绘制求助

问题解决:TypeError: 'list' object is not callable 及外星人绘制实现

一、修复生命值心形的调用错误

你遇到的错误根源是类的实例属性与方法重名:在Scoreboard的__init__方法中定义了self.lives = [](一个列表类型的实例属性),同时又定义了同名的def lives(self)方法。当调用scoreboard.lives()时,Python会优先识别self.lives这个列表对象,而非方法,因此抛出“列表对象不可调用”的错误。

修复步骤:

  1. 修改实例属性名,避免与方法名冲突:将__init__里的self.lives = []改为self.live_hearts = []
  2. 同步修改lives()方法中的列表追加代码:将self.lives.append(heart)改为self.live_hearts.append(heart)
  3. 确保在scoreboard文件顶部导入math模块(你的代码中用到了但未显式导入)

修改后的scoreboard核心代码:

import math
from turtle import Turtle

class Scoreboard(Turtle):
    def __init__(self):
        super().__init__()
        self.live_hearts = []  # 重命名属性避免冲突
        self.score = 0
        self.lives_start_x = 0
        with open("High_Score.txt") as data:
            self.high_score = int(data.read())
        self.color("white")
        self.penup()
        self.update_scoreboard()
        self.hideturtle()

    def lives(self):
        heart = Turtle()
        heart.penup()
        heart.goto(x=self.lives_start_x, y=0)
        heart.color('white')
        heart.hideturtle()
        heart.pendown()
        d = 10
        r = d / math.tan(math.radians(67.5))
        heart.up()
        heart.seth(45)
        heart.begin_fill()
        heart.down()
        heart.fd(d)
        heart.circle(r, 225)
        heart.left(180)
        heart.circle(r, 225)
        heart.fd(d)
        heart.end_fill()
        self.live_hearts.append(heart)  # 同步修改属性引用
        self.lives_start_x += 35

之后调用scoreboard.lives()三次,即可生成右上角的三颗心形生命值图标。

二、用Turtle绘制贴合的太空入侵者风格外星人

下面是一个可复用的Alien类,通过组合椭圆、圆形和多边形绘制经典外星人造型:

from turtle import Turtle

class Alien(Turtle):
    def __init__(self, x=0, y=0):
        super().__init__()
        self.hideturtle()
        self.penup()
        self.goto(x, y)
        self.color("limegreen")
        self.speed(0)
        self.draw_alien()

    def draw_alien(self):
        # 绘制头部(椭圆)
        self.begin_fill()
        self.setheading(0)
        for _ in range(2):
            self.circle(15, 90)
            self.circle(10, 90)
        self.end_fill()

        # 绘制眼睛
        self.penup()
        self.goto(self.xcor() - 8, self.ycor() + 5)
        self.color("white")
        self.begin_fill()
        self.circle(3)
        self.end_fill()
        self.goto(self.xcor() + 16, self.ycor())
        self.begin_fill()
        self.circle(3)
        self.end_fill()
        self.color("limegreen")

        # 绘制身体
        self.penup()
        self.goto(self.xcor() - 8, self.ycor() - 15)
        self.pendown()
        self.begin_fill()
        self.setheading(270)
        self.forward(10)
        self.setheading(180)
        self.forward(16)
        self.setheading(90)
        self.forward(10)
        self.end_fill()

        # 绘制腿部
        self.penup()
        self.goto(self.xcor() - 12, self.ycor() - 10)
        self.pendown()
        self.setheading(270)
        self.forward(5)
        self.penup()
        self.goto(self.xcor() + 4, self.ycor())
        self.pendown()
        self.setheading(270)
        self.forward(5)
        self.penup()
        self.goto(self.xcor() + 8, self.ycor())
        self.pendown()
        self.setheading(270)
        self.forward(5)

使用示例:

# 在坐标(100, 100)处生成一个外星人
alien = Alien(x=100, y=100)

内容的提问来源于stack exchange,提问作者MIchael Dearing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 20:09:25