如何使用 import random 使海龟成为列表中的随机图像

How do you make a turtle a random image from a list using import random

所以我有 3 个老板,每个老板都有图像,但我不知道如何随机 select 乌龟使用图像。我应该制作另一个列表来存储图像,然后让它随机挑选一只乌龟吗?我是不是也应该在乌龟隐藏时立即改变它还是应该在 boss 战斗之前改变它?

import turtle as trtl
import random as rd
#these are all the images
fight1_image = "pixel.gif"
bossfight_cow = "boss_cow.gif"
boss_shape_1 = "1.gif"
boss_shape_2 = "acended.gif"
cow_gif = "cow.gif"
cow_helper1 = "cat_helper.gif"

#the 3 variables are the bosses i want to randomize all 3 with the #images below as variables
boss_list = []
heretic_swordsmen = trtl.Turtle()
priest = trtl.Turtle()
impaler = trtl.Turtle()
boss_list = [heretic_swordsmen,priest,impaler]

def bossfight1():
    play.hideturtle()
    #original cords are (0,-200)
    heretic_swordsmen.penup()
    heretic_swordsmen.goto(0,100)
    heretic_swordsmen.showturtle()
    #bosscow is just the main character ignore it
    bosscow.penup()
    bosscow.goto(0,-100)
    bosscow.showturtle()


首先在您的代码顶部添加:

trtl.register_shape("pixel.gif")
trtl.register_shape("boss_cow.gif")
trtl.register_shape("1.gif")

确保对每张要使用的图像都执行此操作,否则您将无法使用图像。 那么你可以轻松地说:

myboss = rd.choice(boss_list)

这给了你老板形象。 那么你可以为你的老板准备一只乌龟

boss_object = trtl.Turtle()

然后您可以将其形状设置为您的图像:

boss_object.shape(myboss)

大功告成!