0

I'm trying to draw squares in different colors to fill the screen in pygame.

class Box():
    def __init__(self, color, rect):
        self.color = color
        self.rect = rect

    def draw(self, surface):
        pygame.draw.rect(surface, self.color, self.rect)

Background class

class Background():
    def __init__(self, row, columun, ofset, size, color):
        self.row = row
        self.columun = columun
        self.ofset = ofset
        self.size = size
        self.color = color 

    def parse(self):
        return self.row * self.columun

    def extend(self):
        parse = self.parse()
        extend = []
        for tile in range(parse):
            if tile % 2 == 0:
                extend.append(Tile(self.color[0],pygame.Rect(self.ofset,self,size,self.size)))
            else:
                extend.append(Tile(self.color[1],pygame.Rect(self.ofset,self,size,self.size)))
            self.ofset[0] += self.size
            self.ofset[1] += self.size
        return extend

and inside my main loop I'm trying to draw it to the screen

while True:

    screen.fill((255,255,255))

    background = Background(5#row,10#columun,(0,0)#ofset,30#size,((255,255,255),(0,0,0))#multiple color)
    for i in sorted(enumerate(background.extend), reverse=True):
        i.draw(display)

Error:

line 71, in <module>
    for i in sorted(enumerate(background.extend), reverse=True):
TypeError: 'method' object is not iterable

I am not proficient in object oriented programming. is there any logic error in my code? Thanks for your help.

3
  • Do you mean background.extend()? You forgot to call the function. Commented Aug 5, 2021 at 21:50
  • aah yes i forgot that and used a comma instead of a period in some places.Do you think there is any logic error in my code? Commented Aug 5, 2021 at 21:59
  • That is the error. Commented Aug 5, 2021 at 22:04

1 Answer 1

2

You have a number of issues in your code. For the current error, you should call the method to get its return value. Instead of background.extend, write background.extend().

I'm not sure why you want to reverse the returned tiles of tiles, but there is a simpler way than looping through sorted(enumerate(...), reverse=True). Simply do reversed(...).

The Background class's offset instance variable should be a normal variable inside the extend method as otherwise the tiles would be drawn further and further off screen. Also, use a list [0, 0] instead of a tuple (0, 0) as tuples are immutable (meaning you can't do offset[0] += self.size).

You probably also meant to use the Box class instead of the Tile class (which I'm guessing was its previous name).

I'd also rename parse -> num_tiles and extend -> make_tiles or just something that's more descriptive of what it does.

Also note that you don't need a () to define a class.

I'm not sure if the comments inside the Background constructor were part of the code, but if they were, note that comments go until the end of the line, not just the next word. Do note that keyword arguments exist in Python, so you could use those too.

Oh and if you were trying to make a chess board, you shouldn't add to both the x offset and y offset on each tile. I'll leave that as an exercise for you.

End result:

class Box:
    def __init__(self, color, rect):
        self.color = color
        self.rect = rect

    def draw(self, surface):
        pygame.draw.rect(surface, self.color, self.rect)

class Background:
    def __init__(self, rows, columns, size, colors):
        self.rows = rows
        self.columns = columns
        self.size = size
        self.colors = colors

    def num_tiles(self):
        return self.rows * self.columns

    def make_tiles(self):
        num_tiles = self.num_tiles()
        offset = [0, 0]
        tiles = []
        for i in range(num_tiles):
            if i % 2 == 0:
                tiles.append(Box(
                    self.colors[0], 
                    pygame.Rect(*offset, self.size, self.size),
                ))
            else:
                tiles.append(Box(
                    self.colors[1], 
                    pygame.Rect(*offset, self.size, self.size),
                ))
            offset[0] += self.size
            offset[1] += self.size
        return tiles

while True:
    screen.fill((255, 255, 255))

    background = Background(
        rows=5, columns=10, size=30,
        colors=((255, 255, 255), (0, 0, 0)),
    )
    for tile in reversed(background.make_tiles()):
        tile.draw(screen)
Sign up to request clarification or add additional context in comments.

2 Comments

I think I should do some research on functions and methods before developing a project.Thank you for your interest
Sorry if I seemed a little blunt in my answer haha. Kinda seems like I just listed bad point about the code. Know that you're using classes correctly and that everything was neatly bundled together. Good luck with your game :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.