联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Python编程Python编程

日期:2018-10-09 10:00

Computational Thinking 16/08/18, 12)04 AM

file:///Users/cmmccart/Documents/Teaching/159172/Assignment2_2018.html Page 1 of 6

159.172 Computational Thinking

Programming Assignment 1:

The Hungry Caterpillar

This assignment is worth 15% of your final mark. It will be marked out of a total of 20

marks. You are expected to work on this assignment individually and all work that you hand

in is expected to be your own work.

In this assignment you will develop an animated application that uses linked lists as the

basis of the implementation. This application builds somewhat on the work that you did for

tutorial 4.

Go to Stream and download the files

caterpillar.py caterpillar.py

my_catclass.py my_catclass.py

Set up a new project and add these files to it.

caterpillar.py is a program that implements the beginnings of a simple graphical animation.

When you run this program, you will see a screen, 1000 pixels wide by 400 pixels high,

displaying a basic background scene with a 'caterpillar' anchored at random location. To

begin with, the caterpillar just has a face, no body parts.

On each iteration of the main program loop we check to see if the user wants to quit, or if he

has pressed the spacebar, or one of the 'm', 'd' or 's' keys, each of which initiates a different

response (full functionality is described later.) After that, we update the screen by redrawing

the background scene and the caterpillar. The drawing methods for the background and the

caterpillar have been provided for you.

The module my_catclass.py, which is imported into caterpillar.py, implements a linked list

based implementation for a caterpillar object.

A caterpillar consists of the following components:

1. face_xcoord and face_ycoord - horizontal and vertical coordinates for the face. These

coordinates are used by the draw_face(screen) method to give the location of the top

left boundary of the graphic that represents the caterpillar’s face.

2. body – a reference to a segment_queue object. A segment_queue is a linked list of

body_segment nodes that makes up the caterpillar’s body. A segment_queue has head

and last references, just like the Improved Linked Queue class shown in lectures. A

Computational Thinking 16/08/18, 12)04 AM

file:///Users/cmmccart/Documents/Teaching/159172/Assignment2_2018.html Page 2 of 6

and last references, just like the Improved Linked Queue class shown in lectures. A

body_segment(x, y) object consists of x and y coordinates, giving the location of the top

left boundary of the graphic representing the segment; and a reference to the next

body_segment in the list.

3. travel_direction – a string representing the current direction of travel for the caterpillar,

either 'left' or 'right'.

4. food – a reference to a food_list object. A food_list is a linked list of food_item nodes

that makes up the caterpillar’s food. A food_item(x, y) object consists of x and y

coordinates, giving the location of the top left boundary of the graphic representing the

item; and a reference to the next fooditem in the list.

We create an instance of a caterpillar object in our main program caterpillar.py with the code:

mycaterpillar = my_catclass.caterpillar() mycaterpillar = my_catclass.caterpillar()

Tasks

You first need to program code for the following three methods in the caterpillar class:

1. def grow(self): def grow(self):

...

This method adds a body segment to the rear of a caterpillar. Pressing the

spacebar will invoke this method on the mycaterpillar object.

To implement this method, you first need to attend to the segment_queue class.

Inside the segment_queue class, you need to provide a method to add a

body_segment, with given x and y parameters, to a segment_queue. At the

moment this function doesn't do anything.

def addSegment(self, x, y): def addSegment(self, x, y):

return return

You should look at the lecture slides and pay particular attention to those that

show how to insert a cargo item at the end of a queue, using the Improved

Linked Queue implementation.

Inside the caterpillar class, your grow() method will require a call to the

addSegment(x, y) method for the caterpillar body, with correct location

parameters. Note that the x coordinate for the new body segment will depend on

the travel direction of the caterpillar and also on whether or not the segment is the

first one to be added. When the body is empty the new segment should be placed

relative to the caterpillar's face, otherwise it should be placed relative to the last

body segment. Also note that the face graphic is 40 pixels wide, while a body

segment graphic is only 35 pixels wide. The y coordinate for the new segment can

Computational Thinking 16/08/18, 12)04 AM

file:///Users/cmmccart/Documents/Teaching/159172/Assignment2_2018.html Page 3 of 6

segment graphic is only 35 pixels wide. The y coordinate for the new segment can

be taken to be the same as the y coordinate of the caterpillar's face.

2. def reverse(self): def reverse(self):

...

This method sets up the caterpillar to move in the opposite direction. This entails

reversing the travel direction, reversing the linked queue that stores the body

segment coordinates, and then "attaching" the face at the other end. Don’t forget

that the face graphic is 40 pixels wide, while a body segment graphic is only 35

pixels wide. Along with the travel direction, this will affect the calculation of the

new x coordinate for the face.

3. def move_forward(self): def move_forward(self):

...

This method moves the caterpillar forward one step, either to the left or the right,

depending on the travel direction. You will get a fairly smooth animation if you

set a step to equal 2 pixels. You can presume that a caterpillar without a body

can't move, so first test for an empty body. If one more step forward would send

the caterpillar out of the scene, you need to instead have the caterpillar reverse

and then move off in the opposite direction. Note that the scene ranges from 0 to

1000 pixels, 0 at the left edge, 1000 at the right edge. Pressing the 'm' key will

invoke this method on the mycaterpillar object.

