The goal is to create a code for implementing a Columns game using pygame Your p
ID: 3724206 • Letter: T
Question
The goal is to create a code for implementing a Columns game using pygame
Your program will read its input via the Python shell (i.e., using the built-in input() function), printing no prompts to a user with no extraneous output other than precisely what is specified below. The intent here is not to write a user-friendly user interface; what you're actually doing is building a tool for testing your game mechanics, which we'll then be using to automatically test them. So it is vital that your program reads inputs and write outputs precisely as specified below. You can freely assume that the input will match the specification described; we will not be testing your program on any inputs that don't match the specification.
First, your program needs to know the size of the field. It will always be a rectangle, but the number of rows and columns can vary.
First, your program reads a line of input specifying the number of rows in the field. You can assume this will be no less than 4.
Next, your program reads a line of input specifying the number of columns in the field. You can assume this will be no less than 3.
At any given time, the field contains jewels that are one of seven colors. The colors are represented by these uppercase letters (and only these letters): S, T, V, W, X, Y, Z. In both the input and output, we'll use these seven letters to denote the seven colors.
Now, your program needs to know what jewels are in the field to begin with. There are two situations: We might want to start with an empty field, or we might want to specify the contents of the field in the input.
If the field is to begin empty, the word EMPTY will appear alone on the next line of input.
If instead we want to specify the contents of the field in the input, the word CONTENTS will appear alone on the next line of input. Given that there are r rows and c columns in the field, there would then be r lines of input, each of which will contain exactly c characters; these characters represent the contents of each of the field's cells to start with.
For a cell that should contain a jewel of some color, an uppercase letter describing each color will be used.
For a cell that should be empty, a space will be used instead.
Note that when we're specifying the contents of the field explictly, the spaces will always be present in the input for every cell that's empty; the program should expect to read exactly the correct number of characters.
At this point, the game is ready to begin. From here, we will repeatedly do two things: Display the field, then read a command from the user.
The rules for displaying the field are:Given that the field has r rows, the field will be displayed as a total of r + 1 lines of output. The first r will correspond to the r rows of the field, which each row displayed like this:The vertical bar character '|', followed by three characters for each of the c columns in that row, followed by another vertical bar character '|'. For each column in that row, the three characters will be:
Three spaces if the cell is empty
A space, followed by an uppercase letter if the cell contains a jewel that has been frozen.
A left bracket character '[', followed by an uppercase letter, followed by a right bracket character ']' if the cell contains a jewel that is part of the faller (if any).
A vertical bar character '|', followed by an uppercase letter, followed by another vertical bar character '|' if the cell contains a jewel that is part of a faller that has landed but not yet frozen.
An asterisk character '*', followed by an uppercase letter, followed by another asterisk character '*' if the cell contains a jewel that has frozen and has been recognized as a match.
After the last row of the field, a space, followed by 3c dashes, followed by another space is displayed.
The commands that you would read are:A blank line, which is a crude representation of the passage of time. (In our complete game, this would happen without any input; instead, when a certain amount of time passes, we would see the appropriate effect.)
If there is a faller present, it falls; if there is a faller that has landed (and has not been moved so that it is no longer in a landed position), it freezes; and so on.
F, followed by an integer that is a column number (the columns are numbered 1 through c, if there are c columns), followed by a space, followed by three uppercase letters (representing colors), each of these things separated by spaces (e.g., F 1 S T V). This means to create a faller in column 1, with a jewel of color S on the top, a jewel of color T below it, and a jewel of color V below that.
The faller begins with only the bottommost of the three jewels visible. See the example outputs below for more details.
Note that there can only be one faller at a time, so this command has no effect if there is a faller that has not already been frozen.
R alone on a line, which rotates the faller, if there is one. If there is no faller currently, this command has no effect. Note, though, that it is possible to rotate a faller that has landed but not yet frozen.
< alone on a line, which moves the faller one column to the left, if there is one (and if it not blocked by jewels already frozen on the field or by the edge of the field). If there is no faller or the faller can't be moved to the left, this command has no effect. Note, though, that it is possible to move a faller that has landed but not yet frozen, which can take it out of its "landed" status (if it moves to a column with nothing underneath it).
> alone on a line, which moves the faller one column to the right, if there is one (and if it not blocked by jewels already frozen on the field or by the edge of the field). If there is no faller or the faller can't be moved to the right, this command has no effect. Note, though, that it is possible to move a faller that has landed but not yet frozen, which can take it out of its "landed" status (if it moves to a column with nothing underneath it).
Q alone on a line, which means that to quit the program.
There are two ways for the program to end:
If the user specifies the command Q, the program ends, with no additional output being printed.
When a faller freezes without all three of its jewels being visible in the field (for example, if it lands on a jewel that's two rows below the top and then freezes), the game ends, so the program ends, as well. In that case, you would print GAME OVER before ending the program.
Explanation / Answer
class Faller:
def __init__(self, col, color1, color2, color3):
self.col = col
self._color1 = color1
self._color2 = color2
self._color3 = color3
self._faller = self._generateFaller()
self.is_frozen = False
self.is_landing = False
self._faller_count = 0
self._bottom_count = 0
def getFaller(self) -> list:
return self._faller
def getCol(self) -> int:
return self.col
def rotateFaller(self):
self._faller = self._faller[-1:] + self._faller[-3:-1]
def _generateFaller(self) -> list:
faller = []
faller.append(self._color1)
faller.append(self._color2)
faller.append(self._color3)
return faller
def addFrozen(self):
self.is_frozen = True
def addLanding(self):
self.is_landing = True
#tick -> ui
class GameBoard:
def __init__(self, row: int, column: int, field: str):
self._row = row
self._column = column
self._field = field
self._board = self.getField()
#match, land
def getRow(self) -> int:
return self._row
def getColumn(self) -> int:
return self._column
def getList(self):
return self._board
def getField(self) -> [[str]]:
if self._field == 'EMPTY':
self._board = self._generateEmptyField()
return self._board
elif self._field == 'CONTENTS':
self._board = self._generateContentField() #wip
return self._board
###
def startFall(self, block):
self._board[0][int(block.col) -1] = '[' + block._faller[2] + ']'
block._bottom_count = 1
block._faller_count = 1
def falling(self, block):
if block._faller_count < self._row:
if block._faller_count > 0:
self._board[block._faller_count][int(block.col) -1] = '[' + block._faller[2] + ']'
self._board[block._faller_count-1][int(block.col) -1] = '[' + block._faller[1] + ']'
if block._faller_count > 1:
self._board[block._faller_count][int(block.col) -1] = '[' + block._faller[2] + ']'
self._board[block._faller_count-1][int(block.col) -1] = '[' + block._faller[1] + ']'
self._board[block._faller_count-2][int(block.col) -1] = '[' + block._faller[0] + ']'
if block._faller_count > 2:
self._board[block._faller_count-3][int(block.col) -1] = ' '
block._faller_count += 1
block._bottom_count += 1
self.landing(block)
def landing(self, block):
'''
block - faller object
self._faller_count - the row # of the lowest jewel
'''
# TODO: Implement check if above jewel
if block._bottom_count == self._row and (self._board[block._bottom_count-1][int(block.col) -1] != ' '):
self._board[block._bottom_count-1][int(block.col) -1] = '|' + block._faller[2] + '|'
self._board[block._bottom_count-2][int(block.col) -1] = '|' + block._faller[1] + '|'
self._board[block._bottom_count-3][int(block.col) -1] = '|' + block._faller[0] + '|'
block.addLanding() ###
return True
return False
def checkBlock(self, block):
self._board[block._bottom_count-1][int(block.col) -1] = ' ' + block._faller[2] + ' '
self._board[block._bottom_count-2][int(block.col) -1] = ' ' + block._faller[1] + ' '
self._board[block._bottom_count-3][int(block.col) -1] = ' ' + block._faller[0] + ' '
block._bottom_count = 0
block.addFrozen()
def updateBlock(self, block):
if '[' in self._board[block._bottom_count-1][int(block.col) -1]:
self._board[block._bottom_count-1][int(block.col) -1] = '[' + block._faller[2] + ']'
self._board[block._bottom_count-2][int(block.col) -1] = '[' + block._faller[1] + ']'
self._board[block._bottom_count-3][int(block.col) -1] = '[' + block._faller[0] + ']'
else:
self._board[block._bottom_count-1][int(block.col) -1] = '|' + block._faller[2] + '|'
self._board[block._bottom_count-2][int(block.col) -1] = '|' + block._faller[1] + '|'
self._board[block._bottom_count-3][int(block.col) -1] = '|' + block._faller[0] + '|'
###move right, left, landing, frozen, rotate
###
def _generateEmptyField(self):
field = []
for col in range(self._row):
field.append([])
for row in range(self._column):
field[-1].append(' ') #3 spaces
return field
def _generateContentField(self): #wip
content = []
jewel = []
for col in range(self._row):
jewel.append(input())
for color in jewel:
temp = []
for t in color:
slot = ' ' + t + ' '
temp.append(slot)
content.append(temp)
return content
def displayField(game: GameBoard):
display = game.getList()
for space in display:
d = []
for tile in space:
d.append(tile)
print('|' + ''.join(d) + '|')
print(' ' + '-'*3*game.getColumn() + ' ')
if __name__ == '__main__':
print('Please enter how many rows.')
user_row = int(input())
print('Please enter how many columns.')
user_column = int(input())
print('Please enter EMPTY or CONTENTS for the option to fill the field or not.')
start = input()
board = GameBoard(user_row, user_column, start)
displayField(board)
####
while True:
user_create = input()
block = Faller(user_create[2], user_create[4], user_create[6], user_create[8])
print(block)
count = 0
board.startFall(block)
displayField(board)
while not block.is_frozen:
u = input()
if u == "R" and block.is_landing:
block.rotateFaller()
board.updateBlock(block)
displayField(board)
elif u == 'R' and not block.is_landing:
block.rotateFaller()
board.updateBlock(block)
displayField(board)
elif u == '' and block.is_landing:
board.checkBlock(block)
displayField(board)
else:
board.falling(block)
displayField(board)
#if the top one is not in the board yet
#check last one for < >
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.