0% found this document useful (0 votes)
7 views

ludo.py

This document describes a custom Ludo game implemented using Pygame. It includes game setup, player management, board drawing, dice rolling, and handling special tiles such as bonus and trap tiles. The game runs in a loop until a player wins by completing the board.

Uploaded by

Tyr Jardani
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

ludo.py

This document describes a custom Ludo game implemented using Pygame. It includes game setup, player management, board drawing, dice rolling, and handling special tiles such as bonus and trap tiles. The game runs in a loop until a player wins by completing the board.

Uploaded by

Tyr Jardani
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pygame

import sys
import random

# Initialize Pygame
pygame.init()

# Screen dimensions
WIDTH, HEIGHT = 600, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Custom Ludo Game")

# Colors (Kenyan Flag Theme)


BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

# Fonts
font = pygame.font.Font(None, 36)

# Dice configuration
dice_faces = [
pygame.image.load(f"dice_{i}.png") for i in range(1, 7)
]
dice_faces = [pygame.transform.scale(face, (50, 50)) for face in dice_faces]

# Players
players = ["Red", "Green", "Black", "White"]
colors = [RED, GREEN, BLACK, WHITE]
positions = [0, 0, 0, 0] # Starting positions
current_player = 0
scores = [0, 0, 0, 0] # Track player scores

# Board setup
TILE_SIZE = 50
NUM_TILES = 28 # Simplified board with 28 tiles
SAFE_ZONES = [4, 10, 16, 22] # Safe zone tiles
BONUS_TILES = [6, 14, 20] # Bonus tiles
TRAP_TILES = [8, 18, 24] # Trap tiles

# Draw the board


def draw_board():
screen.fill(WHITE)

# Draw tiles
for i in range(NUM_TILES):
x = (i % 7) * TILE_SIZE + 100
y = (i // 7) * TILE_SIZE + 100

color = BLACK if i in SAFE_ZONES else WHITE


if i in BONUS_TILES:
color = GREEN
elif i in TRAP_TILES:
color = RED

pygame.draw.rect(screen, color, (x, y, TILE_SIZE, TILE_SIZE))


pygame.draw.rect(screen, BLACK, (x, y, TILE_SIZE, TILE_SIZE), 2)
# Draw player tokens
for idx, pos in enumerate(positions):
x = (pos % 7) * TILE_SIZE + 100 + TILE_SIZE // 4
y = (pos // 7) * TILE_SIZE + 100 + TILE_SIZE // 4
pygame.draw.circle(screen, colors[idx], (x + TILE_SIZE // 2, y +
TILE_SIZE // 2), 15)

# Roll the dice


def roll_dice():
return random.randint(1, 6)

# Handle special tiles


def handle_special_tiles(player):
if positions[player] in BONUS_TILES:
print(f"{players[player]} hit a BONUS tile! Extra roll!")
return True # Grant an extra turn
elif positions[player] in TRAP_TILES:
print(f"{players[player]} hit a TRAP tile! Back to start!")
positions[player] = 0
return False

# Check for winning condition


def check_winner():
for idx, score in enumerate(scores):
if score >= 1: # Win condition: One token completes the board
return idx
return -1

# Main game loop


running = True
dice_value = 1
extra_turn = False
while running:
draw_board()

# Display dice
screen.blit(dice_faces[dice_value - 1], (WIDTH - 100, HEIGHT // 2))

# Display current player


text = font.render(f"{players[current_player]}'s Turn", True, BLACK)
screen.blit(text, (10, 10))

# Display scores
for i, score in enumerate(scores):
score_text = font.render(f"{players[i]}: {score}", True, colors[i])
screen.blit(score_text, (10, 50 + i * 30))

pygame.display.flip()

for event in pygame.event.get():


if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
dice_value = roll_dice()
positions[current_player] = (positions[current_player] +
dice_value) % NUM_TILES

# Check if player lands on a special tile


extra_turn = handle_special_tiles(current_player)

# Check if the player completes the board


if positions[current_player] == 0 and dice_value == 6:
scores[current_player] += 1

# Check for winner


winner = check_winner()
if winner != -1:
print(f"{players[winner]} wins the game!")
running = False

# Switch to next player unless extra turn


if not extra_turn:
current_player = (current_player + 1) % len(players)

pygame.quit()
sys.exit()

You might also like