Once you have these three methods completed, you can grow the caterpillar for a few

segments, by pressing the space bar, and then press the 's' key; your caterpillar should

run back and forth across the scene, reversing when it hits either edge of the scene.

Press the 's' key again to stop the animation (the main loop will revert to its first

conditional block, invoked when the Boolean variable start_anim is False.)

When you have got this part of the program working properly, the next thing to do is

to program code for the following method:

def drop_food(self): def drop_food(self):

...

This method should drop some food at a random location in the scene, in other words,

add a food_item to the food_list that makes up the caterpillar’s food.

First, generate a random x coordinate for the new food item. The x coordinate for the

new food item can be computed by the method random.randrange(0, 980). Note that

the generator for the x coordinate should range from 0 to 980, rather than 0 to 1000,

since the x coordinate gives the location of the top left corner of the food graphic. The y

coordinate for the new food item should be calculated relative to the caterpillar's

face_ycoord.

Computational Thinking 16/08/18, 12)04 AM

file:///Users/cmmccart/Documents/Teaching/159172/Assignment2_2018.html Page 4 of 6

Once you have this method working, you can press the 'd' key to add some food to the

caterpillar’s food list, and have it displayed in the scene. You will also need to extend

the move_forward() method so that once the caterpillar has moved forward, the method

checks to see if his face has come within 5 pixels of any food item.

The move_forward() method should delete any food item in the food list that is within

5 pixels of the caterpillar's face. Use the abs() function, applied to the difference of the x

coordinates of the caterpillar's face and each food item, to check distances. You should

look at the lecture slides and pay particular attention to those that show how to remove

'bad' items from a linked list.

Your final tasks are to program code to make the caterpillar react to food and advance

his life cycle.

When you have got the caterpillar moving properly and eating his food, you need to

have him react to food. Add a ‘consumption’ attribute to the caterpillar class. This

should be an integer, set to zero upon initialization. Each time he eats some food the

consumption attribute is increased by 1. If ‘consumption’ is above, say, 15, the

caterpillar is blooming with health and his body should turn yellow. If consumption is

above, say, 30, the caterpillar needs to moult his exoskeleton and emerge in larger form.

It is up to you what this more mature caterpillar looks like, but he should still consist

of a face and a body made up of a linked list of body_segment nodes.

These changes can be implemented by altering the provided methods draw_face(),

draw_body() and draw_segment(). You will need to consider the consumption attribute

when re-implementing the draw_face() and draw_body() methods and you will need to

alter the draw_segment() method so that it is passed colour and size parameters. Upon

hitting the higher consumption limit for the first time your code will need to traverse

the body segment_queue, updating the co-ordinates in each of the body_segments, in

order to accommodate a larger graphic. Write your own ‘moult’ method to do this and

add it to the caterpillar class.

def moult(self): def moult(self):

...

Once you have these methods completed, you can grow the caterpillar for a few

segments, by pressing the space bar, and then drop some food for him, by pressing the

'd' key, and then press the 's' key; your caterpillar should run back and forth across the

scene, reversing when it hits either edge of the scene, any food eaten should disappear

and the caterpillar should react appropriately to the food eaten. Press the 's' key again

to stop the animation.

Once you have the animation working properly, you may like to try one of the following

optional extensions.

Computational Thinking 16/08/18, 12)04 AM

file:///Users/cmmccart/Documents/Teaching/159172/Assignment2_2018.html Page 5 of 6

Consider how real caterpillars move, in a wave-like motion from front to back.

See You Tube Close Up Caterpillar Footage

Can you get your caterpillar to move forward using a more natural motion? You will

need to consider both the x and y coordinates of the face and each body segment.

Another option is to consider how to alter the program so that it can sensibly deal with

more than one caterpillar at a time. Your group (or pair) of caterpillars should share

food and will need to be able to access the locations of one another, so the caterpillar

class will need to be altered. One possibility - upon two caterpillars meeting, have the

longer of the two climb over the shorter. No X-rated caterpillar activity!

Submission

Submit the assignment in Stream as a single zipped file containing:

1. Your completed code, contained in the file my_catclass.py.

2. A word document containing annotated screen shots demonstrating the behaviour of

your program.

3. If you have attempted any extension to the project, submit your extended code as

separate code modules, and include a separate word document demonstrating the

extended program behavior. This will not (necessarily) contribute to your final mark,

but imaginative solutions are welcomed!

Marking Scheme:

Implementation of each of the first three methods:

grow(), reverse() reverse() and move_forward() move_forward() - 3 marks each

Implementation of the drop_food() drop_food() method - 2 marks

Implementation of correct reactions to food:

1. colour change - 3 marks

2. moult method and graphic display of mature caterpillar - 4 marks

Annotated screenshots demonstrating the behaviour of your program - 2 marks

Total – 20 marks

Late submission:

Late assignments will be penalised 10% for each weekday past the deadline, for up to five (5)

days; after this no marks will be gained. In special circumstances an extension may be

Computational Thinking 16/08/18, 12)04 AM

file:///Users/cmmccart/Documents/Teaching/159172/Assignment2_2018.html Page 6 of 6

obtained from the paper co-ordinator, and these penalties will not apply. Workload will not

be considered a special circumstance – you must budget your time.


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp