Home > Back-end >  How do you make a turtle a random image from a list using import random
How do you make a turtle a random image from a list using import random

Time:03-31

So I have 3 bosses and I have images for each but I don't know how to the turtle randomly select a image to use. Should I make another list to store the images and then make it randomly pick a turtle? Should I also make the turtle change right then while its hidden or should I make it change before the boss fight?

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()


CodePudding user response:

at first at top of your code add:

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

make sure to do this for every image you want to use else you can't use image. then you can easily say :

myboss = rd.choice(boss_list)

this gives you the boss image . then you can want to have a Turtle for your boss

boss_object = trtl.Turtle()

and then you can set its shape to your image:

boss_object.shape(myboss)

and your done!

  • Related