Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HOW FIX MY BOARD GAME
#1
Photo 
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
Yoriz write Mar-31-2022, 08:35 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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.
Reply
#3
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/
Reply
#4
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()

Attached Files

Thumbnail(s)
   
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to make a board with turtle, nothing happens when running script Quascia 3 685 Nov-01-2023, 03:11 PM
Last Post: deanhystad
  The game should now run and terminate once the board is full, but still can’t identif rango 0 1,464 Jul-22-2021, 03:24 AM
Last Post: rango
  Enabling interrupt on Adafruits button/led board Moris526 0 2,026 Apr-30-2021, 03:29 PM
Last Post: Moris526
  High-Precision Board Voltage Reading from Python into PD Liam484 1 2,094 Mar-29-2021, 02:57 PM
Last Post: Marbelous
  Interrupt for Adafruits Neotrellis button/led board Moris526 0 1,812 Dec-28-2020, 05:42 AM
Last Post: Moris526
  [Help] A function that generates an n x n sized board? vanicci 5 4,775 Aug-14-2018, 02:26 PM
Last Post: vanicci
  Serial communication with a board Bokka 2 5,367 Dec-07-2017, 07:34 AM
Last Post: Bokka
  snakes and ladders board creation help teddyben123 1 5,356 Aug-25-2017, 01:31 PM
Last Post: ichabod801
  Communicating C++ and Python in Jetson TK1 board. hlfan1234567 1 3,190 Jul-09-2017, 04:22 PM
Last Post: hlfan1234567

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020