I want to make a chessboard in pygame with python. Just only the chessboard with for loops. I tried in several ways to do this but i didn't figured out what exactly it will be. Here is my code:
import pygame
pygame.init()
#set color with rgb
white,black,red = (255,255,255),(0,0,0),(255,0,0)
#set display
gameDisplay = pygame.display.set_mode((800,600))
#caption
pygame.display.set_caption("ChessBoard")
#beginning of logic
gameExit = False
lead_x = 20
lead_y = 20
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
#For loop for chessboard
#draw a rectangle
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [lead_x,lead_y,20,20])
pygame.display.update()
#quit from pygame & python
pygame.quit()
quit()
Now i need an expert suggestion what it will be with python code. I just wanna show a chessboard in my screen. Thats it.
More efficient would be to draw the board once at initialization and just blit that surface:
And then in your loop you draw the board surface first:
Possible solution, maybe not the most elegant, but you can create the squares in a loop
You can use
itertools.cycle
to cycle through the colors in a nested for loop and just passnext(colors)
topygame.draw.rect
. I'd create a background surface and draw the rects onto it when the program starts, and then just blit the background surf in the while loop, because that's more efficient than blitting the rects separately.