Automatic Landscape

Table of Contents

This assignment is to be done individually. You can talk to other people in the class, me (Dave), the prefect, and lab assistants for ideas and to gain assistance. You can help each other debug programs, if you wish. The code that you write should be your own, however, and you shouldn't hand a printout of your program to others. See the course syllabus for more details or just ask me if I can clarify.

We will use anonymous grading on Moodle, which means that the grader won't see your name until after the grading is done. This is an easy way to help add an extra element of fairness to the grading. Therefore, make sure your name doesn't appear on your actual submission. When you submit via Moodle, it will know you are. Thanks!

1 Overview

One important way to help people understand weather and climate can be to create computer visualizations. Interactive weather maps show clouds and other forms of weather to help people safely make life choices based on weather. Visual simulations of weather changing over long periods of time can be used to help people understand climate dynamics.

For this assignment, you will create an image that can be changed based on conditions provided by the user.

2 Draw a landscape and a cloud

Create a directory named landscape1 to store your work (remember to do this at the command line), then copy into it the graphics.py library that we have been using. Write Python code to make a canvas of size 500 pixels wide and 700 pixels high, then draw a simple landscape. Your landscape doesn't have to be particularly fancy. For example, you might set the background to blue to represent a sky, then draw a couple of rough hills or buildings using ovals or rectangles.

Draw a small white cloud at the top left corner off your canvas. Your cloud can be as simple as a cloud-sized oval, but feel free to use multiple overlapping ovals or other techniques to make your cloud as attractive as you like.

3 Make the position of your cloud adjustable

The goal of this part of the assignment is to place the cloud in different places in the sky, depending on where the user of your program wants it. Specifically, your program should ask your user for a wind speed (in miles per hour), and the number of hours that have passed since the cloud first appeared in the top left of the window. Your program should then:

  • draw the cloud in its correct location
  • display the horizontal location where the top-left of the cloud object should be

Assume that the image is 20 miles across, and that the wind is blowing directly to the right. Here is what the interaction between your program and your user should look like. (The two numbers, i.e. 3.5 and 2.5, are entered in by the user; everything else is displayed by the program.)

Automatic Landscape Builder

What is the windspeed in miles per hour? 3.5
How many hours have passed? 2.5

Here is your picture!
The horizontal coordinate for the top-left corner is 219

Press the Enter key when done.

You will need to do some arithmetic to determine precisely where the cloud should go:

  • If you divide the number of pixels across by the number of miles across, this will tell you how many pixels are in a mile.
  • If you multiply the wind speed times the number of hours that have passed, this will tell you how many miles the cloud has traveled.
  • If you multiply the number of miles that the cloud has traveled by the number of pixels in a mile, this will tell you the number of pixels in the image that the cloud has traveled.
  • After you have figured out the number of pixels that that the cloud has traveled, you have to round it off to an integer. (A cloud cannot start at half a pixel.) Here is sample code that does rounding:
num1 = 3.2
num2 = round(num1)

This problem can be solved purely with arithmetic: using more advanced programming obscures the fact that there is a straightforward and more efficient approach. Even if you know how to do them, do not use loops, if statements, or other more obscure Python statements that would achieve the same effect.

4 Test your code

Here are some things you can do to test your code:

  • If windspeed = 20 and hours = 0.5, the left side of the cloud should appear halfway across the window, and the horizontal coordinate should be 250.
  • If windspeed = 10 and hours = 1, the left side of the cloud should appear halfway across the window, and the horizontal coordinate should be 250.
  • If windpseed = 10 and hours = 1.5, the left side of the cloud should appear 3/4 of the way across the screen, and the horizontal coordinate should be 375.
  • Do some more examples on your own, work out by hand what the results should be, and see if your program gets them right. We will test your program on more examples than the above. The above tests are not sufficient: it is possible to write code that gets the above tests correct but will fail on others.
  • Make sure to also to test edge cases, such as when the windspeed and/or hours is negative, or when the inputs you give cause the cloud to be drawn outside of the window. Your program should also give correct answers for negative inputs. A negative windspeed means it is moving in the opposite direction; negative time means you're projecting backwards in time.

5 Style

You should make sure that your program follows good style. You should it as readable as possible for someone else trying to understand them. At a minimum, you should put a comment at the top explaining what the program does, and use comments above consecutive portions of Python code explaining what they do. You should also use meaningful and readable variable names.

6 Submit your work

When finished, zip up your code and submit your work through Moodle.

Good luck, and have fun! Remember that lab assistants are available in the evenings in CMC 102 and CMC 306 to help out if you need it, and you can attend prefect sessions as well.

Author: Dave Musicant

Emacs 24.5.1 (Org mode 8.2.10)

Validate