Python Forum

Full Version: HOW FIX MY BOARD GAME
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I try to make my board game like as photo.
my code :
import pygame
from sys import exit

rows = cols = 8
fps = 60
white = (255, 255, 255)
black = (0, 0, 0)

pygame.init()

screen = pygame.Rect(0, 0 , 600, 600)
surface = pygame.display.set_mode(screen.size)
pygame.display.set_caption('draughts')

class Board():
    def __init__(self):
        self.board = [] # 2d matrix
        self.red = self.black = 12
        self.red_king = self.black_king = 0

    def draw_squares(self, surface):
        surface.fill(black)
        board = pygame.Surface(512, 512)
        for i in (0 , 8):
            for j in ((i+1)//2, cols, 2):
                pygame.draw.rect(surface, white, (i*64), (j*64), 64, 64)
        surface.blit(board, (44, 44))

def main():
   run = True
   clock = pygame.time.Clock()
   board = Board()

   while run:
       for event in pygame.event.get():
           if event.type == pygame.QUIT:
               run = False


      board.draw_squares(surface)

      pygame.display.update()

   pygame.quit()

main()
Error:
File "C:/Users/Lampros/PycharmProjects/pythonProject15/main.py", line 40 board.draw_squares(surface) ^ IndentationError: unindent does not match any outer indentation level

[Image: board-with-checkers-vector-3523363]
https://www.vectorstock.com/royalty-free...or-3523363
And....? Does your code not work? Don't you know how to proceed?

You should look here for each of the functions you are trying to use to learn what arguments are expected.

https://www.pygame.org/docs/?msclkid=ff5...80304b88e8

For example, pygame.Surface() says this:
https://www.pygame.org/docs/ref/surface.html Wrote:pygame.Surface
pygame object for representing images
Surface((width, height), flags=0, depth=0, masks=None) -> Surface
Surface((width, height), flags=0, Surface) -> Surface
You call pygame.Surface(512, 512) which is not correct. Do you see why it is not correct?
You also have some indenting errors. Whatever you are using to write code, set the tab = 4 spaces and use the tab key to indent. Your indenting is sometimes 8, sometimes 7.
pygame.draw.rect is also wrong

pygame.draw.rect(surface, color, pygame.Rect(x, x, x, x))

https://www.geeksforgeeks.org/how-to-dra...in-pygame/
Look over the changes that I've made and see what the difference is.
import pygame
from sys import exit
 
rows = cols = 8
fps = 60
white = (255, 255, 255)
black = (0, 0, 0)
 
pygame.init()
 
screen = pygame.Rect(0, 0 , 600, 600)
surface = pygame.display.set_mode(screen.size)
pygame.display.set_caption('draughts')
 
class Board():
	def __init__(self):
		self.board = [] # 2d matrix
		self.red = self.black = 12
		self.red_king = self.black_king = 0
		self.board_image = pygame.Surface ((512, 512))
 
	def draw_squares(self, surface):
		self.board_image.fill(black)
		column_start = 0
		for i in range (1, rows) :
			for j in range (column_start, cols - column_start, 2) :
				pygame.draw.rect(self.board_image, white, (j*64, i*64, 64, 64))
			column_start = 1 - column_start
		surface.blit(self.board_image, (75, 10))
 
def main():
	run = True
	board = Board()

	while run:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				run = False
 
		board.draw_squares(surface)
		pygame.display.update()
		clock = pygame.time.Clock()
 
main()
pygame.quit()