Automatic Landscape, Redux
Table of Contents
This is a pair programming assignment. If you are working in a pair, this means that you and your partner should be doing the entirety of this assignment side-by-side, on a single computer, where one person is "driving" and the other is "navigating." Set a timer to swap every 15 minutes. You can choose your favorite from this online timer page. Make sure your sound volume is audible, but not so loud to disturb the people around you.
If you are working in a pair, only one of you needs to submit your work via Moodle. That said, you should both have a copy of your work in case you want it someday, so make sure that both of you have copies of it; you can email it or use some other mechanism to transfer it.
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
In this automatically adjustable image, you will draw a sun or moon in the sky of a landscape. But this time, you'll need to handle the fact that the sun and moon cross the sky every day.
2 Draw a landscape, sun, and moon
If you wish to use the landscape that one of you did for the previous assignment as a starting point, feel free. Alternatively, you can start over from scratch if you like. Create a directory named landscape2 to store your work, then copy into it graphics.py. Again create a landscape in a 500 x 700 canvas.
Draw a sun (yellow circle) in the top left corner of the window, and a moon (a white circle) in the top right corner.
3 Make the position of your sun and moon depend on time
The goal of this part of the assignment is to place the sun (or moon) in different places in the sky, depending on time of day. For the sun:
- at 6 AM, the center of the sun appears in the first pixel on the the left side of the window (so you see exactly half the sun)
- at 8 PM, the center of the sun appears at precisely the first pixel that's not visible off the right edge of the window.
Likewise, for the moon:
- at 8 PM, the center of the moon appears in the first pixel on the the left side of the window (so you see exactly half the sun)
- at 6 AM, the center of the moon appears at precisely the first pixel that's not visible off the right edge of the window.
Your program should ask your user how many hours have passed since midnight. The user should then enter an number (not necessarily an integer) indicating how many hours have passed. Thus if the user enters 9, this means that it is 9 AM. If the user enters 23, this means that it is 11 PM. If the user enters 25, this means that it is 1 AM the next day. Your program should then:
- draw the sun or the moon in its correct location
- display the horizontal location of the center of the sun, and of the center of the moon. Because your code will not use an
ifstatements, you should display locations for both the sun and the moon, even if one of them is not visible. We will only check for correctness on the celestial bodies that are actually in view. In other words, if the moon is showing in your window and not the sun, we will only check for correctness the pixel location for the moon.
Here are some samples of what the interaction between your program and your user should look like. (The number 7.5 is entered in by the user; everything else is displayed by the program.)
Automatic Day/Night Builder How many hours have passed since midnight? 6 Here is your picture! The center of the sun, in pixels from the left side of the screen, is 0 The center of the moon, in pixels from the left side of the screen, is 500 Press enter when done.
Automatic Day/Night Builder How many hours have passed since midnight? 20 Here is your picture! The center of the sun, in pixels from the left side of the screen, is 500 The center of the moon, in pixels from the left side of the screen, is 0 Press enter when done.
Automatic Day/Night Builder How many hours have passed since midnight? -4 Here is your picture! The center of the sun, in pixels from the left side of the screen, is 500 The center of the moon, in pixels from the left side of the screen, is 0 Press enter when done.
Automatic Day/Night Builder How many hours have passed since midnight? 13 Here is your picture! The center of the sun, in pixels from the left side of the screen, is 250 The center of the moon, in pixels from the left side of the screen, is 850 Press enter when done.
(In the last example above, it doesn't matter what value you show for the moon's location, since it isn't visible.)
You will need to do some arithmetic in a similar fashion to the last assignment to determine precisely where the sun or moon should go. This is all entirely doable using the five basic arithmetic operators, namely +, -, *, /, and/or %, and possibly round.
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
As in the last assignment, you should test your code. 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 similarly.
Make sure to also to test edge cases, such as when the number of hours is 0, very large, or negative; also think about if it is possible for the sun and moon to appear at the same time, or for neither to appear.
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 The last point
If you complete the above successfully (and have good style in your code), you will receive nearly all the points for the assignment. If you get this far, you should feel proud of your achievements! If you want to push yourself harder and go for the last point, do at least two of the following (that may or may not actually reflect nature):
- Adjust the color of the sun so that it smoothly drifts over the course of the day from a bright yellow in the morning to a dark orange in the evening.
- Adjust the height of the sun so that it is at the very top of the screen halfway through the day, but it is somewhat lower in the morning in the evening. Do similarly with the moon. One possible way to do this is to use some of the functions in the math module. You can access them by putting
from math import *at the top of your program, and you can read about what's available from the math module online documentation. You can likely do this with parabolas, trigonometry, or other approaches. - Figure out how to make your moon look like a crescent. Overlapping two circles is one way to do it. Make sure, though, that the output you give for the "center" of the moon is the same as you gave when it was just a circle, since that's one of the ways we're going to check for correctness.
7 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.