Reputation: 37
I have such a task:
The side of the window W and the number of cells N are entered from the keyboard so that W is a multiple of N. The window is painted in a checkerboard black and white square, the lower left square should be black. For example, if you enter "100 5", the following window should appear:
import pygame
pygame.init()
# size = width, height = (1000, 1000)
width = height = int(input('Let\'s draw a chessboard =) \nSpecify the window size from 100 to 1500 pixels (it will be in 1: 1 format): \n'))
size = (width, height)
screen = pygame.display.set_mode(size)
rows = cols = int(input('Specify the desired number of squares, I will raise them to the square and build a chessboard: \n'))
square_size = width / rows
game_name = pygame.display.set_caption("The chess board")
# rgb
white_color = (255, 230, 153)
black_color = (128, 64, 0)
def draw_squares(screen):
screen.fill(white_color)
for row in range(rows):
for col in range(row % 2, rows, 2):
pygame.draw.rect(screen, black_color, (col * square_size, row*square_size, square_size, square_size))
while pygame.event.wait().type != pygame.QUIT:
pygame.display.flip()
draw_squares(screen)
pygame.display.update()
pygame.quit()
I found this solution, but when I enter the height of 100 and the number of squares 5, I get this picture:
Upvotes: 2
Views: 569
Reputation: 54
There is nothing wrong with the function that draws the chess board. The extra 20px screen width you get is because in pygame the minimum width of a window is 120 pixels. This is to accommodate for the three buttons/the minimize, close, restore/ buttons on the top right part of the window.
You can try it yourself by running this code
size = (100, 100)
screen = pygame.display.set_mode(size)
print(screen.get_size())
And the output of the program will be (120, 100).
Upvotes: 2