




Here’s a complete Ludo game implementation in Python using Pygame. This is a simplified version of Ludo that includes basic movement, dice rolling, and win conditions.
Requirements
Ensure you have Pygame installed. If not, install it using:pip install pygame
Complete Ludo Game Code in Python
import pygame import random # Initialize Pygame pygame.init() # Constants WIDTH, HEIGHT = 600, 600 BOARD_COLOR = (255, 255, 255) DICE_COLOR = (0, 0, 0) TEXT_COLOR = (0, 0, 0) PLAYER_COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0)] PLAYER_START_POSITIONS = [(50, 50), (500, 50), (50, 500), (500, 500)] WIN_POS = (300, 300) CELL_SIZE = 50 # Initialize screen screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Ludo Game") # Fonts font = pygame.font.Font(None, 36) # Dice rolling function def roll_dice(): return random.randint(1, 6) # Draw the board def draw_board(): screen.fill(BOARD_COLOR) # Draw player homes for i, color in enumerate(PLAYER_COLORS): pygame.draw.rect(screen, color, (PLAYER_START_POSITIONS[i][0], PLAYER_START_POSITIONS[i][1], CELL_SIZE, CELL_SIZE)) # Draw win position pygame.draw.circle(screen, (0, 0, 0), WIN_POS, 20) # Draw player pieces def draw_players(players): for player in players: pygame.draw.circle(screen, player["color"], player["pos"], 20) # Main function def main(): running = True players = [{"color": PLAYER_COLORS[i], "pos": PLAYER_START_POSITIONS[i]} for i in range(4)] current_player = 0 dice_roll = 1 while running: screen.fill(BOARD_COLOR) draw_board() draw_players(players) # Display dice roll dice_text = font.render(f"Dice: {dice_roll}", True, TEXT_COLOR) screen.blit(dice_text, (250, 550)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: dice_roll = roll_dice() # Move player based on dice roll x, y = players[current_player]["pos"] x += dice_roll * CELL_SIZE if x > WIDTH - CELL_SIZE: x = WIDTH - CELL_SIZE players[current_player]["pos"] = (x, y) # Check win condition if players[current_player]["pos"] == WIN_POS: print(f"Player {current_player + 1} Wins!") running = False # Next player turn current_player = (current_player + 1) % 4 pygame.time.delay(300) pygame.quit() # Run the game if __name__ == "__main__": main()
How It Works
- Players & Board Setup
- Four players with distinct colors start at their respective corners.
- The board is displayed with a win position in the center.
- Game Mechanics
- Players take turns rolling the dice (press SPACEBAR to roll).
- The current player moves forward based on the dice roll.
- The first player to reach the win position wins.
- Controls
- Press SPACEBAR to roll the dice.
- The game ends when one player reaches the win position.
Possible Enhancements
- Implement full board movement logic with proper Ludo paths.
- Add multiplayer turn-based logic.
- Include piece capturing rules.
- Enhance UI with better graphics.
Let me know if you need modifications or improvements!
No responses yet