简体   繁体   中英

Make a 8*8 chessboard in pygame with python

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:

cellSize = 20
board = Surface((cellSize * 8, cellSize * 8))
board.fill((255, 255, 255))
for x in range(0, 8, 2):
    for y in range(0, 8, 2):
        pygame.draw.rect(board, (0,0,0), (x*size, y*size, size, size))

And then in your loop you draw the board surface first:

gameDisplay.blit(board, board.get_rect())
# Draw your game pieces

Possible solution, maybe not the most elegant, but you can create the squares in a loop

#Size of squares
size = 20

#board length, must be even
boardLength = 8
gameDisplay.fill(white)

cnt = 0
for i in range(1,boardLength+1):
    for z in range(1,boardLength+1):
        #check if current loop value is even
        if cnt % 2 == 0:
            pygame.draw.rect(gameDisplay, white,[size*z,size*i,size,size])
        else:
            pygame.draw.rect(gameDisplay, black, [size*z,size*i,size,size])
        cnt +=1
    #since theres an even number of squares go back one value
    cnt-=1
#Add a nice boarder
pygame.draw.rect(gameDisplay,black,[size,size,boardLength*size,boardLength*size],1)

pygame.display.update()

You can use itertools.cycle to cycle through the colors in a nested for loop and just pass next(colors) to pygame.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.

import itertools
import pygame as pg


pg.init()

BLACK = pg.Color('black')
WHITE = pg.Color('white')

screen = pg.display.set_mode((800, 600))
clock = pg.time.Clock()

colors = itertools.cycle((WHITE, BLACK))
tile_size = 20
width, height = 8*tile_size, 8*tile_size
background = pg.Surface((width, height))

for y in range(0, height, tile_size):
    for x in range(0, width, tile_size):
        rect = (x, y, tile_size, tile_size)
        pg.draw.rect(background, next(colors), rect)
    next(colors)

game_exit = False
while not game_exit:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            game_exit = True

    screen.fill((60, 70, 90))
    screen.blit(background, (100, 100))

    pg.display.flip()
    clock.tick(30)

pg.quit()

Here's a little snippet I used in my code

import pygame; from pygame import *
import numpy as np
import itertools


class Chessboard:
    ...
    def create(self):
        for row in range(8):
            for col in range(8):
                self.checker = draw.rect(
                    board_surf, next(self.checker_color),
                    (checker_size * row, checker_size * col,
                    checker_size, checker_size))
                self.append(self.checker)
            next(self.checker_color)
        return np.array(self.checker_coords).reshape(8,8,4)

chessboard = Chessboard()
checkers = chessboard.create()

Now you have a list that you can reference in the future! https://imgur.com/a/N7MXAC5

[[[  0   0  64  64]
  [  0  64  64  64]
  [  0 128  64  64]
  [  0 192  64  64]
  [  0 256  64  64]
  [  0 320  64  64]
  [  0 384  64  64]
  [  0 448  64  64]]

 [[ 64   0  64  64]
  [ 64  64  64  64]
  [ 64 128  64  64]
  [ 64 192  64  64]
  [ 64 256  64  64]
  [ 64 320  64  64]
  [ 64 384  64  64]
  [ 64 448  64  64]]

 [[128   0  64  64]
  [128  64  64  64]
  [128 128  64  64]
  [128 192  64  64]
  [128 256  64  64]
  [128 320  64  64]
  [128 384  64  64]
  [128 448  64  64]]

 [[192   0  64  64]
  [192  64  64  64]
  [192 128  64  64]
  [192 192  64  64]
  [192 256  64  64]
  [192 320  64  64]
  [192 384  64  64]
  [192 448  64  64]]

 [[256   0  64  64]
  [256  64  64  64]
  [256 128  64  64]
  [256 192  64  64]
  [256 256  64  64]
  [256 320  64  64]
  [256 384  64  64]
  [256 448  64  64]]

 [[320   0  64  64]
  [320  64  64  64]
  [320 128  64  64]
  [320 192  64  64]
  [320 256  64  64]
  [320 320  64  64]
  [320 384  64  64]
  [320 448  64  64]]

 [[384   0  64  64]
  [384  64  64  64]
  [384 128  64  64]
  [384 192  64  64]
  [384 256  64  64]
  [384 320  64  64]
  [384 384  64  64]
  [384 448  64  64]]

 [[448   0  64  64]
  [448  64  64  64]
  [448 128  64  64]
  [448 192  64  64]
  [448 256  64  64]
  [448 320  64  64]
  [448 384  64  64]
  [448 448  64  64]]]

just use this:

def pxl(q):
    return q * tile_size
for y in range(8):
            for x in range(8):
                pygame.draw.rect(screen, color, [pxl(x), pxl(y), pxl(1), pxl(1)])(x, y, black if (x + y) % 2 == 0 else white)
  for x in range(width):
            for y in range(height):
                if y % 2 == 0:
                    if not x % 2 == 0:
                        pygame.draw.rect(w, (255, 255, 255), (x * scale, y * scale, scale, scale))
                else:
                    if x % 2 == 0:
                            pygame.draw.rect(w, (255, 255, 255), (x * scale, y * scale, scale, scale))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM