I'll create a Chinese Chess (Xiangqi) game for two players using Pygame. Here's a complete implementation:
import pygame
import sys
# Initialize Pygame
pygame.init()
# Constants
BOARD_WIDTH = 9
BOARD_HEIGHT = 10
CELL_SIZE = 60
MARGIN = 40
WINDOW_WIDTH = CELL_SIZE * (BOARD_WIDTH - 1) + 2 * MARGIN
WINDOW_HEIGHT = CELL_SIZE * (BOARD_HEIGHT - 1) + 2 * MARGIN
# Colors
BOARD_COLOR = (255, 220, 180)
LINE_COLOR = (0, 0, 0)
RED_COLOR = (200, 0, 0)
BLACK_COLOR = (0, 0, 0)
SELECTED_COLOR = (0, 255, 0)
VALID_MOVE_COLOR = (100, 200, 100)
# Piece types
GENERAL = 'G'
ADVISOR = 'A'
ELEPHANT = 'E'
HORSE = 'H'
CHARIOT = 'R'
CANNON = 'C'
SOLDIER = 'S'
class Piece:
def __init__(self, piece_type, color, x, y):
self.type = piece_type
self.color = color
self.x = x
self.y = y
def get_chinese_char(self):
if self.color == 'red':
chars = {
GENERAL: '帥', ADVISOR: '仕', ELEPHANT: '相',
HORSE: '馬', CHARIOT: '車', CANNON: '炮', SOLDIER: '兵'
}
else:
chars = {
GENERAL: '將', ADVISOR: '士', ELEPHANT: '象',
HORSE: '馬', CHARIOT: '車', CANNON: '砲', SOLDIER: '卒'
}
return chars[self.type]
class ChineseChessGame:
def __init__(self):
self.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Chinese Chess (Xiangqi)")
self.clock = pygame.time.Clock()
self.font = pygame.font.Font("simhei.ttf", 30) if pygame.font.match_font("simhei") else pygame.font.Font(None, 30)
self.board = [[None for _ in range(BOARD_WIDTH)] for _ in range(BOARD_HEIGHT)]
self.selected_piece = None
self.current_player = 'red'
self.valid_moves = []
self.init_pieces()
def init_pieces(self):
# Red pieces (bottom)
self.board[9][0] = Piece(CHARIOT, 'red', 0, 9)
self.board[9][1] = Piece(HORSE, 'red', 1, 9)
self.board[9][2] = Piece(ELEPHANT, 'red', 2, 9)
self.board[9][3] = Piece(ADVISOR, 'red', 3, 9)
self.board[9][4] = Piece(GENERAL, 'red', 4, 9)
self.board[9][5] = Piece(ADVISOR, 'red', 5, 9)
self.board[9][6] = Piece(ELEPHANT, 'red', 6, 9)
self.board[9][7] = Piece(HORSE, 'red', 7, 9)
self.board[9][8] = Piece(CHARIOT, 'red', 8, 9)
self.board[7][1] = Piece(CANNON, 'red', 1, 7)
self.board[7][7] = Piece(CANNON, 'red', 7, 7)
for i in range(0, 9, 2):
self.board[6][i] = Piece(SOLDIER, 'red', i, 6)
# Black pieces (top)
self.board[0][0] = Piece(CHARIOT, 'black', 0, 0)
self.board[0][1] = Piece(HORSE, 'black', 1, 0)
self.board[0][2] = Piece(ELEPHANT, 'black', 2, 0)
self.board[0][3] = Piece(ADVISOR, 'black', 3, 0)
self.board[0][4] = Piece(GENERAL, 'black', 4, 0)
self.board[0][5] = Piece(ADVISOR, 'black', 5, 0)
self.board[0][6] = Piece(ELEPHANT, 'black', 6, 0)
self.board[0][7] = Piece(HORSE, 'black', 7, 0)
self.board[0][8] = Piece(CHARIOT, 'black', 8, 0)
self.board[2][1] = Piece(CANNON, 'black', 1, 2)
self.board[2][7] = Piece(CANNON, 'black', 7, 2)
for i in range(0, 9, 2):
self.board[3][i] = Piece(SOLDIER, 'black', i, 3)
def get_valid_moves(self, piece):
moves = []
x, y = piece.x, piece.y
if piece.type == GENERAL:
# General moves within palace
palace_x = range(3, 6)
palace_y = range(0, 3) if piece.color == 'black' else range(7, 10)
for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
new_x, new_y = x + dx, y + dy
if new_x in palace_x and new_y in palace_y:
if not self.board[new_y][new_x] or self.board[new_y][new_x].color != piece.color:
moves.append((new_x, new_y))
elif piece.type == ADVISOR:
# Advisor moves diagonally within palace
palace_x = range(3, 6)
palace_y = range(0, 3) if piece.color == 'black' else range(7, 10)
for dx, dy in [(1, 1), (1, -1), (-1, 1), (-1, -1)]:
new_x, new_y = x + dx, y + dy
if new_x in palace_x and new_y in palace_y:
if not self.board[new_y][new_x] or self.board[new_y][new_x].color != piece.color:
moves.append((new_x, new_y))
elif piece.type == ELEPHANT:
# Elephant moves exactly 2 points diagonally, can't cross river
river_y = range(0, 5) if piece.color == 'black' else range(5, 10)
for dx, dy in [(2, 2), (2, -2), (-2, 2), (-2, -2)]:
new_x, new_y = x + dx, y + dy
mid_x, mid_y = x + dx // 2, y + dy // 2
if 0 <= new_x < BOARD_WIDTH and new_y in river_y:
if not self.board[mid_y][mid_x]: # Check if path is clear
if not self.board[new_y][new_x] or self.board[new_y][new_x].color != piece.color:
moves.append((new_x, new_y))
elif piece.type == HORSE:
# Horse moves in L-shape, can be blocked
horse_moves = [
(2, 1, 1, 0), (2, -1, 1, 0), (-2, 1, -1, 0), (-2, -1, -1, 0),
(1, 2, 0, 1), (1, -2, 0, -1), (-1, 2, 0, 1), (-1, -2, 0, -1)
]
for dx, dy, block_x, block_y in horse_moves:
new_x, new_y = x + dx, y + dy
block_pos_x, block_pos_y = x + block_x, y + block_y
if 0 <= new_x < BOARD_WIDTH and 0 <= new_y < BOARD_HEIGHT:
if not self.board[block_pos_y][block_pos_x]: # Check if not blocked
if not self.board[new_y][new_x] or self.board[new_y][new_x].color != piece.color:
moves.append((new_x, new_y))
elif piece.type == CHARIOT:
# Chariot moves horizontally or vertically
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
for dx, dy in directions:
for i in range(1, max(BOARD_WIDTH, BOARD_HEIGHT)):
new_x, new_y = x + dx * i, y + dy * i
if 0 <= new_x < BOARD_WIDTH and 0 <= new_y < BOARD_HEIGHT:
if not self.board[new_y][new_x]:
moves.append((new_x, new_y))
elif self.board[new_y][new_x].color != piece.color:
moves.append((new_x, new_y))
break
else:
break
else:
break
elif piece.type == CANNON:
# Cannon moves like chariot but captures by jumping over one piece
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
for dx, dy in directions:
jumped = False
for i in range(1, max(BOARD_WIDTH, BOARD_HEIGHT)):
new_x, new_y = x + dx * i, y + dy * i
if 0 <= new_x < BOARD_WIDTH and 0 <= new_y < BOARD_HEIGHT:
if not jumped:
if not self.board[new_y][new_x]:
moves.append((new_x, new_y))
else:
jumped = True
else:
if self.board[new_y][new_x] and self.board[new_y][new_x].color != piece.color:
moves.append((new_x, new_y))
break
else:
break
elif piece.type == SOLDIER:
# Soldier moves forward, and sideways after crossing river
if piece.color == 'red':
if y > 0:
moves.append((x, y - 1))
if y < 5: # Crossed river
if x > 0:
moves.append((x - 1, y))
if x < BOARD_WIDTH - 1:
moves.append((x + 1, y))
else:
if y < BOARD_HEIGHT - 1:
moves.append((x, y + 1))
if y > 4: # Crossed river
if x > 0:
moves.append((x - 1, y))
if x < BOARD_WIDTH - 1:
moves.append((x + 1, y))
# Filter out invalid moves
moves = [(nx, ny) for nx, ny in moves
if 0 <= nx < BOARD_WIDTH and 0 <= ny < BOARD_HEIGHT
and (not self.board[ny][nx] or self.board[ny][nx].color != piece.color)]
return moves
def draw_board(self):
self.screen.fill(BOARD_COLOR)
# Draw grid lines
for i in range(BOARD_HEIGHT):
pygame.draw.line(self.screen, LINE_COLOR,
(MARGIN, MARGIN + i * CELL_SIZE),
(MARGIN + (BOARD_WIDTH - 1) * CELL_SIZE, MARGIN + i * CELL_SIZE), 2)
for i in range(BOARD_WIDTH):
# Don't draw vertical lines through the river
pygame.draw.line(self.screen, LINE_COLOR,
(MARGIN + i * CELL_SIZE, MARGIN),
(MARGIN + i * CELL_SIZE, MARGIN + 4 * CELL_SIZE), 2)
pygame.draw.line(self.screen, LINE_COLOR,
(MARGIN + i * CELL_SIZE, MARGIN + 5 * CELL_SIZE),
(MARGIN + i * CELL_SIZE, MARGIN + 9 * CELL_SIZE), 2)
# Draw palace diagonals
pygame.draw.line(self.screen, LINE_COLOR,
(MARGIN + 3 * CELL_SIZE, MARGIN),
(MARGIN + 5 * CELL_SIZE, MARGIN + 2 * CELL_SIZE), 2)
pygame.draw.line(self.screen, LINE_COLOR,
(MARGIN + 5 * CELL_SIZE, MARGIN),
(MARGIN + 3 * CELL_SIZE, MARGIN + 2 * CELL_SIZE), 2)
pygame.draw.line(self.screen, LINE_COLOR,
(MARGIN + 3 * CELL_SIZE, MARGIN + 7 * CELL_SIZE),
(MARGIN + 5 * CELL_SIZE, MARGIN + 9 * CELL_SIZE), 2)
pygame.draw.line(self.screen, LINE_COLOR,
(MARGIN + 5 * CELL_SIZE, MARGIN + 7 * CELL_SIZE),
(MARGIN + 3 * CELL_SIZE, MARGIN + 9 * CELL_SIZE), 2)
# Draw river
river_text = self.font.render("楚 河 漢 界", True, LINE_COLOR)
text_rect = river_text.get_rect(center=(WINDOW_WIDTH // 2, MARGIN + 4.5 * CELL_SIZE))
self.screen.blit(river_text, text_rect)
def draw_pieces(self):
for y in range(BOARD_HEIGHT):
for x in range(BOARD_WIDTH):
piece = self.board[y][x]
if piece:
center_x = MARGIN + x * CELL_SIZE
center_y = MARGIN + y * CELL_SIZE
# Draw piece background
pygame.draw.circle(self.screen, (255, 255, 255), (center_x, center_y), 25)
pygame.draw.circle(self.screen, RED_COLOR if piece.color == 'red' else BLACK_COLOR,
(center_x, center_y), 25, 3)
# Draw piece character
text = self.font.render(piece.get_chinese_char(), True,
RED_COLOR if piece.color == 'red' else BLACK_COLOR)
text_rect = text.get_rect(center=(center_x, center_y))
self.screen.blit(text, text_rect)
# Highlight selected piece
if self.selected_piece:
x, y =