Untitled
unknown
plain_text
a year ago
4.8 kB
4
Indexable
import pygame
import random
import sys
# Initialize pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 800
BACKGROUND_COLOR = (255, 255, 255)
SQUARE_SIZE = 80
BOARD_COLOR = (0, 0, 0)
PIECE_RADIUS = 25
SAFE_COLOR = (173, 216, 230)
TRACK_COLORS = [(255, 0, 0), (0, 0, 255), (0, 255, 0), (255, 255, 0)] # Red, Blue, Green, Yellow
# Set up the game window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Ludo Game")
# Load images for dice (optional for dice images)
dice_images = [pygame.Surface((60, 60)) for _ in range(6)]
for i in range(6):
dice_images[i].fill((i + 1) * 40) # Just an example, you can replace this with actual dice images
# Player class
class Player:
def _init_(self, color, start_pos):
self.color = color
self.pieces = [start_pos] * 4 # All pieces start at position 0
self.home_positions = [0, 0, 0, 0]
self.in_game = [False, False, False, False]
self.finished = 0
def roll_dice(self):
return random.randint(1, 6)
def move_piece(self, piece_index, steps):
if not self.in_game[piece_index] and steps == 6: # Move piece from home
self.pieces[piece_index] = steps
self.in_game[piece_index] = True
else:
self.pieces[piece_index] += steps
def reset_piece(self, piece_index):
self.pieces[piece_index] = 0
self.in_game[piece_index] = False
# Game class
class LudoGame:
def _init_(self):
self.players = [Player((255, 0, 0), 0), Player((0, 0, 255), 0), Player((0, 255, 0), 0), Player((255, 255, 0), 0)]
self.current_turn = 0
self.dice_roll = 0
self.font = pygame.font.SysFont("Arial", 30)
self.board = self.create_board()
def create_board(self):
board = []
for i in range(15):
row = []
for j in range(15):
if (i in [0, 14] or j in [0, 14]) or (7 <= i <= 8 and 7 <= j <= 8):
row.append("safe")
else:
row.append("track")
board.append(row)
return board
def draw_board(self):
screen.fill(BACKGROUND_COLOR)
# Draw the grid
for row in range(15):
for col in range(15):
color = BOARD_COLOR if self.board[row][col] == "track" else SAFE_COLOR
pygame.draw.rect(screen, color, (col * SQUARE_SIZE, row * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE), 2)
# Draw the players' pieces
for i, player in enumerate(self.players):
for j, piece_pos in enumerate(player.pieces):
if player.in_game[j]:
pygame.draw.circle(screen, player.color,
(piece_pos % 10 * SQUARE_SIZE + SQUARE_SIZE // 2, piece_pos // 10 * SQUARE_SIZE + SQUARE_SIZE // 2), PIECE_RADIUS)
def draw_dice(self):
dice_image = dice_images[self.dice_roll - 1] if self.dice_roll else dice_images[0]
screen.blit(dice_image, (WIDTH - 100, HEIGHT - 100))
def check_for_hit(self, player, piece_index):
for i, other_player in enumerate(self.players):
if i == self.current_turn:
continue
if player.pieces[piece_index] in other_player.pieces:
# If there's a hit, reset the piece
hit_piece_index = other_player.pieces.index(player.pieces[piece_index])
other_player.reset_piece(hit_piece_index)
def handle_turn(self):
player = self.players[self.current_turn]
self.dice_roll = player.roll_dice()
print(f"Player {self.current_turn + 1} rolled a {self.dice_roll}")
# Ask player which piece to move (simple prompt for testing purposes)
# You would replace this with a GUI-based piece selection logic
piece_index = random.choice([i for i in range(4) if player.in_game[i]])
player.move_piece(piece_index, self.dice_roll)
self.check_for_hit(player, piece_index)
def game_over(self):
for player in self.players:
if all(piece >= 100 for piece in player.pieces):
print(f"Player {self.players.index(player) + 1} wins!")
return True
return False
def play_game(self):
while not self.game_over():
self.draw_board()
self.draw_dice()
pygame.display.update()
self.handle_turn()
self.current_turn = (self.current_turn + 1) % 4 # Move to next player
pygame.time.delay(1000)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if _name_ == "_main_":
game = LudoGame()
game.play_game()Editor is loading...
Leave a Comment