How to show two different photos as turtles?

瘦欲@ 提交于 2021-02-07 10:10:12

问题


I'm trying to import two different pictures and show them in a line using Turtle (when I'll manage to do so they are supposed to move) but I can't find a way to show them together, the last picture I assigned to the turtle and the screen is taking over. This is my code:

from turtle import Turtle, Screen
from PIL import Image

screen = Screen()
height = 200

def create_players(height):
    player = Turtle()
    pic_name = input('write exact photo name: ')
    image = Image.open(pic_name)
    new_image = image.resize(((30, 30)))
    pic_resized = 'pic_resized.gif'
    new_image.save(pic_resized)
    screen.addshape(pic_resized)
    player.shape(pic_resized)
    player.penup()
    player.goto(-250, height)
    return player


player1 = create_players(200)
player2 = create_players(150)

Thank you!


回答1:


The problem is this code:

pic_resized = 'pic_resized.gif'
new_image.save(pic_resized)
screen.addshape(pic_resized)

You've pointed both turtle images to the same file. We simply need to make sure the resized image file name is unique:

from turtle import Screen, Turtle
from PIL import Image

def create_players(height):
    pic_name = input("Write exact photo name: ")
    image = Image.open(pic_name)
    new_image = image.resize(((30, 30)))
    pic_resized = pic_name + '_resized.gif'
    new_image.save(pic_resized)
    screen.addshape(pic_resized)

    player = Turtle()
    player.shape(pic_resized)
    player.penup()
    player.goto(-250, height)

    return player

screen = Screen()

player1 = create_players(200)
player2 = create_players(150)

screen.exitonclick()


来源:https://stackoverflow.com/questions/65131166/how-to-show-two-different-photos-as-turtles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!