Question: Build a graphical, playable version of Columns, based on the previous game mechanics. Please refer to the code of game mechanics below to build the

Build a graphical, playable version of Columns, based on the previous game mechanics. Please refer to the code of game mechanics below to build the program (changes can be made if necessary),thank you.
game_mechanics.py
class Column_Game:
def __init__(self, rows, cols, initial_state="EMPTY", initial_field=None):
self.rows = rows
self.cols = cols
self.field =[['' for _ in range(cols)] for _ in range(rows)]
self.faller = None
self.game_over = False
if initial_state != "EMPTY":
for i, line in enumerate(initial_state):
self.field[i]= list(line)
def display_field(self):
for row in self.field:
print("|"+"".join(self.format_cell(cell) for cell in row)+"|")
print(""+"---"* self.cols +"")
def format_cell(self, cell):
if cell =="":
return ""
elif isinstance(cell, list):
return f"[{cell[0]}]"
else:
return f"{cell}"
def add_faller(self, col, jewels):
if self.field[0][col]!='':
self.game_over = True
return
self.faller ={'col': col, 'jewels': jewels, 'row': 0}
self.field[0][col]= f"[{jewels[-1]}]"
def fall(self):
if not self.faller:
return
col = self.faller['col']
row = self.faller['row']
if row +1 self.rows and self.field[row +1][col]=='':
self.field[row][col]=''
self.field[row +1][col]= f"[{self.faller['jewels'][-1]}]"
self.faller['row']+=1
else:
self.freeze_faller()
def freeze_faller(self):
col = self.faller['col']
row = self.faller['row']
for i, jewel in enumerate(self.faller['jewels']):
if row - i >=0:
self.field[row - i][col]= jewel
self.faller = None
self.check_matches()
def rotate_faller(self):
if not self.faller:
return
self.faller['jewels']= self.faller['jewels'][1:]+ self.faller['jewels'][:1]
row = self.faller['row']
col = self.faller['col']
self.field[row][col]= f"[{self.faller['jewels'][0]}]"
def move_left(self):
if not self.faller:
return
col = self.faller['col']
row = self.faller['row']
if col >0 and self.field[row][col -1]=='':
self.field[row][col]=''
self.field[row][col -1]= f"[{self.faller['jewels'][0]}]"
self.faller['col']-=1
def move_right(self):
if not self.faller:
return
col = self.faller['col']
row = self.faller['row']
if col self.cols -1 and self.field[row][col +1]=='':
self.field[row][col]=''
self.field[row][col +1]= f"[{self.faller['jewels'][0]}]"
self.faller['col']+=1
def check_matches(self):
to_remove = set()
for r in range(self.rows):
for c in range(self.cols):
if self.field[r][c]=='':
continue
if c +2 self.cols and self.field[r][c]== self.field[r][c +1]== self.field[r][c +2]:
to_remove.update([(r, c),(r, c +1),(r, c +2)])
if r +2 self.rows and self.field[r][c]== self.field[r +1][c]== self.field[r +2][c]:
to_remove.update([(r, c),(r +1, c),(r +2, c)])
# Mark matches
for r, c in to_remove:
self.field[r][c]='*'
def is_game_over(self):
return self.game_over
Build a graphical, playable version of Columns,

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!