Here's a basic implementation of Chinese Chess (Xiangqi) using Pygame, allowing two human players to play against each other in turns. This version does not include AI, only the board and pieces, with basic movement rules and turn-based interaction.
✅ Features:
- 9x10 Xiangqi board
- Basic piece images or letters for simplicity
- Turn-based gameplay
- Basic move validation (no check/checkmate or full rule enforcement)
- Click-to-select and click-to-move interface
📁 Requirements:
Make sure you have pygame installed:
pip install pygame
🧩 Code: Two-player Chinese Chess (Xiangqi)
import pygame
import sys
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 900
ROWS, COLS = 10, 9
SQUARE_SIZE = 80
BOARD_X_OFFSET = (WIDTH - COLS * SQUARE_SIZE) // 2
BOARD_Y_OFFSET = (HEIGHT - ROWS * SQUARE_SIZE) // 2
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (200, 0, 0)
GREEN = (0, 200, 0)
BLUE = (50, 50, 200)
YELLOW = (255, 255, 0)
# Piece mapping
PIECES = {
'r': '车', 'n': '马', 'b': '相', 'a': '士', 'k': '将', 'c': '炮', 'p': '兵',
'R': '俥', 'N': '馬', 'B': '象', 'A': '仕', 'K': '帅', 'C': '砲', 'P': '卒'
}
# Initial FEN-like setup for board
INITIAL_BOARD = [
['r', 'n', 'b', 'a', 'k', 'a', 'b', 'n', 'r'],
['', '', '', '', '', '', '', '', ''],
['', 'c', '', '', '', '', '', 'c', ''],
['p', '', 'p', '', 'p', '', 'p', '', 'p'],
['', '', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', '', ''],
['P', '', 'P', '', 'P', '', 'P', '', 'P'],
['', 'C', '', '', '', '', '', 'C', ''],
['', '', '', '', '', '', '', '', ''],
['R', 'N', 'B', 'A', 'K', 'A', 'B', 'N', 'R']
]
# Screen setup
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Chinese Chess (Xiangqi) - Two Player")
# Fonts
font = pygame.font.SysFont("SimHei", 36, bold=True) # Supports Chinese characters
# Game state
selected_pos = None
turn = 'red' # red moves first
def draw_board(screen):
screen.fill(WHITE)
# Draw grid lines
for row in range(ROWS):
for col in range(COLS):
rect = (BOARD_X_OFFSET + col*SQUARE_SIZE,
BOARD_Y_OFFSET + row*SQUARE_SIZE,
SQUARE_SIZE, SQUARE_SIZE)
pygame.draw.rect(screen, BLACK, rect, 1)
# Draw river
river_rect = (BOARD_X_OFFSET, BOARD_Y_OFFSET + 4*SQUARE_SIZE, COLS*SQUARE_SIZE, SQUARE_SIZE)
pygame.draw.rect(screen, BLUE, river_rect, 0)
river_text = font.render("楚河 汉界", True, WHITE)
screen.blit(river_text, (BOARD_X_OFFSET + 100, BOARD_Y_OFFSET + 4*SQUARE_SIZE + 20))
def draw_pieces(screen, board):
for row in range(ROWS):
for col in range(COLS):
piece = board[row][col]
if piece:
color = RED if piece.islower() else BLACK
text = PIECES[piece]
txt_surface = font.render(text, True, color)
center_x = BOARD_X_OFFSET + col * SQUARE_SIZE + SQUARE_SIZE // 2
center_y = BOARD_Y_OFFSET + row * SQUARE_SIZE + SQUARE_SIZE // 2
screen.blit(txt_surface, txt_surface.get_rect(center=(center_x, center_y)))
def get_square_under_mouse(pos):
x, y = pos
col = (x - BOARD_X_OFFSET) // SQUARE_SIZE
row = (y - BOARD_Y_OFFSET) // SQUARE_SIZE
if 0 <= col < COLS and 0 <= row < ROWS:
return (row, col)
return None
def is_valid_move(board, start, end, turn):
row1, col1 = start
row2, col2 = end
piece = board[row1][col1]
target = board[row2][col2]
if piece == '':
return False
# Ensure correct player's turn
if turn == 'red' and not piece.isupper():
return False
if turn == 'black' and not piece.islower():
return False
# Placeholder: allow all captures within bounds
# You can expand this with full movement logic later
return True
def main():
global selected_pos, turn
board = [row[:] for row in INITIAL_BOARD]
clock = pygame.time.Clock()
running = True
while running:
draw_board(screen)
draw_pieces(screen, board)
if selected_pos:
s_row, s_col = selected_pos
pygame.draw.rect(screen, YELLOW, (
BOARD_X_OFFSET + s_col * SQUARE_SIZE,
BOARD_Y_OFFSET + s_row * SQUARE_SIZE,
SQUARE_SIZE, SQUARE_SIZE), 3)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
square = get_square_under_mouse(pos)
if square:
row, col = square
if selected_pos:
# Try to move
if is_valid_move(board, selected_pos, (row, col), turn):
piece = board[selected_pos[0]][selected_pos[1]]
captured = board[row][col]
board[row][col] = piece
board[selected_pos[0]][selected_pos[1]] = ''
selected_pos = None
# Switch turn
turn = 'black' if turn == 'red' else 'red'
else:
selected_pos = None
else:
if board[row][col]:
selected_pos = (row, col)
clock.tick(60)
if __name__ == "__main__":
main()
📝 Notes:
- The game uses letters to represent pieces (
r=rook, n=knight, etc.) with proper Chinese characters.
- Move validation is minimal (just checks if the piece belongs to the current player).
- No capture logic or win condition — feel free to expand!
- Uses
SimHei font which supports Chinese characters. If unavailable, replace with another font or use English labels.
🔧 To Do (Optional Improvements):
- Add full movement rules per piece
- Enforce palace and river restrictions
- Add check/checkmate detection
- Highlight valid moves
- Use actual images for pieces
- Sound effects
Let me know if you want a more advanced version with those features!