Python graphics - Why is blitting sometimes clipped

My Python chess GUI is coming on well.
The 8x8, black and white is perfectly structured, colored at a changeable square_size

The white squares are just the white window background.
However, my transparent pieces don't render on a black square (rectangle draw.rect)
They are pasted under it! I can move the piece location by a few pixels, and see it emerge slowly from under the black square.

Is this a known issue?

Here is my relevant code, there is no code between them

pygame.draw.rect(screen, square_color, (x*square_size,y*square_size,square_size,square_size), 0)

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Your  relevant code might not be enough to spot the issue:

On my PC the background is black and not white. If the background color is really white in your case, then it's probably better to draw the black and the white squares to be sure, that
everybody has the same image independent on his machine's default background color
So I have to draw the white squares.

If I execute following code
import pygame

size = 60 # 60 pixel per field
white =(255, 255, 255)

screen = pygame.display.set_mode((size*2, size*2), 0, 32)
figure =  pygame.image.load("Chess_klt60.png")

for x,y in ((0,0), (1,1)):
    pygame.draw.rect(screen, white, (x*size,y*size,size,size), 0)

screen.blit(figure, (size/2, size/2))


import time; time.sleep(10)

Open in new window

Then I get following image:
screen shot of program's output
This looks correct to me.

Can you try to run this test case and tell me what you see?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
beavoidAuthor Commented:
I see that
But, for some reason, the whole 2 rows of start off black and white pieces appear everywhere perfectly, except the 2 ranks of white pieces and pawns. - no white pawns
Black is fine, I'll look at it tomorrow, it's midnight

I hope you find the issue.
If the problem persists I suggest following:

Copy your code into a different directory and modify it.
Try to find the smallest piece of code, that still produces the problem

What you describe is almost as if the draw order is inversed and you blit first the pawns and then the squares.
Perhaps there is an issue in the control logic (if the else / for / while logic), which inverses under some circumstances the draw order
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

beavoidAuthor Commented:
Thanks, all
This is ( GUI window) what I have, down below.
My class for move generations returns a [] of legal moves, so it won't be long until I have it all done. I just wanted to get the correct GUI finished, because I was getting a little sick of debugging squinting (squint-bugging?) with this on my PyCharm console:
7 > ['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r']
6 > ['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p']
5 > [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
4 > [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
3 > [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
2 > [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
1 > ['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P']
0 > ['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R']

Open in new window

My Python chess board GUI
beavoidAuthor Commented:
I might have to ask someone to adjust some of the graphics for the black pieces, so that they are visible on black squares,
just take all the white pieces, and invert their colors
As you said it's just the design of the chess pieces, that's not well suited to be displayed on a blackbackground.

I personally (being a lazy person) would suggest another approach.
Change the chessboard colors in something like ivory (e.g. #FFFFF0, (0xff, 0xff, 0xf0)  ) instead of white and brown for the black fields.

Alternatively take gimp / photoshop and edit the chess pieces.

In order to choose colors a color you can look for example at
or just play around a little.

If you find for example a color with the code #8b6508  you can use (0x8b, 0x65, 0x08) in pygame.

If you use other colors than black and white it might even be, that your chess board looks a little 'warmer' and friendler. SO perhaps just changing the board color is not only lazyness ;-)
beavoidAuthor Commented:
You're welcome.

Just out of curiosity:
Keep me updated about which  approach you finally take.
Was just thinking a little.

even the white figures (e.g. the white king) don't always look that great on black back ground.

If you really want to go for a black and white chess board and you want things to look good, then you would probably best to have:
- each black figure 'optmized' for black background
- each black figure 'optmized' for white background
- each white figure 'optmized' for black background
- each white figure 'optmized' for white background

Your program would then render one or the other image depending on the chess field's color.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Game Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.