Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Multiple classes in another class
#1
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
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 = 0
board.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.
Quote
#2
You call method
'change_tile_color' in class board,
from which (line 20) you attempt to call method change_color of class Tile,
but you never instantiate class tile in class Board.
To do this you need a tile = Tile() in Board.
I would put that in the __init__ mathod of Board.
then line 20 of Board now becomes:
self.board[r][c].tile.change_color(color)
Quote
#3
After looking at this a bit more, I see you are embedding 'Tile' into self.board.
but Tile still has not been instantiated. This is just the address of class Tile
I'm not really sure what you are trying to do here.
Perhaps you could explain a bit more.
Quote
#4
@ Larz60+

Thanks for the quick response. That seemed to solve the issue.
Been using super() in all the books I have been reading but board is not a type of tile so didn't think that was the answer.

Leave it to a simple syntax error on my part. That's usually the issue, but not familiar enough with it to spot it on my own.
Thanks again!! Big Grin

I changed line 7 in board.py to:
self.board = [[Tile()] * self.size for i in range(self.size)]
not getting an error now. but doing further testing to make sure it is working correctly

Ok this still not acting the way I want. I am creating a Lightout game. Only this one has 3 different colors to it (RGB). So if you click on a tile on the board, that tile and all tiles around it change color. After using a looping print statement. I see nothing is changing. Why?

(Sep-08-2017, 12:26 PM)Larz60+ Wrote: After looking at this a bit more, I see you are embedding 'Tile' into self.board.
but Tile still has not been instantiated. This is just the address of class Tile
I'm not really sure what you are trying to do here.
Perhaps you could explain a bit more.

Ok this still not acting the way I want. I am creating a Lightout game. Only this one has 3 different colors to it (RGB). So if you click on a tile on the board, that tile and all tiles around it change color. After using a looping print statement. I see nothing is changing. Why?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pythonic way to handle/spread alerts class in multiple modules psolar 11 464 Feb-12-2020, 04:11 PM
Last Post: psolar
  Save all values to pandas of multiple classes jenniferruurs 0 197 Sep-13-2019, 12:10 PM
Last Post: jenniferruurs
  Return Object Created from Multiple Classes emerger 3 636 Oct-18-2018, 02:14 AM
Last Post: emerger
  Giving class multiple arguments AndyArsalan 1 777 Oct-04-2018, 11:25 PM
Last Post: ODIS
  calling a class multiple times GOB 2 1,134 Feb-02-2018, 05:10 AM
Last Post: GOB
  Using classes? Can I just use classes to structure code? muteboy 5 1,657 Nov-01-2017, 04:20 PM
Last Post: metulburr
  how to make class and instance method (multiple decorator) ? harun2525 7 2,429 May-29-2017, 04:56 PM
Last Post: harun2525
  Class method returning multiple values,dont know which is returned when.. ujjwalrathod007 4 2,124 Oct-03-2016, 08:29 PM
Last Post: ujjwalrathod007

Forum Jump:


Users browsing this thread: 1 Guest(s)