Python Chess-Style Gameboard Generator

import random,math,sys
import pygame
from pygame.locals import *
import os

pygame.init() #Initializing Pygame
pygame.display.set_caption("Sandbox")

#Screen
screenwidth= 800
screenheight= 600
screen=pygame.display.set_mode((screenwidth,screenheight),0, 0)

def terminate():
    pygame.quit()
    sys.exit()

#Squares
purplesquare=pygame.image.load('purplesquare.png')
purplesquare=pygame.transform.scale(purplesquare,(50,50))
bluesquare=pygame.image.load('lightbluesquare.png')
bluesquare=pygame.transform.scale(bluesquare,(50,50))

#Function
def cookie(x,y):
    return random.randint(x,y)

#Variable
tri = 9

while True:
    while tri==9:
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()    
        x = 0
        y = 50
        w = 0
        while  x!=600:
               x=x+50
               w = w+1
               if w%2==0:
                    screen.blit(purplesquare,(x,y))
               else:
                    screen.blit(bluesquare,(x,y))
               pygame.display.update()
               if x>=600 and y!=450:
                    if y<450:
                        x = 0
                        y = y+50
                        w=w-1
                    if y>=450:
                       tri=10
                       break
                   
    while tri==10:
        for event in pygame.event.get():
           if event.type == QUIT:
               terminate()
        pygame.display.update()

Below I’ve provided the images used and a screenshot of the code being utilized. You may notice the function “cookie” isn’t doing anything. I left in it there because you might find it useful. By using w = cookie(1,3) you can create a loop that will produce an image depending on what random number is generated (between one in three). You simply set up an image blit that varies depending on what the value of your “w” is following the execution of the “cookie” function. Lastly, I am using the term “chess-style” because it’s not a chess-board (it does not have the correct amount of spaces). However, you could easily change things up to generate a chess-board out of this code (I can’t do all the work). You might also use screenwidth to generate a “stop on condition x” that will be consistent regardless of your game dimensions (though this consistency would entail adding fewer or more squares). Anyway, I thought this code might be somewhat useful to the theoretical Pygamer. Enjoy.

Code Image
light blue square
purple square

This entry was posted in Pygame. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

* Copy this password:

* Type or paste password here: