Untitled

 avatar
unknown
python
a year ago
3.3 kB
14
Indexable
import pygame
import random

# Initialize Pygame
pygame.init()

# Set the screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# Define player colors
PLAYER_COLORS = [GREEN, RED, BLUE]

# Set the title of the window
pygame.display.set_caption("Snake and Ladder")

# Load sound effects
pygame.mixer.init()
dice_sound = pygame.mixer.Sound("dice_roll.wav")

# Font for displaying text
font = pygame.font.SysFont(None, 36)

# Function to draw text on the screen
def draw_text(text, color, x, y):
    text_surface = font.render(text, True, color)
    screen.blit(text_surface, (x, y))

# Function to roll the dice
def roll_dice():
    dice_sound.play()
    return random.randint(1, 6)

# Function to draw the board
def draw_board():
    screen.fill(WHITE)
    # Draw the board grid
    for i in range(10):
        pygame.draw.line(screen, BLACK, (50, 50 + i * 50), (750, 50 + i * 50), 2)
        pygame.draw.line(screen, BLACK, (50 + i * 70, 50), (50 + i * 70, 550), 2)
    # Draw snakes and ladders
    for start, end in snakes.items():
        pygame.draw.line(screen, RED, get_position(start), get_position(end), 5)
    for start, end in ladders.items():
        pygame.draw.line(screen, GREEN, get_position(start), get_position(end), 5)

# Function to get the screen coordinates of a board position
def get_position(position):
    row = 9 - (position - 1) // 10
    col = (position - 1) % 10
    return 50 + col * 70, 50 + row * 50

# Function to move player
def move_player(player):
    dice_roll = roll_dice()
    player_positions[player] += dice_roll
    if player_positions[player] in snakes:
        player_positions[player] = snakes[player_positions[player]]
    elif player_positions[player] in ladders:
        player_positions[player] = ladders[player_positions[player]]
    if player_positions[player] >= 100:
        return True
    return False

# Function to draw players
def draw_players():
    for i, player in enumerate(player_positions):
        pygame.draw.circle(screen, PLAYER_COLORS[i], get_position(player_positions[player]), 20)

# Function to display game over screen
def game_over(winner):
    screen.fill(WHITE)
    draw_text(f"{winner} wins!", BLACK, SCREEN_WIDTH // 2 - 100, SCREEN_HEIGHT // 2 - 50)
    pygame.display.update()
    pygame.time.delay(3000)

# Set up the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# Define snakes and ladders
snakes = {16: 6, 47: 26, 49: 11, 56: 53, 62: 19, 64: 60, 87: 24, 93: 73, 95: 75, 98: 78}
ladders = {1: 38, 4: 14, 9: 31, 21: 42, 28: 84, 36: 44, 51: 67, 71: 91, 80: 100}

# Initialize player positions
player_positions = {f"Player {i+1}": 1 for i in range(3)}

# Main game loop
running = True
winner = None

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill(WHITE)
    draw_board()
    draw_players()

    for player in player_positions:
        if move_player(player):
            winner = player
            running = False
            break

    if winner:
        game_over(winner)
        break

    pygame.display.flip()
    pygame.time.delay(500)

pygame.quit()
Editor is loading...
Leave a Comment