Python Forum
Calling on a Variable using other Variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling on a Variable using other Variables
#4
Instead of using a variable to store the color you could use a function that returns the color. As I said, somewhere you need code.

Your code is odd. I don't have any pygame experience at all, but I think you would want to some sort of collection object instead of having a named variable for each thing. Since the squares on the board are an important part of the game I might make a class to hold all the information for a square such as the location, the background color, and any chess piece landing on the square:
class Square():
    '''A location on the board'''
    def __init__(self, rank, file):
        self.rank = rank
        self.file = file
        self.name = 'abcdefgh'[rank]+str(file+1)
        self.background = [0,0,120] if (rank+file)%2 else [0,0,0]
        x = (rank)*100  # Not sure of pygame coordinates so this could be wrong
        y = (file)*100
        self.rectangle = [x, y, 100, 100]
        self.occupant = None

    def draw(self):
        '''Draw self on screen'''
        self.square = pygame.draw.rect(screen, self.color, self.rectangle)
        if self.occupant:
            '''Draw image for piece occupying square'''

    @property
    def piece(self):
        '''Return occupant'''
        return self.occupant

    @piece.setter
    def place(self, piece):
       '''Set the occupant.  Redraw the square'''
        self.occupant = piece
        self.draw()
In addition to storing information a class can also have actions. My Square class would have an action (method) to draw the square and another for adding or removing a chess piece. I might make the latter a property so I can use the same property to see or set what piece occupies a square.

The board is just a collection of squares, so I make a class that represents that.
class Board():
    '''A grid of Squares'''
    def __init__(self):
        self.ranks = []
        for rank in range(8):
            self.rank.append([Square(rank, file) for file in range(8)])

    def square(self, rank, file):
        """Return square at rank/file"""
        return self.rank[rank][file]

    def move(self, r1, f1, r2, f2):
        '''Move piece from r1, f1 to r2, f2'''
        self.square(r2, f2).piece = self.square(r1, f1).piece
        self.square(r1, f1).piece = None

    def draw(self):
        '''Draw the board'''
        for rank in self.ranks:
            for square in rank:
                square.draw()
Now I can make and draw a chess board in two lines:
board = Board()
board.draw()
And I can move a piece in one line
board.move(startRank, startFile, endRank, endFile)
Reply


Messages In This Thread
RE: Calling on a Variable using other Variables - by deanhystad - Jul-22-2021, 02:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling functions by making part of their name with variable crouzilles 4 883 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  Calling a base class variable from an inherited class CompleteNewb 3 1,746 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Create new variable dependent on two existing variables JoeOpdenaker 6 3,123 Oct-25-2020, 02:15 PM
Last Post: jefsummers
  Print variable values from a list of variables xnightwingx 3 2,673 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Calling Variables from Other Files in Different Folders illmattic 14 5,644 Aug-01-2020, 07:02 PM
Last Post: deanhystad
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,285 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  calling a variable in a for loop in windows dwaynes 1 1,833 Apr-02-2020, 05:21 PM
Last Post: mcmxl22
  setting pythonpath variable in environment variables saisankalpj 3 3,518 Dec-05-2018, 10:33 PM
Last Post: Gribouillis
  2D Array/List OR using variables in other variable names? IAMK 4 3,916 Apr-16-2018, 09:09 PM
Last Post: IAMK
  calling an object variable in a dictionary sunhear 3 4,345 Dec-30-2016, 05:28 PM
Last Post: sunhear

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020