This week we will be discussing a third-party python library called pygame. With pygame we can easily make appealing and interactive video games. Pygame still uses python 2.6.
The assignment: Turn in game.py
, any interactive game written in python using the Pygame library except for the whack-a-mole game. Notice that the due date is in January, so your reward for turning in this assignment is straight up cool points.
Do a new object-oriented assignment that practices today's material on Python classes and objects. There will be two files to turn-in. The first is a file named date.py
that implements a class Date
, where each Date
object represents a single calendar month/day date such as September 19. Each Date
object should have the following behavior:
days_in_month
method to return the number of days in that Date
object's month.next_day
method that changes the state of the Date
by advancing it 1 day in time. For example, the next day after 9/19 is 9/20; the next day after 9/30 is 10/1; and the next day after 12/31 is 1/1__str__
method to convert the Date
into a string such as "9/19"
.
For example, it should be possible to use the Date
class in the following way at the interpreter:
>>> d = Date(9, 29) >>> d.month 9 >>> d.day 29 >>> d.days_in_month() 30 >>> d.next_day() >>> d.month 9 >>> d.day 30 >>> d.next_day() >>> d.month 10 >>> d.day 1 >>> d.days_in_month() 31 >>> str(d) # calls __str__ '10/1' >>> d.next_day() >>> str(d) # calls __str__ '10/2'
The second file to submit is called birthday2.py
. This file should be a client program that uses your Date
objects to find out how many days it is until the user's next birthday. The following is a sample run of the birthday2
program:
Please enter today's date: What is the month? 7 What is the day? 24 Please enter your birthday: What is the month? 11 What is the day? 6 Your next birthday of 11/6 is in 105 day(s).
Notice that this version of the program doesn't print the absolute day of the year for today or the birthday. You should figure out how many days it is until the user's birthday in a different way. Starting from today, if you walk forward in time until the user's birthday, one day at a time, counting how many steps you took, you'll know how many days it is until the user's next birthday. You don't need to handle the "birthday is tomorrow" case this time; just either print Happy Birthday! or print how many day(s) until the next birthday.
The reward for turning in this program is 1 extra late day for use on your normal Java programs, and +1 charisma and 2 saving throws. (Since it's the end of the quarter, getting extra late days is less motivating than it used to be. Mostly you'd just be doing this one for the learning experience.)
If you missed any of the python lectures in the past and you still would like to learn, then come to the python lecture this week. We will be reviewing all of the old material as well as introducing some new things.
The assignment: Turn in one of the previous homework assignments if you didn't get a chance to before. The reward for this program is 1 extra late day. Instructions: for your first file, name it review.py
. Any subsequent homeworks should have the name of the assignment such as song.py
or guessing_game.py
.
The assignment: Turn in a file guessing_game.py
containing a Python version of your HW5 guessing game program. The reward for turning in this program is 1 extra late day for use on your normal Java programs.
For an extra challenge: implement a two-dimensional guessing game, still named guessing_game.py
, according to these specifications.
The program will look like this:
This program is a 2-D guessing game. I will think of a point somewhere between (1, 1) and (20, 20) and give hints until you guess it. Guess x: 5 Guess y: 7 You're cold. Go south east Guess x: 18 Guess y: 5 You're cold. Go south west Guess x: 15 Guess y: 2 You're warm. Go north west Guess x: 12 Guess y: 3 You're hot! Go north west Guess x: 11 Guess y: 4 You got it right in 5 guesses! Overall results: Games played = 1 Total guesses = 5 Guesses/game = 5.0
The assignment: Turn in a file birthday.py
containing a Python program that produces output such as the output below. The task is identical to the Java HW4 except that you will also print out the user's astrological sign such as Leo or Virgo. At the bottom of your program, you should print an interesting fact about your own astrological sign, or your sign's current horoscope. Check here to find the date ranges for each sign, or look at the following table:
Sign | Date Range | Absolute Day Range |
---|---|---|
Aquarius | 21 January - 18 February | 21 - 49 |
Pisces | 19 February - 20 March | 50 - 79 |
Aries | 21 March - 20 April | 80 - 110 |
Taurus | 21 April - 21 May | 111 - 141 |
Gemini | 22 May - 23 June | 142 - 174 |
Cancer | 24 June - 22 July | 175 - 204 |
Leo | 23 July - 23 August | 205 - 235 |
Virgo | 24 August - 22 September | 236 - 266 |
Libra | 23 September - 23 October | 267 - 296 |
Scorpio | 24 October - 22 November | 297 - 327 |
Sagittarius | 23 November - 21 December | 328 - 355 |
Capricorn | 22 December - 20 January | 356 - 365 and 1 - 20 |
The following is an example output from your program. Notice that for simplicity, the month and day are read on separate lines.
This program tells you how many days it will be until your next birthday. Please enter today's date: What is the month (1-12)? 7 What is the day (1-31)? 24 7/24 is day #205 of 365. Please enter your birthday: What is the month (1-12)? 11 What is the day (1-30)? 6 11/6 is day #310 of 365. Your next birthday is in 105 days. You are a Scorpio. << your sign's fact or horoscope here >>
The assignment: Turn in a file circles.py
(lowercase c) containing a Python version of your HW3 Circles program that produces the output shown below, essentially the same as the Java version but drawn in Python. The window size and coordinates of all drawn figures are the same as in the Java version. As a reference, our sample solution is 40 lines long. The reward for turning in this program is 1 extra late day for use on your normal Java programs.
You won't be able to check your output since our Python DrawingPanel doesn't know how to compare images. But we will be lenient on whether the graphical output exactly matches. If you like, you can play around a bit with the exact colors and coordinates to produce a figure you like better, so long as the overall appearance closely matches the above screenshot.
Producing the above output will count for full credit, but if that sounds too easy for you and you want an added challenge, you can instead make your circles.py
program produce the output below, drawing "globe" figures instead of the normal target figures. This is harder, but probably not as hard as it looks! Read below to see how to do it.
The assignment: Turn in a file spaceneedle.py
(lowercase s) containing a Python version of your HW2 Space Needle program, producing the same output. The program should use any feature of Python that you have seen so far to simplify the program. As a reference, our sample solution is 35 lines long. The reward you'll get for turning in this program is 1 extra late day for use on your normal Java programs.
The assignment: Turn in a file Song.py
(uppercase S) containing a Python version of your HW1 Song program, producing the same output. As a reference, our sample solution is 65 lines long. The reward you'll get for turning in this program is 1 extra late day for use on your normal Java programs.
This quarter in CSE 142, we will conduct a special optional program to offer students a chance to learn a second programming language as you're learning Java. The second language's name is Python.
Python is a language that's good for writing programs to process text and other data. It's used heavily in the Linux operating system and at companies like Google.
Learning two programming languages is a bit like growing up in a bilingual family: you'll not only learn those two languages well, but you may also learn some higher concepts about programming and programming languages in general.
In addition, Python is used in a lot of other fields and disciplines, so it can be useful to have experience in it. Lastly, Python is a powerful language that does a lot of things more easily than Java, so it can be fun rewriting your past Java programs in Python and seeing how much shorter and cleaner they can be solved.
Our Python program will be hosted by some of our TAs, under the advisement of the instructor. Each week, they will hold a 50-minute session to teach you the equivalent of that week's Java course material into Python, along with any related issues.
The work involved in this program would be the following:
Primarily, these projects will consist of solving the same problem as that week's Java programming assignment, but in Python, and perhaps with minor modifications to the assignment spec.
Participation is entirely optional. The reward for doing these projects will be small, to make sure that these sessions don't give students with prior experience an unfair advantage over new programmers. Right now, we're planning to reward students with 1 free late day for each Python program submitted. No grade points will be added or subtracted in any way for participating in this project.
Just go to the next Python session at the time listed above, and if you find it interesting, try writing the Python program given out for that week. If you finish it, you can turn it in from a link that we'll put at the top of this page.
If you want to install and run Python programs on your own computer, follow our Python Installation Instructions below:
Yes! If you miss the session, feel free to look at each week's slides and code that we post, and/or still submit the program. And even if you come to the sessions, you don't have to work on the programs we give out; if you just want to come to the sessions and that's all, that is fine with us. It's up to you.