Learn to code with step-by-step lessons. A place for students to work through programming fundamentals and build skills.
← 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:
check_collision() uses the new offsets[self.offset_num], so it detects if the rotated shape is out of bounds or overlapping the board. If so, we restore offset_num so the piece stays in the previous rotation.Next: clear full rows and add scoring (Step 6).