Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. import pygame
  2.  
  3. #定義棋子和棋盤
  4.  
  5. class Chessboard:
  6.  
  7. def __init__(table):
  8. table.grid_size = 26
  9. table.start_x, table.start_y = 30, 50
  10. table.edge_size = table.grid_size / 2
  11. table.grid_count = 19
  12. table.piece = 'b'
  13. table.winner = None
  14. table.game_over = False
  15.  
  16. table.grid = []
  17. for i in range(table.grid_count):
  18. table.grid.append(list("." * table.grid_count))
  19.  
  20. def handle_key_event(table, e):
  21. origin_x = table.start_x - table.edge_size
  22. origin_y = table.start_y - table.edge_size
  23. size = (table.grid_count - 1) * table.grid_size + table.edge_size * 2
  24. pos = e.pos
  25. if origin_x <= pos[0] <= origin_x + size and origin_y <= pos[1] <= origin_y + size:
  26. if not table.game_over:
  27. x = pos[0] - origin_x
  28. y = pos[1] - origin_y
  29. r = int(y // table.grid_size)
  30. c = int(x // table.grid_size)
  31. if table.set_piece(r, c):
  32. table.check_win(r, c)
  33.  
  34. #黑白棋交換
  35.  
  36. def set_piece(table, r, c):
  37. if table.grid[r][c] == '.':
  38. table.grid[r][c] = table.piece
  39.  
  40. if table.piece == 'b':
  41. table.piece = 'w'
  42. else:
  43. table.piece = 'b'
  44.  
  45. return True
  46. return False
  47.  
  48. #判斷輸贏
  49.  
  50. def check_win(table, r, c):
  51. n_count = table.get_continuous_count(r, c, -1, 0)
  52. s_count = table.get_continuous_count(r, c, 1, 0)
  53.  
  54. e_count = table.get_continuous_count(r, c, 0, 1)
  55. w_count = table.get_continuous_count(r, c, 0, -1)
  56.  
  57. se_count = table.get_continuous_count(r, c, 1, 1)
  58. nw_count = table.get_continuous_count(r, c, -1, -1)
  59.  
  60. ne_count = table.get_continuous_count(r, c, -1, 1)
  61. sw_count = table.get_continuous_count(r, c, 1, -1)
  62.  
  63. if (n_count + s_count + 1 >= 5) or (e_count + w_count + 1 >= 5) or \
  64. (se_count + nw_count + 1 >= 5) or (ne_count + sw_count + 1 >= 5):
  65. table.winner = table.grid[r][c]
  66. table.game_over = True
  67.  
  68. def get_continuous_count(table, r, c, dr, dc):
  69. piece = table.grid[r][c]
  70. result = 0
  71. i = 1
  72. while True:
  73. new_r = r + dr * i
  74. new_c = c + dc * i
  75. if 0 <= new_r < table.grid_count and 0 <= new_c < table.grid_count:
  76. if table.grid[new_r][new_c] == piece:
  77. result += 1
  78. else:
  79. break
  80. else:
  81. break
  82. i += 1
  83. return result
  84.  
  85. #劃出棋盤和
  86.  
  87. def draw(table, screen):
  88. # 棋盤底色
  89. pygame.draw.rect(screen, (185, 122, 87),
  90. [table.start_x - table.edge_size, table.start_y - table.edge_size,
  91. (table.grid_count - 1) * table.grid_size + table.edge_size * 2, (table.grid_count - 1) * table.grid_size + table.edge_size * 2], 0)
  92.  
  93. for r in range(table.grid_count):
  94. y = table.start_y + r * table.grid_size
  95. pygame.draw.line(screen, (0, 0, 0), [table.start_x, y], [table.start_x + table.grid_size * (table.grid_count - 1), y], 2)
  96.  
  97. for c in range(table.grid_count):
  98. x = table.start_x + c * table.grid_size
  99. pygame.draw.line(screen, (0, 0, 0), [x, table.start_y], [x, table.start_y + table.grid_size * (table.grid_count - 1)], 2)
  100.  
  101. for r in range(table.grid_count):
  102. for c in range(table.grid_count):
  103. piece = table.grid[r][c]
  104. if piece != '.':
  105. if piece == 'b':
  106. color = (0, 0, 0)
  107. else:
  108. color = (255, 255, 255)
  109.  
  110. x = table.start_x + c * table.grid_size
  111. y = table.start_y + r * table.grid_size
  112. pygame.draw.circle(screen, color, [x, y], table.grid_size // 2)
  113.  
  114. #展現棋盤和下棋
  115.  
  116.  
  117. class Go():
  118.  
  119. def __init__(table):
  120. pygame.init()
  121.  
  122. table.screen = pygame.display.set_mode((800, 600))
  123. pygame.display.set_caption("五子棋")
  124. table.clock = pygame.time.Clock()
  125. table.font = pygame.font.Font(r"C:\Windows\Fonts\consola.ttf", 24)
  126. table.going = True
  127.  
  128. table.chessboard = Chessboard()
  129.  
  130. def loop(table):
  131. while table.going:
  132. table.update()
  133. table.draw()
  134. table.clock.tick(60)
  135.  
  136. pygame.quit()
  137.  
  138. def update(table):
  139. for e in pygame.event.get():
  140. if e.type == pygame.QUIT:
  141. table.going = False
  142. elif e.type == pygame.MOUSEBUTTONDOWN:
  143. table.chessboard.handle_key_event(e)
  144.  
  145. def draw(table):
  146. table.screen.fill((255, 255, 255))
  147.  
  148.  
  149. table.chessboard.draw(table.screen)
  150. if table.chessboard.game_over:
  151. table.screen.blit(table.font.render("{0} Win".format("Black" if table.chessboard.winner == 'b' else "White"), True, (0, 0, 0)), (500, 10))
  152.  
  153. pygame.display.update()
  154.  
  155.  
  156. if __name__ == '__main__':
  157. game = Go()
  158. game.loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement