WTP Computer Science

Problem Set 8

Due: July 9, 2010




Exercise 1: Understanding Inheritance

This is a paper and pencil exercise.
Consider the following code:

class Spell(object):
    def __init__(self, incantation, name):
        self.name = name
        self.incantation = incantation

    def __str__(self):
        return self.name + ' ' + self.incantation + '\n' + self.get_description()
        
    def get_description(self):
        return 'No description'
    
    def execute(self):
        print self.incantation    


class Accio(Spell):
    def __init__(self):
        Spell.__init__(self, 'Accio', 'Summoning Charm')

class Confundo(Spell):
    def __init__(self):
        Spell.__init__(self, 'Confundo', 'Confundus Charm')

    def get_description(self):
        return 'Causes the victim to become confused and befuddled.'

def study_spell(spell):
    print spell

spell = Accio()
spell.execute()
study_spell(spell)
study_spell(Confundo())
  1. What are the parent and child classes here?





  2. What does the code print out? (Try figuring it out without running it in Python)











  3. Which get_description method is called when 'study_spell(Confundo())' is executed? Why?





  4. What do we need to do so that 'print Accio()' will print the appropriate description ('This charm summons an object to the caster, potentially over a significant distance')? Write down the code that we need to add and/or change.










Exercise 2: Tetris

Tetris is deemed by some the most popular video game of all times. 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. Last year the game celebrated 25 years from its creation. Here is an article in Times magazine covering the story and explaining the premise of the game. Google also created a special Tetris logo to celebrate the anniversary. Tetris has inspired a lot of atristic creations - Human tetris, playing the game on the side of the Brown University Sciences Library, Tetris skirt.

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. If you've never played it before try http://www.freetetris.org/. Also, see http://vadim.oversigma.com/games/gbt.html (this game uses MIT's green building as a screen for playing the game). Just remember you need to stop playing at some point.

Each of the tetrominoes has 4 blocks and a block is a rectangle (more specifically a square). (Notice relationships here? 'Has' signifies containment or an attribute. 'Is' signifies inheritance.) Here is an example of the basic tetrominoes.


Exercise 2.1: Blocks

The tetris board is typically 10x20 squares. Each block occupies a single square at a time.

Create a Block class that inherits from the Rectangle class and save it in a file called tetrominoes.py. It should have x and y attributes that correspond to the position of the block on the tetris board. The position (0,0) is the top left corner of the board and (9, 19) is the bottom right corner. Make your blocks have width of 30 pixels.

Here is how you can use the block and what it will display in a 5x5 board.

win = GraphWin("Tetrominoes", 150, 150)

# the block is drawn at position (1, 1) on the board
block = Block(Point(1, 1), 'red')
block.draw(win)

win.mainloop()
Hint: Think about how to initialize the Rectangle superclass

 


Exercise 2.2: Moving Blocks

Add a move method to your Block class that will take as parameters dx and dy telling the block to move dx squares in the x direction and dy squares in the y direction. Again, our y-axis will be pointing downwards.

Here is how you can move the block:

win = GraphWin("Tetrominoes", 150, 150)

# the block is drawn at position (1, 1) on the board
block = Block(Point(1, 1), 'red')
block.draw(win)
block.move(2, 1)

win.mainloop()
Hint: Think about how to reuse the Rectangle superclass move method

Exercise 2.3: Tetris Shape (Tetromino)

Create a Shape class that has a list of blocks as an attribute and a move and a draw methods. The move method will also take as parameters dx and dy telling the shape to move dx squares in the x direction and dy squares in the y direction. Here is the code for the I_shape class that is subclass of the Shape class.

class I_shape(Shape):
    def __init__(self, center):
        coords = [Point(center.x - 2, center.y),
                  Point(center.x - 1, center.y),
                  Point(center.x    , center.y),
                  Point(center.x + 1, center.y)]
        Shape.__init__(self, coords, "blue")

The parameter center is a Point that holds the position of the central block in the shape.

Once you have written you Shape class verify that it displays the I_shape correctly. Here is an example test:

win = GraphWin("Tetrominoes", 200, 150)


shape = I_shape(Point(1, 1))
shape.draw(win)
shape.move(2, 1)

win.mainloop()

In the code above, the third block of the I shape is originally drawn at position (1, 1) and then moved to position (3, 2).


Exercise 2.4: Many Tetrominoes

Now create a subclass for each of the other 6 shapes. One thing you need to know is which is the center block. Here are all the shapes with their center block marked in black.

Here is an example code for displaying all the shapes above (of course, all your blocks should be the same color).

win = GraphWin("Tetrominoes", 900, 150)
# a list of shape classes
tetrominoes = [I_shape, J_shape, L_shape, O_shape, S_shape, T_shape, Z_shape]
dx = 0
for tetromino in tetrominoes:
    # create a shape centered at row 1, column 3
    shape = tetromino(Point(3, 1))  	
    shape.draw(win)
    shape.move(dx, 0)
    dx += 4   
    
win.mainloop()

Note: you do not have to follow the color scheme of the tetrominoes shown in the picture.

Submit the file electronically by typing:

 athena% submit ps8 tetrominoes.py

What to turn in


  1. Turn in your answers to exercise 1 on this sheet to one of the tutors.
  2. Submit the electronic copy of the program you wrote in exercise 2. (Note: to submit type the following command at the athena prompt:)
     athena% submit ps8 tetrominoes.py