Sep-08-2017, 11:32 AM
I am new to the Python world and am having a bit of trouble trying to figure out a solution. Hopefully someone here can help this newbie out.
I am trying to make a simple game for practice in learning how to use classes. I have created a Tile class and a Board class. The board class is creating multiple Tiles but I seem to be having trouble changing an attribute of the Tile from the Board class.
tile.py
I am trying to make a simple game for practice in learning how to use classes. I have created a Tile class and a Board class. The board class is creating multiple Tiles but I seem to be having trouble changing an attribute of the Tile from the Board class.
tile.py
class Tile: """ Class for the tiles in the game. """ def __init__(self): """ Initialize the tiles with colors set to 0. """ self.red = 0 self.green = 0 self.blue = 0 def get_color(self): """ Returns the tile color in RGB. """ return self.red, self.green, self.blue def change_color(self, color): """ Change the tile color depending on current state. """ # Color is 0, 1, or 2 for RGB. if color == 0: if self.red == 0: self.red = 255 elif self.red == 255: self.red = 0 elif color == 1: if self.green == 0: self.green = 255 elif self.green == 255: self.green = 0 elif color == 2: if self.blue == 0: self.blue = 255 elif self.blue == 255: self.blue = 0board.py
from tile import Tile class Board: """ Class for the game board. """ def __init__(self, size): self.size = size self.board = [[Tile] * self.size for i in range(self.size)] self.board_won = False def change_tile_color(self, row, col, color): temp_row, temp_col = [row - 1, row, row + 1], [col - 1, col, col + 1] row_set, col_set = [], [] for i in range(3): if 0 <= temp_row[i] <= self.size - 1: row_set.append(temp_row[i]) if 0 <= temp_col[i] <= self.size - 1: col_set.append(temp_col[i]) for r in row_set: for c in col_set: self.board[r][c].change_color(color) b = Board(5) b.change_tile_color(2, 4, 0)when I run board.py I get
Error:TypeError: change_color() missing 1 required positional argument: 'color'
I am using Python 3.6.2. I have tried using super() also, but don't think I am doing it correctly.