159.172 Computational Thinking


159.172 Computational Thinking
Tutorial 4: 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

159.172留学生作业代做、代做Python编程设计作业
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.
If you now go to the module my_critters.py you will see that this module implements a
basic implementation for a Caterpillar class.
A caterpillar has the following a=ributes:
1. xcoord and
2. ycoord
horizontal and vertical coordinates which are used by the draw_critter(self, screen)
method to give the location of the top left boundary of the graphic that represents the
caterpillar.
We create an instance of a caterpillar object in our main program critters.py with the code:
newcaterpillar = mycritters.Caterpillar()
Tasks
Tasks
1. Add a size a=ribute to the Caterpillar class, an integer between 1 and 5. Then, improve
the draw_critter(self, screen) method so that it takes account of the size a=ribute
and draws a whole caterpillar (face + body segments), not just a face. The size a=ribute
may determine the number of body segments and complexity of the caterpillar parts as
well as the overall size of the graphic.
Documentation for the required Pygame drawing commands can be found at
www.pygame.org/docs/ref/draw.html.
Consider other a=ributes that could be added to the Caterpillar class to enhance the
display of these cri=ers - health, age, colour-scheme, species … and so on.
2. Create a Bu=erfly class that will allow bu=erflies to be created and displayed. It is highly
recommended to create a base class for all cri=ers (Caterpillers & Bu=erflies) and
implement the shared properties & methods as this base level. It is up to you to
determine a=ributes that a bu=erfly will require, but you should at least include location
a=ributes and a draw_critter(self, screen) method to allow Bu=erfly objects to be
displayed.
3. Add code to the critters.py file so that when the user presses the 'b' key a new
Butterfly object is created and appended to the critterlist.
4. Set the location a=ributes for your two classes so that caterpillars are located at ground
level or below, and bu=erflies are located in the sky.
5. Add a colour_scheme a=ribute to each of your classes, or base class, (make this a list of
colours) and then update your draw_critter methods to take account of this
colour_scheme a=ribute.
6. Add a change_colour(self) method to each of your classes that can be used to switch
between two or more colour schemes.
7. Add code to the critters.py file so that when the user presses the 's' key the
change_colour(self) method is invoked on each of the cri=ers in the critterlist.
Submission
Submit your completed code, contained in the files critters.py and my_critters.py, via
Stream. Internal students: please note that you must a=end a workshop session and show
evidence of making progress on this tutorial during the session in order to gain marks for your
work.

 

因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:99515681@qq.com

微信:codehelp

上一篇:1.2 UML带来了什么——大象 thinking in UML


下一篇:iOS / Android 移动设备中的 Touch Icons