问题
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