Draw a sprite¶
How To Install Pygame
Making Games with Python & Pygame Making Games was written as a sequel for the same age range as Invent with Python. Once you have an understanding of the basics of Python programming, you can now expand your abilities using the Pygame library to make games with graphics, animation, and sound. Pygame.font.SysFont If you’re not looking to be including any ttf files in your code, we then turn to using pygame.font.SysFont. This is the method I personally recommend and use in my own pygame programs. A good strategy is to first find the fonts supported by the system the code is executing on. What is Pygame in python? Python Pygame is a cross-platform set of python modules that are designed for creating video games. It also includes graphics and sound libraries which is to be used for the Python programming language.
Before we can draw anything, we’ll need to save an alien sprite to use. You canright click on this one and save it (“Save Image As…” or similar).
(This sprite has a transparency (or “alpha”) channel, which is great for games!But it’s designed for a dark background, so you may not be able to see thealien’s space helmet until it is shown in the game).
Tip
You can find lots of free sprites, including this one, on kenney.nl. Desktop lastpass. This one comes from thePlatformer Art Deluxe pack.
You need to save the file in the right place so that Pygame Zero can find it.Create a directory called
images
and save the image into it asalien.png
. Both of those must be lower case. Pygame Zero will complainotherwise, to alert you to a potential cross-platform compatibility pitfall.If you’ve done that, your project should look like this:
![Pygame download Pygame download](/uploads/1/3/7/7/137799254/373988567.jpg)
Pygame Game Code
images/
is the standard directory that Pygame Zero will look in to findyour images.There’s a built-in class called
Actor
that you can use to represent agraphic to be drawn to the screen.Pygame Image Scale
Justin guitar pop songbook online. Let’s define one now. Change the
intro.py
file to read:Pygame Tutorial
Your alien should now be appearing on screen! By passing the string
'alien'
to the Actor
class, it automatically loads the sprite, and has attributeslike positioning and dimensions. This allows us to set the HEIGHT
ofthe window based on the height of the alien.The
alien.draw()
method draws the sprite to the screen at its currentposition.