Guest User

Untitled

a guest
Nov 1st, 2019
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. import pygame, sys
  2. from pygame.locals import *
  3. #Setting constant variables
  4.  
  5. lineThickness = 2
  6.  
  7. #w = width
  8. w = 70
  9.  
  10. #h = height
  11. h = w
  12.  
  13. windowHeight = 700
  14. windowWidth = 700
  15.  
  16. #Setting the margin on the Y axis (up-down)
  17. yMargin = (windowHeight - 8 * h) / 2
  18.  
  19. #Setting the margin on the X axis (left-right)
  20. xMargin = (windowWidth - 8 * w) / 2
  21.  
  22. #Setting colours for later use
  23. black = (0,0,0)
  24. white = (255,255,255)
  25. red = (255,0,0)
  26. brown = (102,41,0)
  27.  
  28. #Setting the grid matrix
  29.  
  30. grid = [[1]*8 for n in range(8)]
  31.  
  32. #A matrix counts squares
  33. #like numbers (0,1,2,3,...,64)
  34. #but we want them to be counted
  35. #like chess squares (a1,a2,...,e4,e5,e6)
  36.  
  37. #So we set equivalences between
  38. #letters (alpha) and numbers (index)
  39. #such as a = 1, b = 2,...,h = 7
  40.  
  41. chessMapAlphaIndex = {
  42. "a" : 0,
  43. "b" : 1,
  44. "c" : 2,
  45. "d" : 3,
  46. "e" : 4,
  47. "f" : 5,
  48. "g" : 6,
  49. "h" : 7
  50. }
  51.  
  52.  
  53. #And vice versa
  54. chessMapIndexAlpha = {
  55. 0 : "a",
  56. 1 : "b",
  57. 2 : "c",
  58. 3 : "d",
  59. 4 : "e",
  60. 5 : "f",
  61. 6 : "g",
  62. 7 : "h"
  63. }
  64.  
  65.  
  66. global displaySurf
  67. displaySurf = pygame.display.set_mode((windowWidth, windowHeight))
  68. pygame.draw.rect(displaySurf, white, (((0,0)),((windowWidth, windowHeight))))
  69.  
  70.  
  71. #Setting the colour per square
  72.  
  73. chessSquareBlack = {
  74. "a1", "c1", "e1", "g1",
  75. "b2", "d2", "f2", "h2",
  76. "a3", "c3", "e3", "g3",
  77. "b4", "d4", "f4", "h4",
  78. "a5", "c5", "e5", "g5"
  79. "b6", "d6", "f6", "h6",
  80. "a7", "c7", "e7", "g7"
  81. "b8", "d8", "f8", "h8",
  82. }
  83.  
  84. chessSquareWhite = {
  85. "a1", "c1", "e1", "g1",
  86. "b2", "d2", "f2", "h2",
  87. "a3", "c3", "e3", "g3",
  88. "b4", "d4", "f4", "h4",
  89. "a5", "c5", "e5", "g5"
  90. "b6", "d6", "f6", "h6",
  91. "a7", "c7", "e7", "g7"
  92. "b8", "d8", "f8", "h8",
  93. }
  94.  
  95.  
  96. def draw():
  97.  
  98.  
  99. #Setting x-y coordinates
  100. x,y = 0,0
  101.  
  102. #Drawing background
  103. pygame.draw.rect(displaySurf, brown, (((0,0)), ((windowWidth, windowHeight))))
  104.  
  105.  
  106. #Checking that the row we're working
  107. #on is between 1 and 8
  108. for row in grid:
  109.  
  110. #Checking the row we're working
  111. #on is between 1 and 8
  112. for col in row:
  113.  
  114. #Square background colour
  115. pygame.draw.rect(displaySurf, white, (((x + xMargin, y + yMargin)), ((w, h))))
  116. #Square outline
  117. pygame.draw.rect(displaySurf, black, (((x + xMargin, y + yMargin)), ((w, h))), lineThickness)
  118. x = x + w
  119. y = y + h
  120. x = 0
  121.  
  122. #Display text
  123.  
  124. font = pygame.font.SysFont("Tahoma", 16, True)
  125.  
  126. textA = font.render("a", True, black)
  127. displaySurf.blit(textA, ((2 * w - w / 2),(2 * h / 3)))
  128.  
  129. textB = font.render("b", True, black)
  130. displaySurf.blit(textB, ((3 * w - w / 2),(2 * h / 3)))
  131.  
  132. textC = font.render("c", True, black)
  133. displaySurf.blit(textC, ((4 * w - w / 2),(2 * h / 3)))
  134.  
  135. textD = font.render("d", True, black)
  136. displaySurf.blit(textD, ((5 * w - w / 2),(2 * h / 3)))
  137.  
  138. textE = font.render("e", True, black)
  139. displaySurf.blit(textE, ((6 * w - w / 2),(2 * h / 3)))
  140.  
  141. textF = font.render("f", True, black)
  142. displaySurf.blit(textF, ((7 * w - w / 2),(2 * h / 3)))
  143.  
  144. textG = font.render("g", True, black)
  145. displaySurf.blit(textG, ((8 * w - w / 2),(2 * h / 3)))
  146.  
  147. textH = font.render("h", True, black)
  148. displaySurf.blit(textH, ((9 * w - w / 2),(2 * h / 3)))
  149.  
  150. text1 = font.render("1", True, black)
  151. displaySurf.blit(text1, ((2 * w / 3),(2 * h - h / 2)))
  152.  
  153. text2 = font.render("2", True, black)
  154. displaySurf.blit(text2, ((2 * w / 3),(3 * h - h / 2)))
  155.  
  156. text3 = font.render("3", True, black)
  157. displaySurf.blit(text3, ((2 * w / 3),(4 * h - h / 2)))
  158.  
  159. text4 = font.render("4", True, black)
  160. displaySurf.blit(text4, ((2 * w / 3),(5 * h - h / 2)))
  161.  
  162. text5 = font.render("5", True, black)
  163. displaySurf.blit(text5, ((2 * w / 3),(6 * h - h / 2)))
  164.  
  165. text6 = font.render("6", True, black)
  166. displaySurf.blit(text6, ((2 * w / 3),(7 * h - h / 2)))
  167.  
  168. text7 = font.render("7", True, black)
  169. displaySurf.blit(text7, ((2 * w / 3),(8 * h - h / 2)))
  170.  
  171. text8 = font.render("8", True, black)
  172. displaySurf.blit(text8, ((2 * w / 3),(9 * h - h / 2)))
  173.  
  174.  
  175. def main():
  176.  
  177. pygame.init
  178. pygame.display.set_caption("MatrixTest")
  179.  
  180. draw()
  181.  
  182. while True:
  183. for event in pygame.event.get():
  184. if event.type == QUIT:
  185. pygame.quit()
  186. sys.exit()
  187. pygame.display.update
  188.  
  189. if __name__ == "__main__":
  190. main()
Add Comment
Please, Sign In to add comment