top of page

Using images as graphics when using the turtle module in Python



star wars game python

When using games as a way of learning programming in Python, fancy graphics are the least important part - what matters is a functional game; it can always be made to look prettier later. Still there is a relatively easy way of using images in the games we write. This technical post summarises the steps required to do so when using the turtle package.

 

To use images with the turtle package in Python there are two steps:

(1) You first need to find an image either from the web or elsewhere, or you can create your own image with whatever software you wish. Ideally this should have a transparent background (often seen as a checkered background when viewing an image) otherwise you will see a white square surrounding your image when you use it in your game or animation. (Make sure you download the image file not the link to a webpage.)

— Once you have your image file on your computer, open it. On a Mac you should use the default software, Preview. On a Windows computer do not choose the default option, instead right click on the file and choose 'Open with', then choose Paint.

— Change the size of the image to the size you want it to be in your game/animation. Remember that we are using pixels to determine the size of things so you should change the image size using pixels. A good size might be around 50-100 pixels in width and height. You can always come back and change the size later. On a Mac go to the ’Tools’ menu in Preview and choose ‘Adjust size…’, then select ‘Pixels’ from the drop down menu next to Width and Height and change either the width or the height (the other will change automatically). On a Windows computer choose the ‘Resize' button in Paint, choose pixels and change either height or width (the other will change automatically)


— You are now ready to save the image file. Whatever format the original image was, you will now need to save it as a GIF file. On a Mac go to the ’Files’ menu in Preview and choose ‘Export…’. You will now need to choose GIF from the drop-down menu that says Format BUT you need to click this menu while pressing the option/alt key on your keyboard, otherwise the GIF option will not be visible. On a Windows computer click on ‘File’ and choose ‘Save As’, then choose GIF from the drop-down menu that says ‘Save as type’

— You can save your GIF image directly in the folder you are using for your coding (This is usually in your User folder in a folder called PyCharmProjects [if using PyCharm] and then in a folder that has the name of the Project (sometimes this is called untitled if you haven’t specifically chosen a name). If you have already saved your GIF file elsewhere on your computer, you should move it to that folder.

(2) You are now ready to use this image in Python. There are two steps required here.

— If you have named your Screen() window, then you need the following command: window.register_shape(“name_of_file.gif”). Inside the speech marks you should put the name of your image file, including the .gif. Make sure you write it in exactly as it is in the file name

— If you have a turtle named fred, then you need to use the following command: fred.shape(“name_of_file.gif”). Again the name of your GIF file should go inside the speech marks.

You are now ready to run your program. If you need to change the size of the image, open the image file that is in your project folder and simply change its size as before, then run your program again.

 

Do you want to start learning Python?




1,614 views

Python Coding for Young People

CT-Unlimited-FinalRAW-transparent.png

Codetoday Unlimited is for the curious teenager or preteen keen to learn proper Python coding. Stephen's courses start from the basics and carry on to intermediate and advanced levels.

Python Coding for Adults

The Python Coding Place is Stephen's platform full of courses and other resources for beginners and intermediate learners. The focus is on clarity and Stephen's unique communication style.

bottom of page