Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
Tutorial : Creating Classes and Objects
In this tutorial you will develop class definitions and create objects that are instances of those
classes.
Go to Stream and download the files
critters.py
my_critters.py
Set up a new project and add these files to it.
critters.py is a program that is meant to display caterpillars and bu=erflies (and any other
cri=ers that you want to create) against a simple background scene. When you run this
program, you will see a screen, 1000 pixels wide by 600 pixels high, displaying a basic
background scene.
On each iteration of the main program loop we check to see if the user wants to quit, or if he
has pressed the 'c' key, which initiates creation of a new Caterpillar object that is appended
to the critterlist (initially empty). After that, we update the screen by redrawing the
background scene and the cri=ers in the cri=er list. To begin with, a caterpillar is just depicted
as a face, no body parts.
If you look at the top of the program critter.py , you will see that two modules are
imported, pygame and my_critters. A li=le further down, you will see the drawing function
for the background, draw_background(). If you want a nicer background picture, you can
alter this function.