Tutorial 1.1 - Step 5 Solution

Learn to code with step-by-step lessons. A place for students to work through programming fundamentals and build skills.

Step 5 - Solution

← Step 5 · Tutorial 1.1 · Step 6 →


Here is one way to make rotation use try → check → rollback so we don’t rotate into walls or other pieces.

Updated rotate() on Piece (use import copy at top if you use copy.copy):

def rotate(self, n=1):
    old_offset = self.offset_num
    self.offset_num = (self.offset_num + n) % len(self.offsets)
    if self.check_collision():
        self.offset_num = old_offset

Or without importing copy (since it’s an int):

def rotate(self, n=1):
    old_offset = self.offset_num
    self.offset_num = (self.offset_num + n) % len(self.offsets)
    if self.check_collision():
        self.offset_num = old_offset

What’s going on:

Next: clear full rows and add scoring (Step 6).

← Back to Step 5 · Step 6 →