Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, sys
- from pygame.locals import *
- #Setting constant variables
- lineThickness = 2
- #w = width
- w = 70
- #h = height
- h = w
- windowHeight = 700
- windowWidth = 700
- #Setting the margin on the Y axis (up-down)
- yMargin = (windowHeight - 8 * h) / 2
- #Setting the margin on the X axis (left-right)
- xMargin = (windowWidth - 8 * w) / 2
- #Setting colours for later use
- black = (0,0,0)
- white = (255,255,255)
- red = (255,0,0)
- brown = (102,41,0)
- #Setting the grid matrix
- grid = [[1]*8 for n in range(8)]
- #A matrix counts squares
- #like numbers (0,1,2,3,...,64)
- #but we want them to be counted
- #like chess squares (a1,a2,...,e4,e5,e6)
- #So we set equivalences between
- #letters (alpha) and numbers (index)
- #such as a = 1, b = 2,...,h = 7
- chessMapAlphaIndex = {
- "a" : 0,
- "b" : 1,
- "c" : 2,
- "d" : 3,
- "e" : 4,
- "f" : 5,
- "g" : 6,
- "h" : 7
- }
- #And vice versa
- chessMapIndexAlpha = {
- 0 : "a",
- 1 : "b",
- 2 : "c",
- 3 : "d",
- 4 : "e",
- 5 : "f",
- 6 : "g",
- 7 : "h"
- }
- global displaySurf
- displaySurf = pygame.display.set_mode((windowWidth, windowHeight))
- pygame.draw.rect(displaySurf, white, (((0,0)),((windowWidth, windowHeight))))
- #Setting the colour per square
- chessSquareBlack = {
- "a1", "c1", "e1", "g1",
- "b2", "d2", "f2", "h2",
- "a3", "c3", "e3", "g3",
- "b4", "d4", "f4", "h4",
- "a5", "c5", "e5", "g5"
- "b6", "d6", "f6", "h6",
- "a7", "c7", "e7", "g7"
- "b8", "d8", "f8", "h8",
- }
- chessSquareWhite = {
- "a1", "c1", "e1", "g1",
- "b2", "d2", "f2", "h2",
- "a3", "c3", "e3", "g3",
- "b4", "d4", "f4", "h4",
- "a5", "c5", "e5", "g5"
- "b6", "d6", "f6", "h6",
- "a7", "c7", "e7", "g7"
- "b8", "d8", "f8", "h8",
- }
- def draw():
- #Setting x-y coordinates
- x,y = 0,0
- #Drawing background
- pygame.draw.rect(displaySurf, brown, (((0,0)), ((windowWidth, windowHeight))))
- #Checking that the row we're working
- #on is between 1 and 8
- for row in grid:
- #Checking the row we're working
- #on is between 1 and 8
- for col in row:
- #Square background colour
- pygame.draw.rect(displaySurf, white, (((x + xMargin, y + yMargin)), ((w, h))))
- #Square outline
- pygame.draw.rect(displaySurf, black, (((x + xMargin, y + yMargin)), ((w, h))), lineThickness)
- x = x + w
- y = y + h
- x = 0
- #Display text
- font = pygame.font.SysFont("Tahoma", 16, True)
- textA = font.render("a", True, black)
- displaySurf.blit(textA, ((2 * w - w / 2),(2 * h / 3)))
- textB = font.render("b", True, black)
- displaySurf.blit(textB, ((3 * w - w / 2),(2 * h / 3)))
- textC = font.render("c", True, black)
- displaySurf.blit(textC, ((4 * w - w / 2),(2 * h / 3)))
- textD = font.render("d", True, black)
- displaySurf.blit(textD, ((5 * w - w / 2),(2 * h / 3)))
- textE = font.render("e", True, black)
- displaySurf.blit(textE, ((6 * w - w / 2),(2 * h / 3)))
- textF = font.render("f", True, black)
- displaySurf.blit(textF, ((7 * w - w / 2),(2 * h / 3)))
- textG = font.render("g", True, black)
- displaySurf.blit(textG, ((8 * w - w / 2),(2 * h / 3)))
- textH = font.render("h", True, black)
- displaySurf.blit(textH, ((9 * w - w / 2),(2 * h / 3)))
- text1 = font.render("1", True, black)
- displaySurf.blit(text1, ((2 * w / 3),(2 * h - h / 2)))
- text2 = font.render("2", True, black)
- displaySurf.blit(text2, ((2 * w / 3),(3 * h - h / 2)))
- text3 = font.render("3", True, black)
- displaySurf.blit(text3, ((2 * w / 3),(4 * h - h / 2)))
- text4 = font.render("4", True, black)
- displaySurf.blit(text4, ((2 * w / 3),(5 * h - h / 2)))
- text5 = font.render("5", True, black)
- displaySurf.blit(text5, ((2 * w / 3),(6 * h - h / 2)))
- text6 = font.render("6", True, black)
- displaySurf.blit(text6, ((2 * w / 3),(7 * h - h / 2)))
- text7 = font.render("7", True, black)
- displaySurf.blit(text7, ((2 * w / 3),(8 * h - h / 2)))
- text8 = font.render("8", True, black)
- displaySurf.blit(text8, ((2 * w / 3),(9 * h - h / 2)))
- def main():
- pygame.init
- pygame.display.set_caption("MatrixTest")
- draw()
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- pygame.display.update
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment