酷代码 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) GRAY = (128, 128, 128) # 棋盘参数 BOARD_SIZE = 900 GRID_SIZE = BOARD_SIZE // 9 PIECE_SIZE = GRID_SIZE * 0.8 # 创建窗口 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, 40) 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 ChessBoard: def __init__(self): self.board = [ ['車', '馬', '相', '仕', '帥', '仕', '相', '馬', '車'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', '砲', ' ', ' ', ' ', ' ', ' ', '砲', ' '], ['卒', ' ', '卒', ' ', '卒', ' ', '卒', ' ', '卒'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['兵', ' ', '兵', ' ', '兵', ' ', '兵', ' ', '兵'], [' ', '炮', ' ', ' ', ' ', ' ', ' ', '炮', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['车', '马', '象', '士', '将', '士', '象', '马', '车'] ] self.current_player = '红' def draw_board(self): 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 i in range(10): for j in range(9): piece = self.board[i][j] if piece != ' ': font = pygame.font.Font(None, int(PIECE_SIZE)) text = font.render(piece, True, RED if self.is_red(piece) else BLACK) text_rect = text.get_rect(center=(j * GRID_SIZE + GRID_SIZE // 2, i * GRID_SIZE + GRID_SIZE // 2)) screen.blit(text, text_rect) def is_red(self, piece): return piece in ['車', '馬', '相', '仕', '帥', '兵', '砲'] def get_possible_moves(self, x, y): piece = self.board[x][y] moves = [] if piece == '車': # 车的走法 for i in range(x + 1, 10): if self.board[i][y] == ' ': moves.append((i, y)) else: if self.is_red(piece) != self.is_red(self.board[i][y]): moves.append((i, y)) break for i in range(x - 1, -1, -1): if self.board[i][y] == ' ': moves.append((i, y)) else: if self.is_red(piece) != self.is_red(self.board[i][y]): moves.append((i, y)) break for j in range(y + 1, 9): if self.board[x][j] == ' ': moves.append((x, j)) else: if self.is_red(piece) != self.is_red(self.board[x][j]): moves.append((x, j)) break for j in range(y - 1, -1, -1): if self.board[x][j] == ' ': moves.append((x, j)) else: if self.is_red(piece) != self.is_red(self.board[x][j]): moves.append((x, j)) break # 其他棋子走法类似,这里省略部分代码,可自行补充 return moves def move_piece(self, from_x, from_y, to_x, to_y): if (to_x, to_y) in self.get_possible_moves(from_x, from_y): self.board[to_x][to_y] = self.board[from_x][from_y] self.board[from_x][from_y] = ' ' self.current_player = '黑' if self.current_player == '红' else '红' return True return False # 主函数 def main(): username = login() board = ChessBoard() selected_piece = None while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: x, y = event.pos grid_x = y // GRID_SIZE grid_y = x // GRID_SIZE if selected_piece is None: if board.board[grid_x][grid_y] != ' ' and (board.is_red(board.board[grid_x][grid_y]) and board.current_player == '红' or not board.is_red(board.board[grid_x][grid_y]) and board.current_player == '黑'): selected_piece = (grid_x, grid_y) else: if board.move_piece(selected_piece[0], selected_piece[1], grid_x, grid_y): selected_piece = None else: if board.board[grid_x][grid_y] != ' ' and (board.is_red(board.board[grid_x][grid_y]) and board.current_player == '红' or not board.is_red(board.board[grid_x][grid_y]) and board.current_player == '黑'): selected_piece = (grid_x, grid_y) else: selected_piece = None board.draw_board() if selected_piece is not None: possible_moves = board.get_possible_moves(selected_piece[0], selected_piece[1]) for move in possible_moves: pygame.draw.circle(screen, GRAY, (move[1] * GRID_SIZE + GRID_SIZE // 2, move[0] * GRID_SIZE + GRID_SIZE // 2), 10) pygame.display.flip() if __name__ == "__main__": main() ``` ### 代码说明: 1. **登录页面**:使用`login`函数实现登录页面,用户输入用户名后按回车键进入游戏。 2. **棋盘类`ChessBoard`**: - `__init__`方法初始化棋盘和当前玩家。 - `draw_board`方法绘制棋盘和棋子。 - `is_red`方法判断棋子颜色。 - `get_possible_moves`方法根据棋子类型返回可能的移动位置。 - `move_piece`方法移动棋子并切换玩家。 3. **主函数`main`**:处理用户输入,调用棋盘类的方法进行游戏逻辑处理,并更新屏幕显示。 ### 注意事项: - 代码中只实现了车的走法,其他棋子的走法需要自行补充。 - 将军、绝杀提示等功能也需要进一步完善。 ######[AI问答 | 2205点数解答 | 2026-02-15 10:13:11]

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