gamers beware
gamers beware

Reputation: 53

Why doesn't the rect parameter work in pygame fill?

In the module pygame the Surface.fill has a parameter called rect=[].

in my code, I'm loading in an 8by8 pixel of water with an adjustable level. 1 to 8 pixels tall.

for x in range(len(screen)):
    for y in range(len(screen[x])):
        if screen[x][y] == 0:
            box.fill((0,0,0))
        elif screen[x][y] == 9:
            box.fill((255,255,255))
        else:
            box.fill((0,0,255), rect = [x*8,y*8+8-screen[x][y],8,screen[x][y]])
        window.blit(box, (x*8,y*8))

box is pygame.Surface((8,8))

The screen list is the entire screen

I'm using the screen list to map out the entire screen using values from 0to9, where 9 is a solid platform, 8to1 is water level, and 0 is nothing

This code loads in each screen value as a type of 8by8 pixel(0to9). (since I'm using 8by8 pixels the list is 100 long and my screen is 800 pixels long hence the *8).

In the box.fill part I'm filling in box with blue and I'm using the rect parameter to draw a rect the size of (8 long and water level or screen value tall), and I'm placing that at x*8(formula explained earlier) and y*8 and adding 8-screen[x][y] which is moving the rect down so that the water item is the proper height.

My issue is, is that the water pixels aren't loading in.

Upvotes: 1

Views: 109

Answers (1)

The Big Kahuna
The Big Kahuna

Reputation: 2110

The rect argument is to fill in an area on the surface, the surface your trying to fill in 8x8 big, at the first pixel chunk, it works correctly because 0*8 is 0, but when you get to the second pixel chunk, its trying to fill at 8-16 on the x axis, which is not on the 8x8 box, you want to to start at the 0 (on the x axis).

box.fill((0,0,255), rect=(0,8-screen[x][y],8,screen[x][y]))

Upvotes: 1

Related Questions