Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- #定義棋子和棋盤
- class Chessboard:
- def __init__(table):
- table.grid_size = 26
- table.start_x, table.start_y = 30, 50
- table.edge_size = table.grid_size / 2
- table.grid_count = 19
- table.piece = 'b'
- table.winner = None
- table.game_over = False
- table.grid = []
- for i in range(table.grid_count):
- table.grid.append(list("." * table.grid_count))
- def handle_key_event(table, e):
- origin_x = table.start_x - table.edge_size
- origin_y = table.start_y - table.edge_size
- size = (table.grid_count - 1) * table.grid_size + table.edge_size * 2
- pos = e.pos
- if origin_x <= pos[0] <= origin_x + size and origin_y <= pos[1] <= origin_y + size:
- if not table.game_over:
- x = pos[0] - origin_x
- y = pos[1] - origin_y
- r = int(y // table.grid_size)
- c = int(x // table.grid_size)
- if table.set_piece(r, c):
- table.check_win(r, c)
- #黑白棋交換
- def set_piece(table, r, c):
- if table.grid[r][c] == '.':
- table.grid[r][c] = table.piece
- if table.piece == 'b':
- table.piece = 'w'
- else:
- table.piece = 'b'
- return True
- return False
- #判斷輸贏
- def check_win(table, r, c):
- n_count = table.get_continuous_count(r, c, -1, 0)
- s_count = table.get_continuous_count(r, c, 1, 0)
- e_count = table.get_continuous_count(r, c, 0, 1)
- w_count = table.get_continuous_count(r, c, 0, -1)
- se_count = table.get_continuous_count(r, c, 1, 1)
- nw_count = table.get_continuous_count(r, c, -1, -1)
- ne_count = table.get_continuous_count(r, c, -1, 1)
- sw_count = table.get_continuous_count(r, c, 1, -1)
- if (n_count + s_count + 1 >= 5) or (e_count + w_count + 1 >= 5) or \
- (se_count + nw_count + 1 >= 5) or (ne_count + sw_count + 1 >= 5):
- table.winner = table.grid[r][c]
- table.game_over = True
- def get_continuous_count(table, r, c, dr, dc):
- piece = table.grid[r][c]
- result = 0
- i = 1
- while True:
- new_r = r + dr * i
- new_c = c + dc * i
- if 0 <= new_r < table.grid_count and 0 <= new_c < table.grid_count:
- if table.grid[new_r][new_c] == piece:
- result += 1
- else:
- break
- else:
- break
- i += 1
- return result
- #劃出棋盤和
- def draw(table, screen):
- # 棋盤底色
- pygame.draw.rect(screen, (185, 122, 87),
- [table.start_x - table.edge_size, table.start_y - table.edge_size,
- (table.grid_count - 1) * table.grid_size + table.edge_size * 2, (table.grid_count - 1) * table.grid_size + table.edge_size * 2], 0)
- for r in range(table.grid_count):
- y = table.start_y + r * table.grid_size
- pygame.draw.line(screen, (0, 0, 0), [table.start_x, y], [table.start_x + table.grid_size * (table.grid_count - 1), y], 2)
- for c in range(table.grid_count):
- x = table.start_x + c * table.grid_size
- pygame.draw.line(screen, (0, 0, 0), [x, table.start_y], [x, table.start_y + table.grid_size * (table.grid_count - 1)], 2)
- for r in range(table.grid_count):
- for c in range(table.grid_count):
- piece = table.grid[r][c]
- if piece != '.':
- if piece == 'b':
- color = (0, 0, 0)
- else:
- color = (255, 255, 255)
- x = table.start_x + c * table.grid_size
- y = table.start_y + r * table.grid_size
- pygame.draw.circle(screen, color, [x, y], table.grid_size // 2)
- #展現棋盤和下棋
- class Go():
- def __init__(table):
- pygame.init()
- table.screen = pygame.display.set_mode((800, 600))
- pygame.display.set_caption("五子棋")
- table.clock = pygame.time.Clock()
- table.font = pygame.font.Font(r"C:\Windows\Fonts\consola.ttf", 24)
- table.going = True
- table.chessboard = Chessboard()
- def loop(table):
- while table.going:
- table.update()
- table.draw()
- table.clock.tick(60)
- pygame.quit()
- def update(table):
- for e in pygame.event.get():
- if e.type == pygame.QUIT:
- table.going = False
- elif e.type == pygame.MOUSEBUTTONDOWN:
- table.chessboard.handle_key_event(e)
- def draw(table):
- table.screen.fill((255, 255, 255))
- table.chessboard.draw(table.screen)
- if table.chessboard.game_over:
- table.screen.blit(table.font.render("{0} Win".format("Black" if table.chessboard.winner == 'b' else "White"), True, (0, 0, 0)), (500, 10))
- pygame.display.update()
- if __name__ == '__main__':
- game = Go()
- game.loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement