Guest User

Untitled

a guest
Sep 4th, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.95 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_prmotions = ['king']
  34.  
  35. white_king = pygame.image.load('Checkers Piece White(king).png')
  36. white_king = pygame.transform.scale(white_king, (150, 150))
  37. white_king_small = pygame.transform.scale(white_king, (45, 45))
  38. white_images = [white_man, white_king]
  39. small_white_images = [white_man_small, white_king_small]
  40.  
  41. black_man = pygame.image.load('Checkers Piece Black(man).png')
  42. black_man = pygame.transform.scale(black_man, (150, 150))
  43. black_man_small = pygame.transform.scale(black_man, (45, 45))
  44.  
  45. black_promotions = ['king']
  46.  
  47. black_king = pygame.image.load('Checkers Piece Black(king).png')
  48. black_king = pygame.transform.scale(black_king, (150, 150))
  49. black_king_small = pygame.transform.scale(black_king, (45, 45))
  50. black_images = [black_man, black_king]
  51. small_black_images = [black_man_small, black_king_small]
  52.  
  53. piece_list = ['man', 'king']
  54.  
  55. # check variables
  56. winner = ''
  57. game_over = False
  58. white_promote = False
  59. black_promote = False
  60. promo_index = 100
  61.  
  62. # draw main game board
  63. def draw_board():
  64. for i in range(32):
  65. column = i % 4
  66. row = i // 4
  67. if row % 2 == 0:
  68. pygame.draw.rect(screen, 'light gray', [600 - (column * 200), row * 100, 100, 100])
  69. else:
  70. pygame.draw.rect(screen, 'light gray', [700 - (column * 200), row * 100, 100, 100])
  71. pygame.draw.rect(screen, 'gray', [0, 800, width, 200])
  72. pygame.draw.rect(screen, 'red', [0, 800, width, 150], 5)
  73. pygame.draw.rect(screen, 'red', [800, 0, 200, height], 5)
  74. status_text = ['WHITE: Select a piece to move', 'WHITE: Select a place to move to',
  75. 'BLACK: Select a piece to move', 'BLACK: Select a place to move to']
  76. screen.blit(big_font.render(status_text[turn_step], True, 'black'), (20, 820))
  77. for j in range(9):
  78. pygame.draw.line(screen, 'black', (0, 100 * j), (800, 100 * j), 2)
  79. pygame.draw.line(screen, 'black', (100 * j, 0), (100 * j, 800), 2)
  80. screen.blit(big_font.render('FORFEIT', True, 'black'), (825, 850))
  81. if white_promote or black_promote:
  82. pygame.draw.rect(screen, 'gray', [0, 800, width - 200])
  83. pygame.draw.rect(screen, 'red', [0, 800, width - 150], 5)
  84. screen.blit(big_font.render('Select the king piece to promote',True,'black')(20,820))
  85.  
  86.  
  87. # draw pieces
  88. def draw_pieces():
  89. square_size = 100
  90. piece_size = 150
  91. offset = (square_size - piece_size) // 2
  92. for i in range(len(black_pieces)):
  93. index = piece_list.index(white_pieces[i])
  94. x_pos = white_locations[i][0] * square_size + offset
  95. y_pos = white_locations[i][1] * square_size + offset
  96. if white_pieces[i] == 'man':
  97. screen.blit(white_man, (x_pos, y_pos))
  98. else:
  99. screen.blit(white_images[index], (x_pos, y_pos))
  100. if turn_step < 2:
  101. if selection == i:
  102. pygame.draw.rect(screen, 'gold',
  103. [white_locations[i][0] * 100 + 1, white_locations[i][1] * 100 + 1, 100, 100], 2)
  104.  
  105. if len(black_pieces) != len(black_locations):
  106. print(
  107. f"Error: Mismatch in lengths. black_pieces: {len(black_pieces)}, black_locations: {len(black_locations)}")
  108. return
  109. for i in range(len(black_pieces)):
  110. if i >= len(black_locations): # Safety check
  111. print(f"Error: Index {i} is out of range for black_locations")
  112. break
  113. index = piece_list.index(black_pieces[i])
  114. x_pos = black_locations[i][0] * square_size + offset
  115. y_pos = black_locations[i][1] * square_size + offset
  116. if black_pieces[i] == 'man':
  117. screen.blit(black_man, (x_pos, y_pos))
  118. else:
  119. screen.blit(black_images[index], (x_pos, y_pos))
  120. if turn_step >= 2:
  121. if selection == i:
  122. pygame.draw.rect(screen, 'silver',
  123. [black_locations[i][0] * 100 + 1, black_locations[i][1] * 100 + 1, 100, 100], 2)
  124.  
  125.  
  126. # check all valid piece options on board
  127. def check_options(pieces, locations, turn):
  128. if len(pieces) != len(locations):
  129. print(f"Error: Mismatch in lengths. Pieces: {len(pieces)}, Locations: {len(locations)}")
  130. return []
  131.  
  132. moves_list = []
  133. all_moves_list = []
  134. for i in range(len(pieces)):
  135. location = locations[i]
  136. piece = pieces[i]
  137. if piece == 'man':
  138. moves_list = check_man(location, turn)
  139. elif piece == 'king':
  140. moves_list = check_king(location, turn)
  141. all_moves_list.append(moves_list)
  142. return all_moves_list
  143.  
  144.  
  145. # check valid man moves
  146. def check_man(position, colour):
  147. moves_list = []
  148. if colour == 'white':
  149. if (position[0] - 1 >= 0 and position[1] - 1 >= 0 and (
  150. position[0] - 1, position[1] - 1) not in white_locations and (
  151. position[0] - 1, position[1] - 1) not in black_locations):
  152. moves_list.append((position[0] - 1, position[1] - 1))
  153. if (position[0] + 1 < 8 and position[1] - 1 >= 0 and (
  154. position[0] + 1, position[1] - 1) not in white_locations and (
  155. position[0] + 1, position[1] - 1) not in black_locations):
  156. moves_list.append((position[0] + 1, position[1] - 1))
  157.  
  158. # Capture pieces
  159. if (position[0] - 2 >= 0 and position[1] - 2 >= 0 and (
  160. position[0] - 1, position[1] - 1) in black_locations and (
  161. position[0] - 2, position[1] - 2) not in white_locations and (
  162. position[0] - 2, position[1] - 2) not in black_locations):
  163. moves_list.append((position[0] - 2, position[1] - 2))
  164. black_locations.remove((position[0] - 1, position[1] - 1))
  165. if (position[0] + 2 < 8 and position[1] - 2 >= 0 and (position[0] + 1, position[1] - 1) in black_locations and (
  166. position[0] + 2, position[1] - 2) not in white_locations and (
  167. position[0] + 2, position[1] - 2) not in black_locations):
  168. moves_list.append((position[0] + 2, position[1] - 2))
  169. black_locations.remove((position[0] + 1, position[1] - 1))
  170.  
  171. else:
  172. if (position[0] - 1 >= 0 and position[1] + 1 < 8 and (
  173. position[0] - 1, position[1] + 1) not in white_locations and (
  174. position[0] - 1, position[1] + 1) not in black_locations):
  175. moves_list.append((position[0] - 1, position[1] + 1))
  176. if (position[0] + 1 < 8 and position[1] + 1 < 8 and (
  177. position[0] + 1, position[1] + 1) not in white_locations and (
  178. position[0] + 1, position[1] + 1) not in black_locations):
  179. moves_list.append((position[0] + 1, position[1] + 1))
  180.  
  181. # capture pieces
  182. if (position[0] - 2 >= 0 and position[1] + 2 < 8 and (position[0] - 1, position[1] + 1) in white_locations and (
  183. position[0] - 2, position[1] + 2) not in white_locations and (
  184. position[0] - 2, position[1] + 2) not in black_locations):
  185. moves_list.append((position[0] - 2, position[1] + 2))
  186. white_locations.remove((position[0] - 1, position[1] + 1))
  187. if (position[0] + 2 < 8 and position[1] + 2 < 8 and (position[0] + 1, position[1] + 1) in white_locations and (
  188. position[0] + 2, position[1] + 2) not in white_locations and (
  189. position[0] + 2, position[1] + 2) not in black_locations):
  190. moves_list.append((position[0] + 2, position[1] + 2))
  191. white_locations.remove((position[0] + 1, position[1] + 1))
  192.  
  193. return moves_list
  194.  
  195.  
  196. # check valid king moves
  197. def check_king(position, colour):
  198. moves_list = []
  199. if colour == 'white':
  200. ally_list = white_locations
  201. else:
  202. ally_list = black_locations
  203. directions = [(-1, -1), (-1, 1), (1, -1), (1, 1)]
  204. for i in range(4):
  205. target = (position[0] + directions[i][0], position[1] + directions[i][1])
  206. if target not in ally_list and 0 <= target[0] <= 7 and 0 <= target[1] <= 7:
  207. moves_list.append(target)
  208. return moves_list
  209.  
  210.  
  211. # check valid moves only for selected piece
  212. def check_valid_moves():
  213. if turn_step < 2:
  214. options_list = white_options
  215. else:
  216. options_list = black_options
  217. valid_options = options_list[selection]
  218. return valid_options
  219.  
  220.  
  221. # draw valid moves on screen
  222. def draw_valid(moves):
  223. if turn_step < 2:
  224. colour = 'gold'
  225. else:
  226. colour = 'blue'
  227. for i in range(len(moves)):
  228. pygame.draw.circle(screen, colour, (moves[i][0] * 100 + 50, moves[i][1] * 100 + 50), 5)
  229.  
  230.  
  231. # draw captured pieces on side of screen
  232. def draw_captured():
  233. for i in range(len(captured_pieces_white)):
  234. captured_piece = captured_pieces_white[i]
  235. index = piece_list.index(captured_piece)
  236. screen.blit(small_black_images[index], (825, 5 + 50 + i))
  237. for i in range(len(captured_pieces_black)):
  238. captured_piece = captured_pieces_black[i]
  239. index = piece_list.index(captured_piece)
  240. screen.blit(small_white_images[index], (925, 5 + 50 + i))
  241.  
  242. #draw game over sign
  243. def draw_game_over():
  244. pygame.draw.rect(screen, 'black', [200, 200, 400, 70])
  245. screen.blit(font.render(f'{winner} is victorious', True, 'yellow'), (210, 210))
  246. screen.blit(font.render(f'Press ENTER to start new game', True, 'green'), (210, 240))
  247.  
  248. #man promotion
  249. def check_promotion():
  250. man_indexes = []
  251. white_promotion = False
  252. black_promotion = False
  253. promote_index = 100
  254. for i in range(len(white_pieces)):
  255. if white_pieces[i]== 'man':
  256. man_indexes.append(i)
  257. for i in range(len(man_indexes)):
  258. if white_locations[man_indexes[i]][1] == 0:
  259. white_promotion = True
  260. promote_index = man_indexes[i]
  261. man_indexes = []
  262.  
  263. for i in range(len(black_pieces)):
  264. if black_pieces[i]== 'man':
  265. man_indexes.append(i)
  266. for i in range(len(man_indexes)):
  267. if black_locations[man_indexes[i]][1] == 0:
  268. black_promotion = True
  269. promote_index = man_indexes[i]
  270. return white_promotion, black_promotion, promote_index
  271.  
  272. def draw_promotion():
  273. pygame.draw.rect(screen,'dark gray',[800,0,200,420])
  274. if white_promote:
  275. colour = 'white'
  276. for i in range(len(white_promotions)):
  277. piece = white_promotions[i]
  278. index = piece_list.index(piece)
  279. screen.blit(white_images[index],(800,5+100*i))
  280. elif black_promote:
  281. colour = 'black'
  282. for i in range(len(black_promotions)):
  283. piece = black_promotions[i]
  284. index = piece_list.index(piece)
  285. screen.blit(black_images[index],(800,5+100*i))
  286. pygame.draw.rect(screen,colour,[800,0,200,420],8)
  287.  
  288. def check_promo_select():
  289. mouse_pos = pygame.mouse.get_pos()
  290. left_click = pygame.mouse.get_pressed()[0]
  291. x_position = mouse_pos[0]//100
  292. y_position = mouse_pos[1]//100
  293. if white_promote and left_click and x_position < 0 and y_position < 2:
  294. white_pieces[promo_index] = white_promotions[y_position]
  295. elif black_promote and left_click and x_position < 0 and y_position < 2:
  296. black_pieces[promo_index] = black_promotions[y_position]
  297.  
  298. # main game loop
  299.  
  300. black_options = check_options(black_pieces, black_locations, 'black')
  301. white_options = check_options(white_pieces, white_locations, 'white')
  302.  
  303. run = True
  304. while run:
  305. timer.tick(fps)
  306. screen.fill('dimgray')
  307. draw_board()
  308. draw_pieces()
  309. draw_captured()
  310. if not game_over:
  311. white_promote,black_promote, promo_index = check_promotion()
  312. if white_promote or black_promote:
  313. draw_promotion()
  314. check_promo_select()
  315.  
  316. if selection != 100:
  317. valid_moves = check_valid_moves()
  318. draw_valid(valid_moves)
  319.  
  320. # event handling
  321. for event in pygame.event.get():
  322. if event.type == pygame.QUIT:
  323. run = False
  324. if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and not game_over:
  325. x_coord = event.pos[0] // 100
  326. y_coord = event.pos[1] // 100
  327. click_coords = (x_coord, y_coord)
  328. if turn_step <= 1:
  329. if click_coords == (8, 8) or click_coords == (9, 8):
  330. winner = 'Black'
  331. game_over = True
  332. if click_coords in white_locations:
  333. selection = white_locations.index(click_coords)
  334. if turn_step == 0:
  335. turn_step = 1
  336. if click_coords in valid_moves and selection != 100:
  337. white_locations[selection] = click_coords
  338. if click_coords in black_locations:
  339. black_piece = black_locations.index(click_coords)
  340. captured_pieces_white.append(black_pieces[black_piece])
  341. if black_pieces[black_piece] == 0:
  342. winner = 'White'
  343. game_over = True
  344. black_pieces.pop(black_piece)
  345. black_locations.pop(black_piece)
  346. black_options = check_options(black_pieces, black_locations, 'black')
  347. white_options = check_options(white_pieces, white_locations, 'white')
  348. turn_step = 2
  349. selection = 100
  350. valid_moves = []
  351. if turn_step > 1:
  352. if len(black_pieces) == 0:
  353. winner = 'White'
  354. game_over = True
  355. if click_coords in black_locations:
  356. selection = black_locations.index(click_coords)
  357. if turn_step == 2:
  358. turn_step = 3
  359. black_piece = black_locations.index(click_coords)
  360. captured_pieces_white.append(black_pieces[black_piece])
  361. black_pieces.pop(black_piece)
  362. black_locations.pop(black_piece)
  363. if click_coords in valid_moves and selection != 100:
  364. black_locations[selection] = click_coords
  365. if click_coords in white_locations:
  366. white_piece = white_locations.index(click_coords)
  367. captured_pieces_black.append(white_pieces[white_piece])
  368. white_pieces.pop(white_piece)
  369. white_locations.pop(white_piece)
  370. if len(white_pieces) == 0:
  371. winner = 'Black'
  372. game_over = True
  373. black_options = check_options(black_pieces, black_locations, 'black')
  374. white_options = check_options(white_pieces, white_locations, 'white')
  375. turn_step = 0
  376. selection = 100
  377. valid_moves = []
  378. if event.type == pygame.KEYDOWN and game_over:
  379. if event.key == pygame.K_RETURN:
  380. game_over = False
  381. winner = ''
  382. white_pieces = ['man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man']
  383. white_locations = [(0, 7), (2, 7), (4, 7), (6, 7), (1, 6), (3, 6), (5, 6), (7, 6), (0, 5), (2, 5),
  384. (4, 5), (6, 5)]
  385. black_pieces = ['man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man', 'man']
  386. black_locations = [(1, 0), (3, 0), (5, 0), (7, 0), (0, 1), (2, 1), (4, 1), (6, 1), (1, 2), (3, 2),
  387. (5, 2), (7, 2)]
  388. turn_step = 0
  389. selection = 100
  390. valid_moves = []
  391. black_options = check_options(black_pieces, black_locations, 'black')
  392. white_options = check_options(white_pieces, white_locations, 'white')
  393. captured_pieces_white = []
  394. captured_pieces_black = []
  395. if winner != '':
  396. game_over = True
  397. draw_game_over()
  398. pygame.display.flip()
  399. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment