HerupuMe

Untitled

Sep 3rd, 2024
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.46 KB | None | 0 0
  1. import pygame
  2.  
  3. pygame.init()
  4.  
  5. height = 950
  6. width = 1000
  7. screen = pygame.display.set_mode([width, height])
  8. pygame.display.set_caption('Checkers match')
  9. font = pygame.font.SysFont('Arial', 20)
  10. big_font = pygame.font.SysFont('calibri', 45)
  11. timer = pygame.time.Clock()
  12. fps = 60
  13.  
  14. # game variables and images
  15.  
  16. white_pieces = ['man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man']
  17. white_locations = [(0, 7), (2, 7), (4, 7), (6, 7), (1, 6), (3, 6), (5, 6), (7, 6), (0, 5), (2, 5), (4, 5), (6, 5)]
  18. black_pieces = ['man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man']
  19. black_locations = [(1, 0), (3, 0), (5, 0), (7, 0), (0, 1), (2, 1), (4, 1), (6, 1), (1, 2), (3, 2), (5, 2), (7, 2)]
  20.  
  21. captured_pieces_white = []
  22. captured_pieces_black = []
  23.  
  24. turn_step = 0
  25. selection = 100
  26. valid_moves = []
  27.  
  28. # piece images
  29. white_man = pygame.image.load('Checkers Piece White(man).png')
  30. white_man = pygame.transform.scale(white_man, (150, 150))
  31. white_man_small = pygame.transform.scale(white_man, (45, 45))
  32.  
  33. white_king = pygame.image.load('Checkers Piece White(king).png')
  34. white_king = pygame.transform.scale(white_king, (150, 150))
  35. white_king_small = pygame.transform.scale(white_king, (45, 45))
  36. white_images = [white_man, white_king]
  37. small_white_images = [white_man_small, white_king_small]
  38.  
  39. black_man = pygame.image.load('Checkers Piece Black(man).png')
  40. black_man = pygame.transform.scale(black_man, (150, 150))
  41. black_man_small = pygame.transform.scale(black_man, (45, 45))
  42. black_king = pygame.image.load('Checkers Piece Black(king).png')
  43. black_king = pygame.transform.scale(black_king, (150, 150))
  44. black_king_small = pygame.transform.scale(black_king, (45, 45))
  45. black_images = [black_man, black_king]
  46. small_black_images = [black_man_small, black_king_small]
  47.  
  48. piece_list = ['man', 'king']
  49.  
  50. # check variables
  51. winner = ''
  52. game_over = False
  53.  
  54.  
  55. # draw main game board
  56. def draw_board():
  57. for i in range(32):
  58. column = i % 4
  59. row = i // 4
  60. if row % 2 == 0:
  61. pygame.draw.rect(screen, 'light gray', [600 - (column * 200), row * 100, 100, 100])
  62. else:
  63. pygame.draw.rect(screen, 'light gray', [700 - (column * 200), row * 100, 100, 100])
  64. pygame.draw.rect(screen, 'gray', [0, 800, width, 200])
  65. pygame.draw.rect(screen, 'red', [0, 800, width, 150], 5)
  66. pygame.draw.rect(screen, 'red', [800, 0, 200, height], 5)
  67. status_text = ['WHITE: Select a piece to move', 'WHITE: Select a place to move to',
  68. 'BLACK: Select a piece to move', 'BLACK: Select a place to move to']
  69. screen.blit(big_font.render(status_text[turn_step], True, 'black'), (20, 820))
  70. for j in range(9):
  71. pygame.draw.line(screen, 'black', (0, 100 * j), (800, 100 * j), 2)
  72. pygame.draw.line(screen, 'black', (100 * j, 0), (100 * j, 800), 2)
  73. screen.blit(big_font.render('FORFEIT', True, 'black'), (825, 850))
  74.  
  75.  
  76. # draw pieces
  77. def draw_pieces():
  78. square_size = 100
  79. piece_size = 150
  80. offset = (square_size - piece_size) // 2
  81. for i in range(len(black_pieces)):
  82. index = piece_list.index(white_pieces[i])
  83. x_pos = white_locations[i][0] * square_size + offset
  84. y_pos = white_locations[i][1] * square_size + offset
  85. if white_pieces[i] == 'man':
  86. screen.blit(white_man, (x_pos, y_pos))
  87. else:
  88. screen.blit(white_images[index], (x_pos, y_pos))
  89. if turn_step < 2:
  90. if selection == i:
  91. pygame.draw.rect(screen, 'gold',
  92. [white_locations[i][0] * 100 + 1, white_locations[i][1] * 100 + 1, 100, 100], 2)
  93.  
  94. if len(black_pieces) != len(black_locations):
  95. print(
  96. f"Error: Mismatch in lengths. black_pieces: {len(black_pieces)}, black_locations: {len(black_locations)}")
  97. return
  98. for i in range(len(black_pieces)):
  99. if i >= len(black_locations): # Safety check
  100. print(f"Error: Index {i} is out of range for black_locations")
  101. break
  102. index = piece_list.index(black_pieces[i])
  103. x_pos = black_locations[i][0] * square_size + offset
  104. y_pos = black_locations[i][1] * square_size + offset
  105. if black_pieces[i] == 'man':
  106. screen.blit(black_man, (x_pos, y_pos))
  107. else:
  108. screen.blit(black_images[index], (x_pos, y_pos))
  109. if turn_step >= 2:
  110. if selection == i:
  111. pygame.draw.rect(screen, 'silver',
  112. [black_locations[i][0] * 100 + 1, black_locations[i][1] * 100 + 1, 100, 100], 2)
  113.  
  114.  
  115. # check all valid piece options on board
  116. def check_options(pieces, locations, turn):
  117. if len(pieces) != len(locations):
  118. print(f"Error: Mismatch in lengths. Pieces: {len(pieces)}, Locations: {len(locations)}")
  119. return []
  120.  
  121. moves_list = []
  122. all_moves_list = []
  123. for i in range(len(pieces)):
  124. location = locations[i]
  125. piece = pieces[i]
  126. if piece == 'man':
  127. moves_list = check_man(location, turn)
  128. elif piece == 'king':
  129. moves_list = check_king(location, turn)
  130. all_moves_list.append(moves_list)
  131. return all_moves_list
  132.  
  133.  
  134. # check valid man moves
  135. def check_man(position, colour):
  136. moves_list = []
  137. if colour == 'white':
  138. if (position[0] - 1 >= 0 and position[1] - 1 >= 0 and (
  139. position[0] - 1, position[1] - 1) not in white_locations and (
  140. position[0] - 1, position[1] - 1) not in black_locations):
  141. moves_list.append((position[0] - 1, position[1] - 1))
  142. if (position[0] + 1 < 8 and position[1] - 1 >= 0 and (
  143. position[0] + 1, position[1] - 1) not in white_locations and (
  144. position[0] + 1, position[1] - 1) not in black_locations):
  145. moves_list.append((position[0] + 1, position[1] - 1))
  146.  
  147. # Capture pieces
  148. if (position[0] - 2 >= 0 and position[1] - 2 >= 0 and (
  149. position[0] - 1, position[1] - 1) in black_locations and (
  150. position[0] - 2, position[1] - 2) not in white_locations and (
  151. position[0] - 2, position[1] - 2) not in black_locations):
  152. moves_list.append((position[0] - 2, position[1] - 2))
  153. black_locations.remove((position[0] - 1, position[1] - 1))
  154. if (position[0] + 2 < 8 and position[1] - 2 >= 0 and (position[0] + 1, position[1] - 1) in black_locations and (
  155. position[0] + 2, position[1] - 2) not in white_locations and (
  156. position[0] + 2, position[1] - 2) not in black_locations):
  157. moves_list.append((position[0] + 2, position[1] - 2))
  158. black_locations.remove((position[0] + 1, position[1] - 1))
  159.  
  160. else:
  161. if (position[0] - 1 >= 0 and position[1] + 1 < 8 and (
  162. position[0] - 1, position[1] + 1) not in white_locations and (
  163. position[0] - 1, position[1] + 1) not in black_locations):
  164. moves_list.append((position[0] - 1, position[1] + 1))
  165. if (position[0] + 1 < 8 and position[1] + 1 < 8 and (
  166. position[0] + 1, position[1] + 1) not in white_locations and (
  167. position[0] + 1, position[1] + 1) not in black_locations):
  168. moves_list.append((position[0] + 1, position[1] + 1))
  169.  
  170. # capture pieces
  171. if (position[0] - 2 >= 0 and position[1] + 2 < 8 and (position[0] - 1, position[1] + 1) in white_locations and (
  172. position[0] - 2, position[1] + 2) not in white_locations and (
  173. position[0] - 2, position[1] + 2) not in black_locations):
  174. moves_list.append((position[0] - 2, position[1] + 2))
  175. white_locations.remove((position[0] - 1, position[1] + 1))
  176. if (position[0] + 2 < 8 and position[1] + 2 < 8 and (position[0] + 1, position[1] + 1) in white_locations and (
  177. position[0] + 2, position[1] + 2) not in white_locations and (
  178. position[0] + 2, position[1] + 2) not in black_locations):
  179. moves_list.append((position[0] + 2, position[1] + 2))
  180. white_locations.remove((position[0] + 1, position[1] + 1))
  181.  
  182. return moves_list
  183.  
  184.  
  185. # check valid king moves
  186. def check_king(position, colour):
  187. moves_list = []
  188. if colour == 'white':
  189. ally_list = white_locations
  190. else:
  191. ally_list = black_locations
  192. directions = [(-1, -1), (-1, 1), (1, -1), (1, 1)]
  193. for i in range(4):
  194. target = (position[0] + directions[i][0], position[1] + directions[i][1])
  195. if target not in ally_list and 0 <= target[0] <= 7 and 0 <= target[1] <= 7:
  196. moves_list.append(target)
  197. return moves_list
  198.  
  199.  
  200. # check valid moves only for selected piece
  201. def check_valid_moves():
  202. if turn_step < 2:
  203. options_list = white_options
  204. else:
  205. options_list = black_options
  206. valid_options = options_list[selection]
  207. return valid_options
  208.  
  209.  
  210. # draw valid moves on screen
  211. def draw_valid(moves):
  212. if turn_step < 2:
  213. colour = 'gold'
  214. else:
  215. colour = 'blue'
  216. for i in range(len(moves)):
  217. pygame.draw.circle(screen, colour, (moves[i][0] * 100 + 50, moves[i][1] * 100 + 50), 5)
  218.  
  219.  
  220. # draw captured pieces on side of screen
  221. def draw_captured():
  222. for i in range(len(captured_pieces_white)):
  223. captured_piece = captured_pieces_white[i]
  224. index = piece_list.index(captured_piece)
  225. screen.blit(small_black_images[index], (825, 5 + 50 + i))
  226. for i in range(len(captured_pieces_black)):
  227. captured_piece = captured_pieces_black[i]
  228. index = piece_list.index(captured_piece)
  229. screen.blit(small_white_images[index], (925, 5 + 50 + i))
  230.  
  231.  
  232. def draw_game_over():
  233. pygame.draw.rect(screen, 'black', [200, 200, 400, 70])
  234. screen.blit(font.render(f'{winner} is victorious', True, 'yellow'), (210, 210))
  235. screen.blit(font.render(f'Press ENTER to start new game', True, 'green'), (210, 240))
  236.  
  237.  
  238. # main game loop
  239.  
  240. black_options = check_options(black_pieces, black_locations, 'black')
  241. white_options = check_options(white_pieces, white_locations, 'white')
  242.  
  243. run = True
  244. while run:
  245. timer.tick(fps)
  246. screen.fill('dimgray')
  247. draw_board()
  248. draw_pieces()
  249. draw_captured()
  250. if selection != 100:
  251. valid_moves = check_valid_moves()
  252. draw_valid(valid_moves)
  253.  
  254. # event handling
  255. for event in pygame.event.get():
  256. if event.type == pygame.QUIT:
  257. run = False
  258. if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and not game_over:
  259. x_coord = event.pos[0] // 100
  260. y_coord = event.pos[1] // 100
  261. click_coords = (x_coord, y_coord)
  262. if turn_step <= 1:
  263. if click_coords == (8, 8) or click_coords == (9, 8):
  264. winner = 'Black'
  265. game_over = True
  266. if click_coords in white_locations:
  267. selection = white_locations.index(click_coords)
  268. if turn_step == 0:
  269. turn_step = 1
  270. if click_coords in valid_moves and selection != 100:
  271. white_locations[selection] = click_coords
  272. if click_coords in black_locations:
  273. black_piece = black_locations.index(click_coords)
  274. captured_pieces_white.append(black_pieces[black_piece])
  275. if black_pieces[black_piece] == 0:
  276. winner = 'White'
  277. game_over = True
  278. black_pieces.pop(black_piece)
  279. black_locations.pop(black_piece)
  280. black_options = check_options(black_pieces, black_locations, 'black')
  281. white_options = check_options(white_pieces, white_locations, 'white')
  282. turn_step = 2
  283. selection = 100
  284. valid_moves = []
  285. if turn_step > 1:
  286. if len(black_pieces) == 0:
  287. winner = 'White'
  288. game_over = True
  289. if click_coords in black_locations:
  290. selection = black_locations.index(click_coords)
  291. if turn_step == 2:
  292. turn_step = 3
  293. black_piece = black_locations.index(click_coords)
  294. captured_pieces_white.append(black_pieces[black_piece])
  295. black_pieces.pop(black_piece)
  296. black_locations.pop(black_piece)
  297. if click_coords in valid_moves and selection != 100:
  298. black_locations[selection] = click_coords
  299. if click_coords in white_locations:
  300. white_piece = white_locations.index(click_coords)
  301. captured_pieces_black.append(white_pieces[white_piece])
  302. white_pieces.pop(white_piece)
  303. white_locations.pop(white_piece)
  304. if len(white_pieces) == 0:
  305. winner = 'Black'
  306. game_over = True
  307. black_options = check_options(black_pieces, black_locations, 'black')
  308. white_options = check_options(white_pieces, white_locations, 'white')
  309. turn_step = 0
  310. selection = 100
  311. valid_moves = []
  312. if event.type == pygame.KEYDOWN and game_over:
  313. if event.key == pygame.K_RETURN:
  314. game_over = False
  315. winner = ''
  316. white_pieces = ['man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man']
  317. white_locations = [(0, 7), (2, 7), (4, 7), (6, 7), (1, 6), (3, 6), (5, 6), (7, 6), (0, 5), (2, 5),
  318. (4, 5), (6, 5)]
  319. black_pieces = ['man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man']
  320. black_locations = [(1, 0), (3, 0), (5, 0), (7, 0), (0, 1), (2, 1), (4, 1), (6, 1), (1, 2), (3, 2),
  321. (5, 2), (7, 2)]
  322. turn_step = 0
  323. selection = 100
  324. valid_moves = []
  325. black_options = check_options(black_pieces, black_locations, 'black')
  326. white_options = check_options(white_pieces, white_locations, 'white')
  327. captured_pieces_white = []
  328. captured_pieces_black = []
  329. if winner != '':
  330. game_over = True
  331. draw_game_over()
  332. pygame.display.flip()
  333. pygame.quit()
  334.  
Add Comment
Please, Sign In to add comment