Python graphics - Why is blitting sometimes clipped

beavoid
beavoid used Ask the Experts™
on
Hi
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)
       
            screen.blit(white_king,(x*square_size,y*square_size))

Open in new window

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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)

pygame.init()
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))

pygame.display.flip()

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?
beavoidSelf Employed

Author

Commented:
thanks.
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

Thanks.
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
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

beavoidSelf Employed

Author

Commented:
Hi
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
beavoidSelf Employed

Author

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 http://www.color-hex.com/color-names.html
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 ;-)
beavoidSelf Employed

Author

Commented:
Thanks
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial