PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 6, 2022

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

 May 06, 2022     image, python, random, turtle-graphics     No comments   

Issue

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



Solution

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!



Answered By - Amin shahrabi
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing