You belong in this class!

Full power ahead!

This week we will introduce some classes into our well of code knowledge. Classes, you ask? What are they good for?

Once again we will use the Tetris to try and figure this one out. The way I see classes is that they are a sort of wrapper around a bunch of functions. For example, we have a shape which is able to move left, right and down on our board. This shape can also rotate, it mustn’t go out of the border limits and also mustn’t move to the space which is already occupied. So, each shape has to do a lot of stuff. For each bit of that stuff we can write a function; for example:

def shape_can_move(coordinates):
  if board_free(coordinates):
    return True
    return False
def shape_move():
  if shape_can_move(shape.coordinates):
    shape.coordinates = (x, y)

The problem starts when there is more than one shape, because the program can’t really read our minds and know which shape exactly we want to move. The idea is, to introduce a class called Shape, like this:

class Shape():

  def __init__(self, coordinates, color):	    
	self.shape = draw_the_blocks(coordinates)		
  def can_move(self, coordinates):
    if board_free(coordinates):
      return True
      return False
  def move(self, x, y):
    if shape_can_move(x, y):
      self.shape.coordinates = (x, y)

The def __init__() is called a constructor. This is the first function or rather the method that gets called automatically when the shape is created. Now we can create many shapes and have no problem telling the computer which of them we want to move around. The code to create one shape could look something like this:

shape = Shape((x, y), 'deep pink')

I created an instance of a class Shape. Now I want it to move around:

shape.move(new_x, new_y)

The self part of the attribute in functions always has to be there. If functions are declared in a class they are called “methods”. When you actually want to use the method of a class, you can omit the self parameter, but you must send other parameters if there are any. In the case above I also had to send new coordinates (new_x and new_y).

Wow, this really sounds complicated. Let me tell you, right now I feel like I don’t know anything about anything. The one thing I do know, that despite my feelings, persistence will pay off. If you feel you want to give up now, come to the discussion forums and tell me all about it. We can ask others for their support. Let’s be brave today!

By the way, have you talked to Erika on the forum yet? She told me, she used to be a hairdresser, but now she is a Python programmer. I wonder how long it took for her to get the hang of this classes business. The forum is full of interesting people, and they know a lot…a lot. Find your way there, you will not regret it.

This week’s materials explain classes in more depth:


What are those pesky classes that programmers are always talking about? Find out in more depth in Chapter 12, Chapter 13 and Chapter 14 from the online textbook How to Think Like a Computer Scientist.

Trying it out

Codecademy - How do you like it? Let’s work through unit Introduction to classes this week.


From the MIT OCW’s course A Gentle Introduction to Programming Using Python

  • First: Complete sections 3.10 and 3.11 from Written Exercises 3

  • Second: Complete sections 3.6 and Optionals Exercises 1 & 2 from Handout 3


It is time that we get really serious about our Tetris game! We now know most of the code building that is needed to build it properly, so let’s not hesitate and start with Section 1 & 2 of the Final Project

Please Note: The file referred to in the above handout is available on the assignments page of the MIT OCW’s course materials. You can also download it here.

Come to the discussion forums and let’s discuss how we will apply our knowledge to see this project through together.

Show off them mad skills!

Much happy projecting.

moocie face
–Your mechanical friend MOOC-E