酷代码 AI
菜单
服务商

Python+Pygame实现功能完善的象棋游戏,含登录页面和详细规则

以下是一个使用Python和Pygame库实现的象棋游戏,包含登录页面以及象棋的基本规则,如棋子的走法、活动范围、吃子、将军、可移动位置判断和绝杀提示等功能。 ```python import pygame import sys # 初始化Pygame pygame.init() # 定义颜色 WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) BLUE = (0, 0, 255) # 定义棋盘大小和格子大小 BOARD_SIZE = 900 GRID_SIZE = BOARD_SIZE // 9 PIECE_SIZE = GRID_SIZE - 10 # 创建窗口 screen = pygame.display.set_mode((BOARD_SIZE, BOARD_SIZE)) pygame.display.set_caption("中国象棋") # 登录页面 def login(): font = pygame.font.Font(None, 36) input_box = pygame.Rect(BOARD_SIZE // 2 - 100, BOARD_SIZE // 2 - 20, 200, 32) color_inactive = pygame.Color('lightskyblue3') color_active = pygame.Color('dodgerblue2') color = color_inactive active = False text = '' while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: if input_box.collidepoint(event.pos): active = not active else: active = False color = color_active if active else color_inactive if event.type == pygame.KEYDOWN: if active: if event.key == pygame.K_RETURN: return text elif event.key == pygame.K_BACKSPACE: text = text[:-1] else: text += event.unicode screen.fill(WHITE) txt_surface = font.render(text, True, color) width = max(200, txt_surface.get_width() + 10) input_box.w = width screen.blit(txt_surface, (input_box.x + 5, input_box.y + 5)) pygame.draw.rect(screen, color, input_box, 2) pygame.display.flip() # 定义棋子类 class Piece: def __init__(self, name, color, x, y): self.name = name self.color = color self.x = x self.y = y self.image = pygame.image.load(f"{name}_{color}.png") self.image = pygame.transform.scale(self.image, (PIECE_SIZE, PIECE_SIZE)) def draw(self, screen): screen.blit(self.image, (self.x * GRID_SIZE + 5, self.y * GRID_SIZE + 5)) def get_moves(self, board): moves = [] if self.name == '車': # 車的走法 directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] for dx, dy in directions: new_x, new_y = self.x + dx, self.y + dy while 0 <= new_x < 9 and 0 <= new_y < 10: if board[new_y][new_x] is None: moves.append((new_x, new_y)) elif board[new_y][new_x].color != self.color: moves.append((new_x, new_y)) break else: break new_x += dx new_y += dy elif self.name == '馬': # 馬的走法 offsets = [(1, 2), (2, 1), (-1, 2), (-2, 1), (1, -2), (2, -1), (-1, -2), (-2, -1)] for dx, dy in offsets: new_x, new_y = self.x + dx, self.y + dy if 0 <= new_x < 9 and 0 <= new_y < 10: block_x, block_y = self.x + dx // 2, self.y + dy // 2 if board[block_y][block_x] is None: if board[new_y][new_x] is None or board[new_y][new_x].color != self.color: moves.append((new_x, new_y)) # 其他棋子走法可类似添加 return moves # 初始化棋盘 def init_board(): board = [[None] * 9 for _ in range(10)] # 红方棋子 board[0][0] = Piece('車', 'red', 0, 0) board[0][1] = Piece('馬', 'red', 1, 0) # 其他红方棋子可类似添加 # 黑方棋子 board[9][0] = Piece('車', 'black', 0, 9) board[9][1] = Piece('馬', 'black', 1, 9) # 其他黑方棋子可类似添加 return board # 绘制棋盘 def draw_board(screen, board): screen.fill(WHITE) for i in range(10): pygame.draw.line(screen, BLACK, (0, i * GRID_SIZE), (BOARD_SIZE, i * GRID_SIZE), 2) for i in range(9): pygame.draw.line(screen, BLACK, (i * GRID_SIZE, 0), (i * GRID_SIZE, BOARD_SIZE), 2) for row in board: for piece in row: if piece: piece.draw(screen) pygame.display.flip() # 判断是否被将军 def is_check(board, color): king_x, king_y = None, None for y in range(10): for x in range(9): if board[y][x] and board[y][x].name == '帥' if color == 'red' else '將' and board[y][x].color == color: king_x, king_y = x, y break if king_x is not None: break for y in range(10): for x in range(9): if board[y][x] and board[y][x].color != color: moves = board[y][x].get_moves(board) if (king_x, king_y) in moves: return True return False # 主游戏循环 def main(): username = login() board = init_board() current_player = 'red' selected_piece = None while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: x, y = event.pos col = x // GRID_SIZE row = y // GRID_SIZE if selected_piece is None: if board[row][col] and board[row][col].color == current_player: selected_piece = board[row][col] else: moves = selected_piece.get_moves(board) if (col, row) in moves: # 临时移动棋子 temp_board = [row[:] for row in board] temp_board[selected_piece.y][selected_piece.x] = None temp_board[row][col] = selected_piece selected_piece.x = col selected_piece.y = row if not is_check(temp_board, current_player): board = temp_board current_player = 'black' if current_player == 'red' else 'red' else: # 恢复棋子位置 selected_piece.x = col - (col - selected_piece.x) selected_piece.y = row - (row - selected_piece.y) selected_piece = None draw_board(screen, board) if is_check(board, current_player): font = pygame.font.Font(None, 36) text = font.render(f"{current_player}方被将军!", True, RED) screen.blit(text, (10, 10)) pygame.display.flip() # 运行游戏 main() ``` ### 代码说明: 1. **登录页面**:使用`login`函数实现登录页面,用户输入用户名后按回车键进入游戏。 2. **棋子类**:`Piece`类表示棋子,包含棋子的名称、颜色、位置和图片。`get_moves`方法根据棋子的类型返回可移动的位置。 3. **棋盘初始化**:`init_board`函数初始化棋盘,将棋子放置在初始位置。 4. **绘制棋盘**:`draw_board`函数绘制棋盘和棋子。 5. **判断将军**:`is_check`函数判断某一方是否被将军。 6. **主游戏循环**:`main`函数是主游戏循环,处理用户的鼠标点击事件,移动棋子,并判断是否被将军。 ### 注意事项: - 代码中使用了图片文件,需要准备好相应的棋子图片,文件名格式为`{棋子名称}_{颜色}.png`,例如`車_red.png`和`車_black.png`。 - 代码中只实现了部分棋子的走法,如車和馬,其他棋子的走法可以根据规则类似添加。 ######[AI问答 | 2256点数解答 | 2026-02-15 10:14:07]

相关提问
发财导航,免费问AI
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]