Learn to code with step-by-step lessons. A place for students to work through programming fundamentals and build skills.
← Step 4 · Tutorial 1.1 · Step 6 →
Right now, when you press rotate the piece always switches to the next offset list. So the piece can rotate into the wall or into locked blocks and look like it’s overlapping. That’s wrong: rotation should only be allowed if the new position is valid.
Use the same try → check → rollback pattern as for movement:
offset_num to the next state.check_collision()).offset_num.So rotation is only applied when the rotated shape doesn’t go out of bounds and doesn’t overlap the board.
rotate(), before changing offset_num, save the current value (e.g. old_offset = self.offset_num or use copy.copy).self.offset_num = (self.offset_num + 1) % len(self.offsets).check_collision(). If it returns True, set self.offset_num back to the saved value.rotate(1) on the rotate key.check_collision() already looks at the piece’s current origin and offsets[self.offset_num]. So after you change offset_num, calling check_collision() checks the *new* shape. If it overlaps the wall or the board, we rollback.
offset_num, not origin.
len(self.offsets) == 1 and rotation never changes the shape. That’s fine - the rollback logic still works; we just never see a different rotation.
Rotating near a wall or near locked blocks should only change the shape when the new shape fits. If it would overlap, the piece should stay in the previous rotation. Compare with the solution when ready.
Then go to Step 6 - Clearing full rows.