Unformatted text preview:

6.189 Homework 4 Readings How To Think Like A Computer Scientist: Wednesday: Make sure you’ve finished Chapters 12-14 (all), & Chapter 16 (all); Thursday - get all readings finished! What to turn in Turn in a printout of your code exercises stapled to your answers to the written exercises by 2:10 PM on Tuesday, January 18th. Exercise 4.1 – Inventing The Wheel 1. Graphics setup • Download the graphics.py file from the Materials section of the course webpage. Make sure to save it in the directory you’ll be doing your work in. • Run the module as if it were a Python program (open it in IDLE and run it). If everything was done correctly you will get a demo window with a triangle and some text. GET AN LA’S HELP if this doesn’t work right on your machine! • Documentation on the graphics module is on the handout given in class, and also on the Materials section of the course webpage. 2. Basic Graphics Application Here is a skeleton of a new graphics program: from graphics import * #add any functions or classes you might define here # create a window with width = 700 and height = 500 new_win = GraphWin(’Program Name’, 700, 500) # add your code below this point new_win.mainloop() 13. Animating the wheel Download the file wheel.py from the Materials section of the course webpage (it is exactly the same as what you saw in class today). Run it and make sure that a wheel appears in a pop-up window. Now let’s try to add an animate method that would move the wheel across the screen. We will make use of the move method in the wheel class that moves the object dx units in the x direction and dy units in the y direction. Here is what the animate method will look like: from graphics import * class Wheel: ... def animate(self, win, dx, dy, n): if n > 0: self.move(dx, dy) win.after(100, self.animate, win, dx, dy, n-1) The animate method has 4 parameters - a GraphWin object win, the units by which to move the object in the x and y directions, dx and dy, and the number of times to call the animate method, n. The animation will stop when n = 0. The interesting part here is the after method of the GraphWin object. The first parameter is the time in milliseconds after which the GraphWin object will call the animate method again. The second parameter is the function object the GraphWin object needs to call; in our case it is the animate method of the Wheel object. As we mentioned in class, in Python everything is an object - even functions/methods - and they can be passed as parameters to other functions/methods. The rest of the parameters are the new parameters to the animate method. Note that we decrement n by 1 every time we setup a new call to animate. Now, write a program that uses the updated Wheel class and create a Wheel object (you can pick the colors1 of the tire and wheel to be anything you want) and make it move the wheel across the screen by 1 unit in the x direction 100 times. Remember you first need to draw the wheel before you can move it. Exercise 4.2 – Drawing A Car 1. Drawing Rectangles To display a rectangle, you need to specify two points: the upper left corner and the bottom right corner. Remember our y-axis is flipped. Make a file car.py and try the code below: from graphics import * new_win = GraphWin("A Car", 300, 300) rect = Rectangle( Point( 10,10), Point(200, 100 ) ) rect.setFill( "blue" ) rect.draw( new_win ) new_win.mainloop() Run your program and make sure that the rectangle appears on the screen. Try changing the color and width of the outline of the rectange. Look at the setOutline and setWidth methods. 1There’s a list of available colors in the file rgb.txt on the course webpage. 22. Drawing the car In this exercise, we will create a class for a car that will use the Wheel class from exercise 1. To do that, include the line from wheel import * directly underneath the line from graphics import *. Be sure that wheel.py and car.py are saved in the same directory; this will enable you to use your definition of Wheel from 4.1 instead of redefining it. Ask an LA if you’re confused about this. The car will contain 3 attributes: two Wheel objects and one Rectangle object (the body of the car) that is horizontal and whose bottom corners correspond to the centers of the wheels. Below is an example on how to use the Car object. Try to figure out how the class Car should be defined based on the way it is used. new_win = GraphWin(’A Car’, 700, 300) # create a car object # 1st wheel centered at 50,50 with radius 15 # 2nd wheel centered at 100,50 with radius 15 # rectangle with a height of 40 car1 = Car(Point(50, 50), 15, Point(100,50), 15, 40) car1.draw( new_win ) # color the wheels grey with black tires, and the body pink car1.set_color(’black’, ’grey’, ’pink’ ) # make the car move on the screen car1.animate(new_win, 1, 0, 400) new_win.mainloop() The size of the wheel is given only by the radius of the tire circle. You can compute the radius of the wheel circle as a percentage of the radius of the tire circle, e.g. 60%. Exercise 4.3 – Tetrominoes This exercise should be done after you’ve completed the written exercises. You may want to wait until after Thursday’s lecture to start. Tetris is deemed by some to be the most popular video game of all time. It is a puzzle game developed by Alexey Pajitnov in 1984 while he was working at the Academy of Science of the former USSR in Moscow. There have been hundreds of variants of the game developed since. We are going to create our own version of the basic Tetris game for the final project. The goal of this exercise is to get familiar with the game and to create the shapes (also called tetrominoes) used in the game. Here are several links where you can get a taste of the game if you’ve never played it before -http://www.tetrisfriends.com/games/Survival/game.php (fancy gui) and and http://vadim.oversigma.com/games/gbt.html (uses the Green Building as a screen for playing the game). Just remember you need to stop playing at some point :D. Each of the tetrominoes has 4 blocks; a block is a rectangle (more specifically, a square). Notice some relationships here - ’Has’ signifies containment, or an attribute, while ’is’ signifies


View Full Document

MIT 6 189 - Homework 4

Download Homework 4
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Homework 4 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Homework 4 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?