LUDO GAME BY PYTHON(" tkinter,pil MODULE"):
![]() |
Ludo Game in Python |
DOWNLOAD SOURCE CODE AND MEDIA FILE
CODE DESCRIPTION:
1. Imports: The code begins with importing necessary modules, including Tkinter, PIL (Pillow), time, and radiant.
2. Ludo Class: The Ludo class is the main class for managing the Ludo board. It takes several parameters during initialization, including the Tkinter root window and various block images representing different sides of a Ludo die.
3. Canvas: The make_canvas attribute is a Tkinter canvas widget used to draw and display the Ludo board.
4. Data Containers: Several lists (made_red_coin, made_green_coin, etc.) are used to store information about coins and labels associated with them.
5. Block Images: The block_number_side list stores images representing different sides of a Ludo die.
6. Coin Positions: Lists such as red_coin_position, green_coin_position, etc., store the positions of coins on the board.
7. Move Counters: Variables like move_red_counter, move_green_counter, etc., keep track of the number of moves made by each player.
8. Control and Flags: Various control variables and flags like take_permission, six_with_overlap, red_store_active, etc., are used to control the game's flow.
9. Board Setup: The board_set_up method is responsible for drawing the initial Ludo board layout using rectangles and colors. It sets up the game's initial visual appearance.
10. Instruction Buttons: There are methods like instruction_btn_red, instruction_btn_sky_blue, etc., which are likely responsible for creating instruction buttons or elements related to specific colors.
11.take_initial_control: This method appears to be related to initializing the game control and flow.
12. Commented Code: The code block at the end seems to be a part of the board_set_up method but is currently commented out.
13. Separation Lines: This section creates separation lines on the left and right sides of the board, as well as on the top and bottom. These lines divide the board into different regions. These regions are typically used for moving the game pieces.
14. Square Boxes (Coin Containers): This part defines four square boxes inside the white region. These boxes serve as containers for coins of different colors (red, green, yellow, sky blue).
15. Inside Square Boxes: Within each of the four square boxes, smaller square boxes are created. These represent the starting positions for each color of the player's pieces (coins). For example, the red starting positions are filled with red-colored squares.
16. Start Positions: This section defines the starting positions for each color: red, green, yellow, and sky blue. These are the locations where the player's coins begin the game.
17. Triangles in the Middle: Four triangles are created in the middle of the board, each with a different color (red, yellow, green, and sky blue). These triangles are typically used for marking the center region where pieces can be safe from capture.
18. Coins: Four red coins (game pieces) are created and placed in the left-up square box. These are represented as red circles.
19. Red Coins and Labels:
- Four red coins (oval shapes) are created and placed in the left-up square block.
- Four labels (with numbers 1, 2, 3, and 4) are created and placed under the red coins.
- The labels are configured with a specific font, background color (red), and text color (black).
- Both the red coins and labels are added to the self.made_red_coin and self.red_number_label lists for later reference.
20. Green Coins and Labels:
- Four green coins (oval shapes) are created and placed in the right-up square block.
- Four labels (with numbers 1, 2, 3, and 4) are created and placed under the green coins.
- Similar to the red labels, these labels have a specific font, background color (#00FF00, green), and text color (black).
- Both the green coins and labels are added to the self.made_green_coin and self.green_number_label lists.
21. Sky Blue Coins and Labels:
- Four sky blue coins (oval shapes) are created and placed in the left-down square block.
- Four labels (with numbers 1, 2, 3, and 4) are created and placed under the sky-blue coins.
- The labels have a font, background color (#04d9ff, sky blue), and text color (black).
- Both the sky blue coins and labels are added to the self.made_sky_blue_coin and self.sky_blue_number_label lists.
22. Yellow Coins and Labels:
- Four yellow coins (oval shapes) are created and placed in the right-down square block.
- Four labels (with numbers 1, 2, 3, and 4) are created and placed under the yellow coins.
- These labels have a font, background color (yellow), and text color (black).
- Both the yellow coins and labels are added to the self.made_yellow_coin and self.yellow_number_label lists.
23. Creating the Star Safe Zone:
- The code creates a star-shaped safe zone on the Ludo board using polygons. This safe zone has four arms extending from the center, forming a star.
- The star is created separately for each of the four colored player blocks: right (blue), up (blue), left (blue), and down (blue).
24.take_initial_control Method:
- This method is used to take initial control of the game. It opens a window where the total number of players is entered.
- The player count is filtered to ensure it is between 2 and 4 players.
- Once the player count is validated, the game initialization continues.
25.make_prediction Method:
- This method is used to predict the next move for a player of a specific color (red, sky blue, yellow, or green) based on probability.
- It simulates the prediction process with an animation of the numbers 1 to 6 cycling before revealing the predicted number.
- The predicted number is displayed as an image on the game board.
- Depending on the predicted number and game rules, further actions are determined.
26.instructional_btn_customization_based_on_current_situation Method:
- This method customizes the instructional buttons based on the current game situation.
- It checks if a player can make a move, considering factors such as whether all of their coins are in the safe zone, if they rolled a 6, or if they have any overlap.
- It enables or disables the "Predict" and "Give" buttons accordingly.
27.make_command Method:
- This method manages the player turn sequence by enabling and disabling player controls.
- It updates the turn order based on the number of players.
28. Methods for Configuring Player Controls:
- There are methods like instruction_btn_red, instruction_btn_sky_blue, and instruction_btn_yellow for configuring controls for specific players (red, sky blue, and yellow).
- These methods create labels, buttons for prediction, entry fields for taking actions, and buttons for making final moves.
- The buttons are initially disabled until the player's turn comes up.
29.instruction_btn_green Method:
- This method is similar to the previous methods for configuring controls for specific players (in this case, green).
- It creates labels, buttons for prediction, entry fields for taking actions, and buttons for making final moves.
- The buttons are initially disabled until the player's turn comes up.
30.store_instructional_btn Method:
- This method is used to store information about the instructional buttons for each player.
- It creates a list temp that holds references to the block indicator, predictor button, entry controller, and final move button.
- The temp list is appended to the block_value_predict list to keep track of the instructional elements for all players.
31.red_circle_start_position Method:
- This method updates the position of a red player's coin when it starts from the home position.
- It deletes the previous coin image and creates a new oval representing the coin in the appropriate starting position.
- The position of the coin is updated in the red_coin_position list.
- A label displaying the coin number is also updated to show the number inside the coin.
32.green_circle_start_position Method:
- This method is similar to red_circle_start_position but for green player coins.
- It updates the position of a green player's coin when it starts from the home position.
33.yellow_circle_start_position Method:
- This method updates the position of a yellow player's coin when it starts from the home position.
- It deletes the previous coin image and creates a new oval representing the coin in the appropriate starting position.
- The position of the coin is updated in the yellow_coin_position list.
- A label displaying the coin number is also updated to show the number inside the coin.
34.sky_blue_circle_start_position Method:
- This method is similar to yellow_circle_start_position but for sky blue player coins.
- It updates the position of a sky-blue player's coin when it starts from the home position.
35.main_controller Method:
- This method serves as the main control for player actions in the game based on their color and selected coin number.
- It begins with input filtering to ensure the provided coin number is valid (between 1 and 4).
- Depending on the player's color (red, green, yellow, sky blue), it takes appropriate actions:
- If it's a red player's turn, it handles their moves, updates positions, and checks for conditions like reaching the destination.
- Similar logic is applied for green, yellow, and sky blue players.
- The coord_overlap function appears to manage overlapping coins on the board.
- It prints the current positions of coins for each player.
- It checks for game termination conditions and invokes check_winner_and_runner to determine if a player has won.
- If the game is not over, it allows the next player to make a move.
- Finally, it calls make_command which seems to be part of the broader game control logic.
36.motion_of_coin: This function manages the movement of a game coin. It updates the position of the coin on the game board based on certain conditions and parameters.
37.coord_overlap: This function handles the case when two coins end up in the same position. It removes the previous coin from its position and places it back in its respective room.
38.under_room_traversal_control: This function controls the movement of a coin when it enters a room.
39.room_red_traversal, room_green_traversal, room_yellow_traversal, room_sky_blue_traversal: These functions handle the movement of coins within specific rooms (red, green, yellow, sky blue).
40.check_winner_and_runner: This function checks if a player has reached the destination and if they are the winner or a runner-up.
41.input_filtering: This function is used for input validation.
SOURCE CODE:
from tkinter import messagebox
import PIL
from PIL import Image,ImageTk
import time
from random import randint
class Ludo:
def __init__(self, root,six_side_block,five_side_block,four_side_block,
self.window = root
# Make canvas
self.make_canvas = Canvas(self.window, bg="#4d4dff", width=800, height=630)
self.make_canvas.pack(fill=BOTH,expand=1)
# Make some containers to store data
self.made_red_coin = []
self.made_green_coin = []
self.made_yellow_coin = []
self.made_sky_blue_coin = []
self.red_number_label = []
self.green_number_label = []
self.yellow_number_label = []
self.sky_blue_number_label = []
self.block_value_predict = []
self.total_people_play = []
# Ludo block all side image store
self.block_number_side = [one_side_block, two_side_block, three_side_block,
# Use for store specific position of all coins
self.red_coord_store = [-1, -1, -1, -1]
self.green_coord_store = [-1, -1, -1, -1]
self.yellow_coord_store = [-1, -1, -1, -1]
self.sky_blue_coord_store = [-1, -1, -1, -1]
self.red_coin_position = [0, 1, 2, 3]
self.green_coin_position = [0, 1, 2, 3]
self.yellow_coin_position = [0, 1, 2, 3]
self.sky_blue_coin_position = [0, 1, 2, 3]
for index in range(len(self.red_coin_position)):# Specific coin position set to
self.red_coin_position[index] = -1
self.green_coin_position[index] = -1
self.yellow_coin_position[index] = -1
self.sky_blue_coin_position[index] = -1
# Number to room to be traverse by specific color coin, store in that variable
self.move_red_counter = 0
self.move_green_counter = 0
self.move_yellow_counter = 0
self.move_sky_blue_counter = 0
self.take_permission = 0
self.six_with_overlap = 0
self.red_store_active = 0
self.sky_blue_store_active = 0
self.yellow_store_active = 0
self.green_store_active = 0
self.six_counter = 0
self.time_for = -1
# Some variables initializes with None
self.right_star = None
self.down_star = None
self.left_star = None
self.up_star = None
# By default some function call
self.board_set_up()
self.instruction_btn_red()
self.instruction_btn_sky_blue()
self.instruction_btn_yellow()
self.instruction_btn_green()
self.take_initial_control()
def board_set_up(self):
# Cover Box made
self.make_canvas.create_rectangle(100, 15, 100 + (40 * 15), 15 + (40 * 15),
# Square box
self.make_canvas.create_rectangle(100, 15, 100+240, 15+240, width=3, fill="red")
self.make_canvas.create_rectangle(100, (15+240)+(40*3), 100+240, (15+240)+(40*3)+
self.make_canvas.create_rectangle(340+(40*3), 15, 340+(40*3)+(40*6), 15+240,
self.make_canvas.create_rectangle(340+(40*3), (15+240)+(40*3), 340+(40*3)+
# Left 3 box(In white region)
self.make_canvas.create_rectangle(100, (15+240), 100+240, (15+240)+40, width=3)
self.make_canvas.create_rectangle(100+40, (15 + 240)+40, 100 + 240, (15 + 240)
self.make_canvas.create_rectangle(100, (15 + 240)+80, 100 + 240, (15 + 240)
# right 3 box(In white region)
self.make_canvas.create_rectangle(100+240, 15, 100 + 240+40, 15 + (40*6), width=3)
self.make_canvas.create_rectangle(100+240+40, 15+40, 100+240+80, 15 + (40*6),
self.make_canvas.create_rectangle(100+240+80, 15, 100 + 240+80+40, 15 + (40*6),
# up 3 box(In white region)
self.make_canvas.create_rectangle(340+(40*3), 15+240, 340+(40*3)+(40*6),
self.make_canvas.create_rectangle(340+(40*3), 15+240+40, 340+(40*3)+(40*6)-40,
self.make_canvas.create_rectangle(340+(40*3), 15+240+80, 340+(40*3)+(40*6),
# down 3 box(In white region)
self.make_canvas.create_rectangle(100, (15 + 240)+(40*3), 100 + 240+40,
self.make_canvas.create_rectangle(100+240+40, (15 + 240)+(40*3),
self.make_canvas.create_rectangle(100 + 240+40+40, (15 + 240)+(40*3),
# All left separation line
start_x = 100 + 40
start_y = 15 + 240
end_x = 100 + 40
end_y = 15 + 240 + (40 * 3)
for _ in range(5):
self.make_canvas.create_line(start_x, start_y, end_x, end_y, width=3)
start_x+=40
end_x+= 40
# All right separation line
start_x = 100+240+(40*3)+40
start_y = 15 + 240
end_x = 100+240+(40*3)+40
end_y = 15 + 240 + (40 * 3)
for _ in range(5):
self.make_canvas.create_line(start_x, start_y, end_x, end_y, width=3)
start_x += 40
end_x += 40
# All up separation done
start_x = 100+240
start_y = 15+40
end_x = 100+240+(40*3)
end_y = 15+40
for _ in range(5):
self.make_canvas.create_line(start_x, start_y, end_x, end_y, width=3)
start_y += 40
end_y += 40
# All down separation done
start_x = 100 + 240
start_y = 15 + (40*6)+(40*3)+40
end_x = 100 + 240 + (40 * 3)
end_y = 15 + (40*6)+(40*3)+40
for _ in range(5):
self.make_canvas.create_line(start_x, start_y, end_x, end_y, width=3)
start_y += 40
end_y += 40
# Square box(Coins containers) white region make
self.make_canvas.create_rectangle(100+20, 15+40-20, 100 + 40 + 60 + 40 +60+20,
self.make_canvas.create_rectangle(340+(40*3)+40 - 20, 15 + 40-20,
self.make_canvas.create_rectangle(100+20, 340+80-20+15, 100 + 40 + 60 + 40
self.make_canvas.create_rectangle(340+(40*3)+40 - 20, 340 + 80 - 20+15, 340+
# Left up square inside box made
self.make_canvas.create_rectangle(100+40, 15+40, 100+40+40, 15+40+40,
self.make_canvas.create_rectangle(100+40+60+60, 15 + 40, 100+40+60+40+60,
self.make_canvas.create_rectangle(100 + 40, 15 + 40+100, 100 + 40 + 40, 15
self.make_canvas.create_rectangle(100 + 40 + 60 + 60, 15 + 40+100, 100 +
# Right up square inside box made
self.make_canvas.create_rectangle(340+(40*3)+40, 15 + 40, 340+(40*3)+40 +
self.make_canvas.create_rectangle(340+(40*3)+40+ 60 + 40+20, 15 + 40,
self.make_canvas.create_rectangle(340+(40*3)+40, 15 + 40 + 100, 340+(40*3)+
self.make_canvas.create_rectangle(340+(40*3)+40+ 60 + 40+20, 15 + 40 + 100,
# Left down square inside box made
self.make_canvas.create_rectangle(100 + 40, 340+80+15, 100 + 40 + 40,
self.make_canvas.create_rectangle(100 + 40 + 60 + 40+20, 340+80+15, 100 +
self.make_canvas.create_rectangle(100 + 40, 340+80+60+40+15, 100 + 40 + 40,
self.make_canvas.create_rectangle(100 + 40 + 60 + 40+20, 340+80+60+40+15,
# Right down square inside box made
self.make_canvas.create_rectangle(340 + (40 * 3) + 40, 340+80+15, 340 + (40 * 3) +
self.make_canvas.create_rectangle(340 + (40 * 3) + 40 + 60 + 40+20, 340+80+15,
self.make_canvas.create_rectangle(340 + (40 * 3) + 40, 340+80+60+40+15, 340 +
self.make_canvas.create_rectangle(340 + (40 * 3) + 40 + 60 + 40+20, 340+80+60+40+15
# sky_blue start position
self.make_canvas.create_rectangle(100+240,340+(40*5)-5,100+240+40,340+(40*6)-5,
# Red start position
self.make_canvas.create_rectangle(100 + 40, 15+(40*6), 100 +40 + 40, 15+(40*6)+40,
# Green start position
self.make_canvas.create_rectangle(100 + (40*8), 15 + 40, 100 +(40*9), 15 + 40+ 40,
# Yellow start position
self.make_canvas.create_rectangle(100 + (40 * 6)+(40*3)+(40*4), 15 + (40*8), 100 +
# Traingle in middle
self.make_canvas.create_polygon(100+240, 15+240, 100+240+60, 15+240+60, 100+240,
self.make_canvas.create_polygon(100 + 240+(40*3), 15 + 240, 100 + 240 + 60, 15 + 240
self.make_canvas.create_polygon(100 + 240, 15 + 240+(40*3), 100 + 240 + 60, 15 + 240
# Make coin for red left up block
red_1_coin = self.make_canvas.create_oval(100+40, 15+40, 100+40+40, 15+40+40,
red_2_coin = self.make_canvas.create_oval(100+40+60+60, 15 + 40, 100+40+60+60+40,
red_3_coin = self.make_canvas.create_oval(100 + 40 + 60 + 60, 15 + 40 + 100, 100 +
red_4_coin = self.make_canvas.create_oval(100 + 40, 15 + 40+100, 100 + 40 + 40, 15 +
self.made_red_coin.append(red_1_coin)
self.made_red_coin.append(red_2_coin)
self.made_red_coin.append(red_3_coin)
self.made_red_coin.append(red_4_coin)
# Make coin under number label for red left up block
red_1_label = Label(self.make_canvas, text="1", font=("Arial", 15, "bold"),
red_1_label.place(x=100 + 40 + 10, y=15 + 40 + 5)
red_2_label = Label(self.make_canvas, text="2", font=("Arial", 15, "bold"),
red_2_label.place(x=100 + 40 + 60 + 60 + 10, y=15 + 40 + 5)
red_3_label = Label(self.make_canvas, text="3", font=("Arial", 15, "bold"),
red_3_label.place(x=100 + 40 + 60 + 60 + 10, y=15 + 40 + 100 + 5)
red_4_label = Label(self.make_canvas, text="4", font=("Arial", 15, "bold"),
red_4_label.place(x=100 + 40 + 10, y=15 + 40 + 100 + 5)
self.red_number_label.append(red_1_label)
self.red_number_label.append(red_2_label)
self.red_number_label.append(red_3_label)
self.red_number_label.append(red_4_label)
# Make coin for green right up block
green_1_coin = self.make_canvas.create_oval(340+(40*3)+40, 15 + 40, 340+(40*3)+40 +
green_2_coin = self.make_canvas.create_oval(340+(40*3)+40+ 60 + 40+20, 15 + 40, 340+
green_3_coin = self.make_canvas.create_oval(340 + (40 * 3) + 40 + 60 + 40 + 20, 15 +
green_4_coin = self.make_canvas.create_oval(340+(40*3)+40, 15 + 40 + 100, 340+
self.made_green_coin.append(green_1_coin)
self.made_green_coin.append(green_2_coin)
self.made_green_coin.append(green_3_coin)
self.made_green_coin.append(green_4_coin)
# Make coin under number label for green right up block
green_1_label = Label(self.make_canvas, text="1", font=("Arial", 15, "bold"),
green_1_label.place(x=340 + (40 * 3) + 40 + 10, y=15 + 40 + 5)
green_2_label = Label(self.make_canvas, text="2", font=("Arial", 15, "bold"),
green_2_label.place(x=340 + (40 * 3) + 40 + 40 + 60 + 30, y=15 + 40 + 5)
green_3_label = Label(self.make_canvas, text="3", font=("Arial", 15, "bold"),
green_3_label.place(x=340 + (40 * 3) + 40 + 40 + 60 + 30, y=15 + 40 + 100 + 5)
green_4_label = Label(self.make_canvas, text="4", font=("Arial", 15, "bold"),
green_4_label.place(x=340 + (40 * 3) + 40 + 10, y=15 + 40 + 100 + 5)
self.green_number_label.append(green_1_label)
self.green_number_label.append(green_2_label)
self.green_number_label.append(green_3_label)
self.green_number_label.append(green_4_label)
# Make coin for sky_blue left down block
sky_blue_1_coin = self.make_canvas.create_oval(100 + 40, 340+80+15, 100 + 40 + 40,
sky_blue_2_coin = self.make_canvas.create_oval(100 + 40 + 60 + 40+20, 340+80+15, 100
sky_blue_3_coin = self.make_canvas.create_oval(100 + 40 + 60 + 40 + 20, 340 + 80 +
sky_blue_4_coin = self.make_canvas.create_oval( 100 + 40, 340+80+60+40+15, 100 + 40 +
self.made_sky_blue_coin.append(sky_blue_1_coin)
self.made_sky_blue_coin.append(sky_blue_2_coin)
self.made_sky_blue_coin.append(sky_blue_3_coin)
self.made_sky_blue_coin.append(sky_blue_4_coin)
# Make coin under number label for sky_blue left down block
sky_blue_1_label = Label(self.make_canvas, text="1", font=("Arial", 15, "bold"),
sky_blue_1_label.place(x=100 + 40 + 10, y=30 + (40 * 6) + (40 * 3) + 40 + 10)
sky_blue_2_label = Label(self.make_canvas, text="2", font=("Arial", 15, "bold"),
sky_blue_2_label.place(x=100 + 40 + 60 + 60 + 10, y=30 + (40 * 6) + (40 * 3)+40+10)
sky_blue_3_label = Label(self.make_canvas, text="3", font=("Arial", 15, "bold"),
sky_blue_3_label.place(x=100 + 40 + 60 + 60 + 10, y=30 + (40 * 6) + (40 * 3) +
sky_blue_4_label = Label(self.make_canvas, text="4", font=("Arial", 15, "bold"),
sky_blue_4_label.place(x=100 + 40 + 10, y=30 + (40 * 6) + (40 * 3) + 40+60+40+10)
self.sky_blue_number_label.append(sky_blue_2_label)
self.sky_blue_number_label.append(sky_blue_3_label)
self.sky_blue_number_label.append(sky_blue_4_label)
# Make coin for yellow right down block
yellow_1_coin = self.make_canvas.create_oval(340 + (40 * 3) + 40, 340+80+15, 340 +
yellow_2_coin = self.make_canvas.create_oval(340 + (40 * 3) + 40 + 60 + 40 + 20, 340+
yellow_3_coin = self.make_canvas.create_oval(340 + (40 * 3) + 40 + 60 + 40 + 20, 340
yellow_4_coin = self.make_canvas.create_oval(340 + (40 * 3) + 40, 340+80+60+40+15,
self.made_yellow_coin.append(yellow_1_coin)
self.made_yellow_coin.append(yellow_2_coin)
self.made_yellow_coin.append(yellow_3_coin)
self.made_yellow_coin.append(yellow_4_coin)
# Make coin under number label for yellow right down block
yellow_1_label = Label(self.make_canvas, text="1", font=("Arial", 15, "bold"),
yellow_1_label.place(x=340 + (40 * 3) + 40 + 10, y=30 + (40 * 6) + (40 * 3)+40+10)
yellow_2_label = Label(self.make_canvas, text="2", font=("Arial", 15, "bold"),
yellow_2_label.place(x=340 + (40 * 3) + 40 + 40 + 60 + 30, y=30 + (40 * 6) + (40 * 3) + 40 + 10)
yellow_3_label = Label(self.make_canvas, text="3", font=("Arial", 15, "bold"), bg="yellow", fg="black")
yellow_3_label.place(x=340 + (40 * 3) + 40 + 40 + 60 + 30, y=30 + (40 * 6) + (40 * 3) + 40 + 100 + 10)
yellow_4_label = Label(self.make_canvas, text="4", font=("Arial", 15, "bold"), bg="yellow", fg="black")
yellow_4_label.place(x=340 + (40 * 3) + 40 + 10, y=30 + (40 * 6) + (40 * 3) + 40 + 100 + 10)
self.yellow_number_label.append(yellow_1_label)
self.yellow_number_label.append(yellow_2_label)
self.yellow_number_label.append(yellow_3_label)
self.yellow_number_label.append(yellow_4_label)
# Make star safe zone
"""
A
L * B
K * * * * * * C
* *
J * * D
* *
I* * * * * * E
H * F
G
"""
# Right star
common_x = 340+(40*6)+20
common_y = 15+240+2
# A B C D E F G H I J K L
coord = [common_x,common_y, common_x+5,common_y+15, common_x+15,common_y+15, common_x+8,common_y+20, common_x+15,common_y+25, common_x+5,common_y+25, common_x,common_y+25+10, common_x-5,common_y+25, common_x-16,common_y+25, common_x-8,common_y+15+5, common_x-15,common_y+15, common_x-5,common_y+15]
self.make_canvas.create_polygon(coord,width=3,fill="blue")
# Up star
common_x = 100+240+2+18
common_y = 15 + (40*2) + 2
# A B C D E F G H I J K L
coord = [common_x, common_y, common_x + 5, common_y + 15, common_x + 15, common_y + 15, common_x + 8, common_y + 20, common_x + 15, common_y + 25, common_x + 5, common_y + 25, common_x, common_y + 25 + 10, common_x - 5, common_y + 25, common_x - 16, common_y + 25, common_x - 8,common_y + 15 + 5, common_x - 15,common_y + 15, common_x - 5,common_y + 15]
self.make_canvas.create_polygon(coord, width=3, fill="blue")
# Left star
common_x = 100 + (40*2) + 2 +18
common_y = 15 + 240+(40*2) + 2
# A B C D E F G H I J K L
coord = [common_x, common_y, common_x + 5, common_y + 15, common_x + 15, common_y + 15, common_x + 8,common_y + 20, common_x + 15, common_y + 25, common_x + 5, common_y + 25, common_x, common_y + 25 + 10, common_x - 5, common_y + 25, common_x - 16, common_y + 25, common_x - 8, common_y + 15 + 5, common_x - 15, common_y + 15, common_x - 5, common_y + 15]
self.make_canvas.create_polygon(coord, width=3, fill="blue")
# Down star
common_x = 100 + 240 + (40*2) + 2 + 18
common_y = 15 + (40 * 6) + (40*3)+(40*3)+2
# A B C D E F G H I J K L
coord = [common_x, common_y, common_x + 5, common_y + 15, common_x + 15, common_y + 15, common_x + 8, common_y + 20, common_x + 15, common_y + 25, common_x + 5, common_y + 25, common_x, common_y + 25 + 10, common_x - 5, common_y + 25, common_x - 16, common_y + 25, common_x - 8, common_y + 15 + 5, common_x - 15, common_y + 15, common_x - 5, common_y + 15]
self.make_canvas.create_polygon(coord, width=3, fill="blue")
# Total number of players: Control take at first
def take_initial_control(self):
for i in range(4):
self.block_value_predict[i][1]['state'] = DISABLED
# Make other window to control take
top = Toplevel()
top.geometry("600x150")
top.maxsize(600,150)
top.minsize(600,150)
top.config(bg="orange")
top.iconbitmap(R"C:\Users\madde\PycharmProjects\game\pics\ludo-bg.jpg")
head = Label(top,text="-:Total number of players:- ",font=("Arial",25,"bold","italic"),bg="orange",fg="chocolate")
head.place(x=70,y=30)
take_entry = Entry(top,font=("Arial",18,"bold","italic"),relief=SUNKEN,bd=7,width=12)
take_entry.place(x=150,y=80)
take_entry.focus()
def filtering():# Total player input value filtering
response_take = self.input_filtering(take_entry.get())
if response_take is True and int(take_entry.get())>1:
for player_index in range(int(take_entry.get())):
self.total_people_play.append(player_index)
print(self.total_people_play)
self.make_command()
top.destroy()
else:
messagebox.showerror("Input Error", "Please input number of players between 2 and 4")
submit_btn = Button(top,text="Submit",bg="black",fg="#00FF00",font=("Arial",13,"bold"),relief=RAISED,bd=8,command=filtering)
submit_btn.place(x=350,y=80)
top.mainloop()
# Get block value after prediction based on probability
def make_prediction(self,color_indicator):
try:
if color_indicator == "red":
block_value_predict = self.block_value_predict[0]
permanent_block_number = self.move_red_counter = randint(1, 6)
elif color_indicator == "sky_blue":
block_value_predict = self.block_value_predict[1]
permanent_block_number = self.move_sky_blue_counter = randint(1, 6)
elif color_indicator == "yellow":
block_value_predict = self.block_value_predict[2]
permanent_block_number = self.move_yellow_counter = randint(1, 6)
else:
block_value_predict = self.block_value_predict[3]
permanent_block_number = self.move_green_counter = randint(1, 6)
block_value_predict[1]['state'] = DISABLED
# Illusion of coin floating
temp_counter = 15
while temp_counter>0:
move_temp_counter = randint(1, 6)
block_value_predict[0]['image'] = self.block_number_side[move_temp_counter - 1]
self.window.update()
time.sleep(0.1)
temp_counter-=1
print("Prediction result: ", permanent_block_number)
# Permanent predicted value containing image set
block_value_predict[0]['image'] = self.block_number_side[permanent_block_number-1]
self.instructional_btn_customization_based_on_current_situation(color_indicator,permanent_block_number,block_value_predict)
except:
print("Force stop error")
def instructional_btn_customization_based_on_current_situation(self,color_indicator,permanent_block_number,block_value_predict):
if color_indicator == "red":
temp_coin_position = self.red_coin_position
elif color_indicator == "green":
temp_coin_position = self.green_coin_position
elif color_indicator == "yellow":
temp_coin_position = self.yellow_coin_position
else:
temp_coin_position = self.sky_blue_coin_position
all_in = 1
for i in range(4):
if temp_coin_position[i] == -1:
all_in = 1
else:
all_in = 0
break
if permanent_block_number == 6:
self.six_counter += 1
else:
self.six_counter = 0
if ((all_in == 1 and permanent_block_number == 6) or (all_in==0)) and self.six_counter<3:
permission = 1
if color_indicator == "red":
temp = self.red_coord_store
elif color_indicator == "green":
temp = self.green_coord_store
elif color_indicator == "yellow":
temp = self.yellow_coord_store
else:
temp = self.sky_blue_coord_store
if permanent_block_number<6:
if self.six_with_overlap == 1:
self.time_for-=1
self.six_with_overlap=0
for i in range(4):
if temp[i] == -1:
permission=0
elif temp[i]>100:
if temp[i]+permanent_block_number<=106:
permission=1
break
else:
permission=0
else:
permission=1
break
else:
for i in range(4):
if temp[i]>100:
if temp[i] + permanent_block_number <= 106:
permission = 1
break
else:
permission = 0
else:
permission = 1
break
if permission == 0:
self.make_command()
else:
block_value_predict[3]['state'] = NORMAL# Give btn activation
block_value_predict[1]['state'] = DISABLED# Predict btn deactivation
else:
block_value_predict[1]['state'] = NORMAL# Predict btn activation
if self.six_with_overlap == 1:
self.time_for -= 1
self.six_with_overlap = 0
self.make_command()
if permanent_block_number == 6 and self.six_counter<3 and block_value_predict[3]['state'] == NORMAL:
self.time_for-=1
else:
self.six_counter=0
# Player Scope controller
def make_command(self):
if self.time_for == -1:
pass
else:
self.block_value_predict[self.total_people_play[self.time_for]][1]['state'] = DISABLED
if self.time_for == len(self.total_people_play)-1:
self.time_for = -1
self.time_for+=1
self.block_value_predict[self.total_people_play[self.time_for]][1]['state'] = NORMAL
def instruction_btn_red(self):
block_predict_red = Label(self.make_canvas,image=self.block_number_side[0])
block_predict_red.place(x=45,y=15)
predict_red = Button(self.make_canvas, bg="black", fg="#00FF00", relief=RAISED, bd=5, text="Predict", font=("Arial", 8, "bold"), command=lambda: self.make_prediction("red"))
predict_red.place(x=37, y=15 + 40)
entry_take_red = Entry(self.make_canvas,bg="white",fg="blue",font=("Arial",25,"bold","italic"),width=2,relief=SUNKEN,bd=5)
entry_take_red.place(x=40,y=15+80)
final_move = Button(self.make_canvas,bg="black",fg="#00FF00",relief=RAISED,bd=5,text="Give",font=("Arial",8,"bold"),command=lambda: self.main_controller("red",entry_take_red.get()),state=DISABLED)
final_move.place(x=42,y=15+140)
Label(self.make_canvas,text="Player 1",bg="#4d4dff",fg="gold",font=("Arial",15,"bold")).place(x=15,y=15+140+40)
self.store_instructional_btn(block_predict_red,predict_red,entry_take_red,final_move)
def instruction_btn_sky_blue(self):
block_predict_sky_blue = Label(self.make_canvas, image=self.block_number_side[0])
block_predict_sky_blue.place(x=45, y=15+(40*6+40*3)+10)
predict_sky_blue = Button(self.make_canvas, bg="black", fg="#00FF00", relief=RAISED, bd=5, text="Predict",font=("Arial", 8, "bold"), command=lambda: self.make_prediction("sky_blue"))
predict_sky_blue.place(x=37, y=15+(40*6+40*3)+40 + 10)
entry_take_sky_blue = Entry(self.make_canvas, bg="white", fg="blue", font=("Arial", 25, "bold", "italic"), width=2,relief=SUNKEN, bd=5)
entry_take_sky_blue.place(x=40, y=15+(40*6+40*3)+40 + 50)
final_move = Button(self.make_canvas, bg="black", fg="#00FF00", relief=RAISED, bd=5, text="Give", font=("Arial", 8, "bold"),command=lambda: self.main_controller("sky_blue",entry_take_sky_blue.get()),state=DISABLED)
final_move.place(x=42, y=15+(40*6+40*3)+40 + 110)
Label(self.make_canvas, text="Player 2", bg="#4d4dff", fg="gold", font=("Arial", 15, "bold")).place(x=15,y=15+(40*6+40*3)+40 + 110+ 40)
self.store_instructional_btn(block_predict_sky_blue, predict_sky_blue, entry_take_sky_blue, final_move)
def instruction_btn_yellow(self):
block_predict_yellow = Label(self.make_canvas, image=self.block_number_side[0])
block_predict_yellow.place(x=100 + (40 * 6 + 40 * 3 + 40 * 6 + 10)+10, y=15 + (40 * 6 + 40 * 3) + 10)
predict_yellow = Button(self.make_canvas, bg="black", fg="#00FF00", relief=RAISED, bd=5, text="Predict",font=("Arial", 8, "bold"), command=lambda: self.make_prediction("yellow"))
predict_yellow.place(x=100 + (40 * 6 + 40 * 3 + 40 * 6 + 2)+10, y=15 + (40 * 6 + 40 * 3) + 40 + 10)
entry_take_yellow = Entry(self.make_canvas, bg="white", fg="blue", font=("Arial", 25, "bold", "italic"),width=2, relief=SUNKEN, bd=5)
entry_take_yellow.place(x=100 + (40 * 6 + 40 * 3 + 40 * 6 + 2)+13, y=15 + (40 * 6 + 40 * 3) + 40 + 50)
final_move = Button(self.make_canvas, bg="black", fg="#00FF00", relief=RAISED, bd=5, text="Give",font=("Arial", 8, "bold"),command=lambda: self.main_controller("yellow",entry_take_yellow.get()),state=DISABLED)
final_move.place(x=100 + (40 * 6 + 40 * 3 + 40 * 6 + 2)+17, y=15 + (40 * 6 + 40 * 3) + 40 + 110)
Label(self.make_canvas, text="Player 3", bg="#4d4dff", fg="gold", font=("Arial", 15, "bold")).place(x=100 + (40 * 6 + 40 * 3 + 40 * 6 + 3),y=15 + (40 * 6 + 40 * 3) + 40 + 110 + 40)
self.store_instructional_btn(block_predict_yellow, predict_yellow, entry_take_yellow, final_move)
def instruction_btn_green(self):
block_predict_green = Label(self.make_canvas, image=self.block_number_side[0])
block_predict_green.place(x=100+(40*6+40*3+40*6+10)+10, y=15)
predict_green = Button(self.make_canvas, bg="black", fg="#00FF00", relief=RAISED, bd=5, text="Predict", font=("Arial", 8, "bold"), command=lambda: self.make_prediction("green"))
predict_green.place(x=100+(40*6+40*3+40*6+2)+10, y=15 + 40)
entry_take_green = Entry(self.make_canvas, bg="white", fg="blue", font=("Arial", 25, "bold", "italic"), width=2, relief=SUNKEN, bd=5)
entry_take_green.place(x=100+(40*6+40*3+40*6+2)+13, y=15 + 80)
final_move = Button(self.make_canvas, bg="black", fg="#00FF00", relief=RAISED, bd=5, text="Give",font=("Arial", 8, "bold"),command=lambda: self.main_controller("green",entry_take_green.get()),state=DISABLED)
final_move.place(x=100+(40*6+40*3+40*6+2)+17, y=15 + 140)
Label(self.make_canvas, text="Player 4", bg="#4d4dff", fg="gold", font=("Arial", 15, "bold")).place(x=100+(40*6+40*3+40*6+3), y=15 + 140+40)
self.store_instructional_btn(block_predict_green, predict_green, entry_take_green, final_move)
def store_instructional_btn(self, block_indicator, predictor, entry_controller, give_finally):
temp = []
temp.append(block_indicator)
temp.append(predictor)
temp.append(entry_controller)
temp.append(give_finally)
self.block_value_predict.append(temp)
# self.block_value_predict[0] = red instruction information
# self.block_value_predict[1] = sky_blue instruction information
# self.block_value_predict[2] = yellow instruction information
# self.block_value_predict[3] = green instruction information
# self.block_value_predict[something][1] = Predict btn
# self.block_value_predict[something][3] = Give btn
def red_circle_start_position(self, coin_number):
self.make_canvas.delete(self.made_red_coin[int(coin_number)-1])
self.made_red_coin[int(coin_number)-1] = self.make_canvas.create_oval(100 + 40, 15+(40*6), 100 +40 + 40, 15+(40*6)+40, fill="red", width=3, outline="black")
self.red_number_label[int(coin_number)-1].place_forget()
red_start_label_x = 100 + 40 + 10
red_start_label_y = 15 + (40 * 6) + 5
self.red_number_label[int(coin_number)-1].place(x=red_start_label_x, y=red_start_label_y)
self.red_coin_position[int(coin_number)-1] = 1
self.window.update()
time.sleep(0.2)
def green_circle_start_position(self,coin_number):
self.make_canvas.delete(self.made_green_coin[int(coin_number)-1])
self.made_green_coin[int(coin_number)-1] = self.make_canvas.create_oval(100 + (40*8), 15 + 40, 100 +(40*9), 15 + 40+ 40, fill="#00FF00", width=3)
self.green_number_label[int(coin_number)-1].place_forget()
green_start_label_x = 100 + (40*8) + 10
green_start_label_y = 15 + 40 + 5
self.green_number_label[int(coin_number)-1].place(x=green_start_label_x, y=green_start_label_y)
self.green_coin_position[int(coin_number)-1] = 14
self.window.update()
time.sleep(0.2)
def yellow_circle_start_position(self,coin_number):
self.make_canvas.delete(self.made_yellow_coin[int(coin_number)-1])
self.made_yellow_coin[int(coin_number)-1] = self.make_canvas.create_oval(100 + (40 * 6)+(40*3)+(40*4), 15 + (40*8), 100 + (40 * 6)+(40*3)+(40*5), 15 + (40*9), fill="yellow", width=3)
self.yellow_number_label[int(coin_number)-1].place_forget()
yellow_start_label_x = 100 + (40 * 6)+(40*3)+(40*4) + 10
yellow_start_label_y = 15 + (40*8) + 5
self.yellow_number_label[int(coin_number) - 1].place(x=yellow_start_label_x, y=yellow_start_label_y)
self.yellow_coin_position[int(coin_number) - 1] = 27
self.window.update()
time.sleep(0.2)
def sky_blue_circle_start_position(self,coin_number):
self.make_canvas.delete(self.made_sky_blue_coin[int(coin_number)-1])
self.made_sky_blue_coin[int(coin_number)-1] = self.make_canvas.create_oval(100+240,340+(40*5)-5,100+240+40,340+(40*6)-5,fill="#04d9ff",width=3)
self.sky_blue_number_label[int(coin_number)-1].place_forget()
sky_blue_start_label_x = 100+240 + 10
sky_blue_start_label_y = 340+(40*5)-5 + 5
self.sky_blue_number_label[int(coin_number) - 1].place(x=sky_blue_start_label_x, y=sky_blue_start_label_y)
self.sky_blue_coin_position[int(coin_number) - 1] = 40
self.window.update()
time.sleep(0.2)
def main_controller(self, color_coin, coin_number):
processing_result = self.input_filtering(coin_number)# Value filtering
if processing_result is True:
pass
else:
messagebox.showerror("Wrong input number","Please input the coin number between 1 to 4")
return
if color_coin == "red":
self.block_value_predict[0][3]['state'] = DISABLED
if self.move_red_counter == 106:
messagebox.showwarning("Destination reached","Reached at the destination")
elif self.red_coin_position[int(coin_number)-1] == -1 and self.move_red_counter == 6:
self.red_circle_start_position(coin_number)
self.red_coord_store[int(coin_number) - 1] = 1
elif self.red_coin_position[int(coin_number)-1] > -1:
take_coord = self.make_canvas.coords(self.made_red_coin[int(coin_number)-1])
red_start_label_x = take_coord[0] + 10
red_start_label_y = take_coord[1] + 5
self.red_number_label[int(coin_number) - 1].place(x=red_start_label_x, y=red_start_label_y)
if self.red_coin_position[int(coin_number)-1]+self.move_red_counter<=106:
self.red_coin_position[int(coin_number)-1] = self.motion_of_coin(self.red_coin_position[int(coin_number) - 1],self.made_red_coin[int(coin_number)-1],self.red_number_label[int(coin_number)-1],red_start_label_x,red_start_label_y,"red",self.move_red_counter)
else:
messagebox.showerror("Not possible","Sorry, not permitted")
self.block_value_predict[0][3]['state'] = NORMAL
return
if self.red_coin_position[int(coin_number)-1]==22 or self.red_coin_position[int(coin_number)-1]==9 or self.red_coin_position[int(coin_number)-1]==48 or self.red_coin_position[int(coin_number)-1]==35 or self.red_coin_position[int(coin_number)-1]==14 or self.red_coin_position[int(coin_number)-1]==27 or self.red_coin_position[int(coin_number)-1]==40:
pass
else:
if self.red_coin_position[int(coin_number) - 1] < 100:
self.coord_overlap(self.red_coin_position[int(coin_number)-1],color_coin, self.move_red_counter)
self.red_coord_store[int(coin_number)-1] = self.red_coin_position[int(coin_number)-1]
else:
messagebox.showerror("Wrong choice","Sorry, Your coin in not permitted to travel")
self.block_value_predict[0][3]['state'] = NORMAL
return
self.block_value_predict[0][1]['state'] = NORMAL
elif color_coin == "green":
self.block_value_predict[3][3]['state'] = DISABLED
if self.move_green_counter == 106:
messagebox.showwarning("Destination reached","Reached at the destination")
elif self.green_coin_position[int(coin_number) - 1] == -1 and self.move_green_counter == 6:
self.green_circle_start_position(coin_number)
self.green_coord_store[int(coin_number) - 1] = 14
elif self.green_coin_position[int(coin_number) - 1] > -1:
take_coord = self.make_canvas.coords(self.made_green_coin[int(coin_number) - 1])
green_start_label_x = take_coord[0] + 10
green_start_label_y = take_coord[1] + 5
self.green_number_label[int(coin_number) - 1].place(x=green_start_label_x, y=green_start_label_y)
if self.green_coin_position[int(coin_number) - 1] + self.move_green_counter <= 106:
self.green_coin_position[int(coin_number) - 1] = self.motion_of_coin(self.green_coin_position[int(coin_number) - 1], self.made_green_coin[int(coin_number) - 1], self.green_number_label[int(coin_number) - 1], green_start_label_x, green_start_label_y, "green", self.move_green_counter)
else:
messagebox.showerror("Not possible","No path available")
self.block_value_predict[3][3]['state'] = NORMAL
return
if self.green_coin_position[int(coin_number)-1]==22 or self.green_coin_position[int(coin_number)-1]==9 or self.green_coin_position[int(coin_number)-1]==48 or self.green_coin_position[int(coin_number)-1]==35 or self.green_coin_position[int(coin_number)-1]==1 or self.green_coin_position[int(coin_number)-1]==27 or self.green_coin_position[int(coin_number)-1]==40:
pass
else:
if self.green_coin_position[int(coin_number) - 1] < 100:
self.coord_overlap(self.green_coin_position[int(coin_number) - 1],color_coin, self.move_green_counter)
self.green_coord_store[int(coin_number) - 1] = self.green_coin_position[int(coin_number) - 1]
else:
messagebox.showerror("Wrong choice", "Sorry, Your coin in not permitted to travel")
self.block_value_predict[3][3]['state'] = NORMAL
return
self.block_value_predict[3][1]['state'] = NORMAL
elif color_coin == "yellow":
self.block_value_predict[2][3]['state'] = DISABLED
if self.move_yellow_counter == 106:
messagebox.showwarning("Destination reached","Reached at the destination")
elif self.yellow_coin_position[int(coin_number) - 1] == -1 and self.move_yellow_counter == 6:
self.yellow_circle_start_position(coin_number)
self.yellow_coord_store[int(coin_number) - 1] = 27
elif self.yellow_coin_position[int(coin_number) - 1] > -1:
take_coord = self.make_canvas.coords(self.made_yellow_coin[int(coin_number) - 1])
yellow_start_label_x = take_coord[0] + 10
yellow_start_label_y = take_coord[1] + 5
self.yellow_number_label[int(coin_number) - 1].place(x=yellow_start_label_x, y=yellow_start_label_y)
if self.yellow_coin_position[int(coin_number) - 1] + self.move_yellow_counter <= 106:
self.yellow_coin_position[int(coin_number) - 1] = self.motion_of_coin(self.yellow_coin_position[int(coin_number) - 1], self.made_yellow_coin[int(coin_number) - 1], self.yellow_number_label[int(coin_number) - 1], yellow_start_label_x, yellow_start_label_y, "yellow", self.move_yellow_counter)
else:
messagebox.showerror("Not possible","No path available")
self.block_value_predict[2][3]['state'] = NORMAL
return
if self.yellow_coin_position[int(coin_number)-1]==22 or self.yellow_coin_position[int(coin_number)-1]==9 or self.yellow_coin_position[int(coin_number)-1]==48 or self.yellow_coin_position[int(coin_number)-1]==35 or self.yellow_coin_position[int(coin_number)-1]==1 or self.yellow_coin_position[int(coin_number)-1]==14 or self.yellow_coin_position[int(coin_number)-1]==40:
pass
else:
if self.yellow_coin_position[int(coin_number) - 1] < 100:
self.coord_overlap(self.yellow_coin_position[int(coin_number) - 1],color_coin, self.move_yellow_counter)
self.yellow_coord_store[int(coin_number) - 1] = self.yellow_coin_position[int(coin_number) - 1]
else:
messagebox.showerror("Wrong choice", "Sorry, Your coin in not permitted to travel")
self.block_value_predict[2][3]['state'] = NORMAL
return
self.block_value_predict[2][1]['state'] = NORMAL
elif color_coin == "sky_blue":
self.block_value_predict[1][3]['state'] = DISABLED
if self.move_red_counter == 106:
messagebox.showwarning("Destination reached","Reached at the destination")
elif self.sky_blue_coin_position[int(coin_number) - 1] == -1 and self.move_sky_blue_counter == 6:
self.sky_blue_circle_start_position(coin_number)
self.sky_blue_coord_store[int(coin_number) - 1] = 40
elif self.sky_blue_coin_position[int(coin_number) - 1] > -1:
take_coord = self.make_canvas.coords(self.made_sky_blue_coin[int(coin_number) - 1])
sky_blue_start_label_x = take_coord[0] + 10
sky_blue_start_label_y = take_coord[1] + 5
self.sky_blue_number_label[int(coin_number) - 1].place(x=sky_blue_start_label_x, y=sky_blue_start_label_y)
if self.sky_blue_coin_position[int(coin_number) - 1] + self.move_sky_blue_counter <= 106:
self.sky_blue_coin_position[int(coin_number) - 1] = self.motion_of_coin(self.sky_blue_coin_position[int(coin_number) - 1], self.made_sky_blue_coin[int(coin_number) - 1], self.sky_blue_number_label[int(coin_number) - 1], sky_blue_start_label_x, sky_blue_start_label_y, "sky_blue", self.move_sky_blue_counter)
else:
messagebox.showerror("Not possible","No path available")
self.block_value_predict[1][3]['state'] = NORMAL
return
if self.sky_blue_coin_position[int(coin_number)-1]==22 or self.sky_blue_coin_position[int(coin_number)-1]==9 or self.sky_blue_coin_position[int(coin_number)-1]==48 or self.sky_blue_coin_position[int(coin_number)-1]==35 or self.sky_blue_coin_position[int(coin_number)-1]==1 or self.sky_blue_coin_position[int(coin_number)-1]==14 or self.sky_blue_coin_position[int(coin_number)-1]==27:
pass
else:
if self.sky_blue_coin_position[int(coin_number) - 1] < 100:
self.coord_overlap(self.sky_blue_coin_position[int(coin_number) - 1],color_coin, self.move_sky_blue_counter)
self.sky_blue_coord_store[int(coin_number) - 1] = self.sky_blue_coin_position[int(coin_number) - 1]
else:
messagebox.showerror("Wrong choice", "Sorry, Your coin in not permitted to travel")
self.block_value_predict[1][3]['state'] = NORMAL
return
self.block_value_predict[1][1]['state'] = NORMAL
print(self.red_coord_store)
print(self.green_coord_store)
print(self.yellow_coord_store)
print(self.sky_blue_coord_store)
permission_granted_to_proceed = True
if color_coin == "red" and self.red_coin_position[int(coin_number)-1] == 106:
permission_granted_to_proceed = self.check_winner_and_runner(color_coin)
elif color_coin == "green" and self.green_coin_position[int(coin_number)-1] == 106:
permission_granted_to_proceed = self.check_winner_and_runner(color_coin)
elif color_coin == "yellow" and self.yellow_coin_position[int(coin_number)-1] == 106:
permission_granted_to_proceed = self.check_winner_and_runner(color_coin)
elif color_coin == "sky_blue" and self.sky_blue_coin_position[int(coin_number)-1] == 106:
permission_granted_to_proceed = self.check_winner_and_runner(color_coin)
if permission_granted_to_proceed:# if that is False, Game is over and not proceed more
self.make_command()
def motion_of_coin(self,counter_coin,specific_coin,number_label,number_label_x ,number_label_y,color_coin,path_counter):
number_label.place(x=number_label_x,y=number_label_y)
while True:
if path_counter == 0:
break
elif (counter_coin == 51 and color_coin == "red") or (counter_coin==12 and color_coin == "green") or (counter_coin == 25 and color_coin == "yellow") or (counter_coin == 38 and color_coin == "sky_blue") or counter_coin>=100:
if counter_coin<100:
counter_coin=100
counter_coin = self.under_room_traversal_control(specific_coin, number_label, number_label_x, number_label_y, path_counter, counter_coin, color_coin)
if counter_coin == 106:
messagebox.showinfo("Destination reached","Congrats! You now at the destination")
if path_counter == 6:
self.six_with_overlap = 1
else:
self.time_for -= 1
break
counter_coin += 1
path_counter -=1
number_label.place_forget()
print(counter_coin)
if counter_coin<=5:
self.make_canvas.move(specific_coin, 40, 0)
number_label_x+=40
elif counter_coin == 6:
self.make_canvas.move(specific_coin, 40, -40)
number_label_x += 40
number_label_y-=40
elif 6< counter_coin <=11:
self.make_canvas.move(specific_coin, 0, -40)
number_label_y -= 40
elif counter_coin <=13:
self.make_canvas.move(specific_coin, 40, 0)
number_label_x += 40
elif counter_coin <=18:
self.make_canvas.move(specific_coin, 0, 40)
number_label_y += 40
elif counter_coin == 19:
self.make_canvas.move(specific_coin, 40, 40)
number_label_x += 40
number_label_y += 40
elif counter_coin <=24:
self.make_canvas.move(specific_coin, 40, 0)
number_label_x += 40
elif counter_coin <=26:
self.make_canvas.move(specific_coin, 0, 40)
number_label_y += 40
elif counter_coin <=31:
self.make_canvas.move(specific_coin, -40, 0)
number_label_x -= 40
elif counter_coin == 32:
self.make_canvas.move(specific_coin, -40, 40)
number_label_x -= 40
number_label_y += 40
elif counter_coin <= 37:
self.make_canvas.move(specific_coin, 0, 40)
number_label_y += 40
elif counter_coin <= 39:
self.make_canvas.move(specific_coin, -40, 0)
number_label_x -= 40
elif counter_coin <= 44:
self.make_canvas.move(specific_coin, 0, -40)
number_label_y -= 40
elif counter_coin == 45:
self.make_canvas.move(specific_coin, -40, -40)
number_label_x -= 40
number_label_y -= 40
elif counter_coin <= 50:
self.make_canvas.move(specific_coin, -40, 0)
number_label_x -= 40
elif 50< counter_coin <=52:
self.make_canvas.move(specific_coin, 0, -40)
number_label_y -= 40
elif counter_coin == 53:
self.make_canvas.move(specific_coin, 40, 0)
number_label_x += 40
counter_coin = 1
number_label.place_forget()
number_label.place(x=number_label_x, y=number_label_y)
self.window.update()
time.sleep(0.2)
return counter_coin
# For same position, previous coin deleted and set to the room
def coord_overlap(self, counter_coin, color_coin, path_to_traverse_before_overlap):
if color_coin!="red":
for take_coin_number in range(len(self.red_coord_store)):
if self.red_coord_store[take_coin_number] == counter_coin:
if path_to_traverse_before_overlap == 6:
self.six_with_overlap=1
else:
self.time_for-=1
self.make_canvas.delete(self.made_red_coin[take_coin_number])
self.red_number_label[take_coin_number].place_forget()
self.red_coin_position[take_coin_number] = -1
self.red_coord_store[take_coin_number] = -1
if take_coin_number == 0:
remade_coin = self.make_canvas.create_oval(100+40, 15+40, 100+40+40, 15+40+40, width=3, fill="red", outline="black")
self.red_number_label[take_coin_number].place(x=100 + 40 + 10, y=15 + 40 + 5)
elif take_coin_number == 1:
remade_coin = self.make_canvas.create_oval(100+40+60+60, 15 + 40, 100+40+60+60+40, 15 + 40 + 40, width=3, fill="red", outline="black")
self.red_number_label[take_coin_number].place(x=100 + 40 + 60 +60 + 10, y=15 + 40 + 5)
elif take_coin_number == 2:
remade_coin = self.make_canvas.create_oval(100 + 40 + 60 + 60, 15 + 40 + 100, 100 + 40 + 60 + 60 + 40, 15 + 40 + 40 + 100, width=3, fill="red", outline="black")
self.red_number_label[take_coin_number].place(x=100 + 40 + 60 + 60 + 10, y=15 + 40 + 100 + 5)
else:
remade_coin = self.make_canvas.create_oval(100 + 40, 15 + 40+100, 100 + 40 + 40, 15 + 40 + 40+100, width=3,fill="red", outline="black")
self.red_number_label[take_coin_number].place(x=100 + 40 + 10, y=15 + 40 + 100 + 5)
self.made_red_coin[take_coin_number]=remade_coin
if color_coin != "green":
for take_coin_number in range(len(self.green_coord_store)):
if self.green_coord_store[take_coin_number] == counter_coin:
if path_to_traverse_before_overlap == 6:
self.six_with_overlap = 1
else:
self.time_for-=1
self.make_canvas.delete(self.made_green_coin[take_coin_number])
self.green_number_label[take_coin_number].place_forget()
self.green_coin_position[take_coin_number] = -1
self.green_coord_store[take_coin_number] = -1
if take_coin_number == 0:
remade_coin = self.make_canvas.create_oval(340+(40*3)+40, 15 + 40, 340+(40*3)+40 + 40, 15 + 40 + 40, width=3, fill="#00FF00", outline="black")
self.green_number_label[take_coin_number].place(x=340 + (40 * 3) + 40 + 10, y=15 + 40 + 5)
elif take_coin_number == 1:
remade_coin = self.make_canvas.create_oval(340+(40*3)+40+ 60 + 40+20, 15 + 40, 340+(40*3)+40 + 60 + 40 + 40+20, 15 + 40 + 40, width=3, fill="#00FF00", outline="black")
self.green_number_label[take_coin_number].place(x=340 + (40 * 3) + 40 + 40 + 60 + 30, y=15 + 40 + 5)
elif take_coin_number == 2:
remade_coin = self.make_canvas.create_oval(340 + (40 * 3) + 40 + 60 + 40 + 20, 15 + 40 + 100, 340 + (40 * 3) + 40 + 60 + 40 + 40 + 20, 15 + 40 + 40 + 100, width=3, fill="#00FF00", outline="black")
self.green_number_label[take_coin_number].place(x=340 + (40 * 3) + 40 + 40 + 60 + 30, y=15 + 40 + 100 + 5)
else:
remade_coin = self.make_canvas.create_oval(340+(40*3)+40, 15 + 40 + 100, 340+(40*3)+40 + 40, 15 + 40 + 40 + 100, width=3, fill="#00FF00", outline="black")
self.green_number_label[take_coin_number].place(x=340+(40*3) + 40 + 10, y=15 + 40 + 100 + 5)
self.made_green_coin[take_coin_number] = remade_coin
if color_coin != "yellow":
for take_coin_number in range(len(self.yellow_coord_store)):
if self.yellow_coord_store[take_coin_number] == counter_coin:
if path_to_traverse_before_overlap == 6:
self.six_with_overlap = 1
else:
self.time_for -= 1
self.make_canvas.delete(self.made_yellow_coin[take_coin_number])
self.yellow_number_label[take_coin_number].place_forget()
self.yellow_coin_position[take_coin_number] = -1
self.yellow_coord_store[take_coin_number] = -1
if take_coin_number == 0:
remade_coin = self.make_canvas.create_oval(340 + (40 * 3) + 40, 340+80+15, 340 + (40 * 3) + 40 + 40, 340+80+40+15, width=3, fill="yellow", outline="black")
self.yellow_number_label[take_coin_number].place(x=340+(40*3) + 40 + 10, y=30 + (40*6)+(40*3)+40+10)
elif take_coin_number == 1:
remade_coin = self.make_canvas.create_oval(340 + (40 * 3) + 40 + 60 + 40 + 20, 340+80+15, 340 + (40 * 3) + 40 + 60 + 40 + 40+20, 340+80+40+15, width=3, fill="yellow", outline="black")
self.yellow_number_label[take_coin_number].place(x=340+(40*3)+ 40 + 40+ 60 + 30, y=30 + (40*6)+(40*3)+40+10)
elif take_coin_number == 2:
remade_coin = self.make_canvas.create_oval(340 + (40 * 3) + 40 + 60 + 40 + 20, 340 + 80 + 60 + 40 + 15, 340 + (40 * 3) + 40 + 60 + 40 + 40 + 20, 340 + 80 + 60 + 40 + 40 + 15, width=3, fill="yellow", outline="black")
self.yellow_number_label[take_coin_number].place(x=340+(40*3)+ 40 + 40+ 60 + 30, y=30 + (40*6)+(40*3)+40+100+10)
else:
remade_coin = self.make_canvas.create_oval(340 + (40 * 3) + 40, 340+80+60+40+15, 340 + (40 * 3) + 40 + 40,340+80+60+40+40+15, width=3, fill="yellow", outline="black")
self.yellow_number_label[take_coin_number].place(x=340 + (40 * 3) + 40 + 10, y=30 + (40 * 6) + (40 * 3) + 40 + 100 + 10)
self.made_yellow_coin[take_coin_number] = remade_coin
if color_coin != "sky_blue":
for take_coin_number in range(len(self.sky_blue_coord_store)):
if self.sky_blue_coord_store[take_coin_number] == counter_coin:
if path_to_traverse_before_overlap == 6:
self.six_with_overlap = 1
else:
self.time_for -= 1
self.make_canvas.delete(self.made_sky_blue_coin[take_coin_number])
self.sky_blue_number_label[take_coin_number].place_forget()
self.sky_blue_coin_position[take_coin_number] = -1
self.sky_blue_coord_store[take_coin_number]=-1
if take_coin_number == 0:
remade_coin = self.make_canvas.create_oval(100 + 40, 340+80+15, 100 + 40 + 40, 340+80+40+15, width=3, fill="#04d9ff", outline="black")
self.sky_blue_number_label[take_coin_number].place(x=100+40+10, y=30 + (40*6)+(40*3)+40+10)
elif take_coin_number == 1:
remade_coin = self.make_canvas.create_oval(100 + 40 + 60 + 40+20, 340+80+15, 100 + 40 + 60 + 40 + 40+20, 340+80+40+15, width=3, fill="#04d9ff", outline="black")
self.sky_blue_number_label[take_coin_number].place(x=100 + 40 + 60 +60 + 10, y=30 + (40*6)+(40*3)+40+10)
elif take_coin_number == 2:
remade_coin = self.make_canvas.create_oval(100 + 40 + 60 + 40 + 20, 340 + 80 + 60 + 40 + 15, 100 + 40 + 60 + 40 + 40 + 20, 340 + 80 + 60 + 40 + 40 + 15, width=3, fill="#04d9ff", outline="black")
self.sky_blue_number_label[take_coin_number].place(x=100 + 40 + 60 + 60 + 10, y=30 + (40 * 6) + (40 * 3) + 40 + 60 + 40 + 10)
else:
remade_coin = self.make_canvas.create_oval( 100 + 40, 340+80+60+40+15, 100 + 40 + 40, 340+80+60+40+40+15, width=3, fill="#04d9ff", outline="black")
self.sky_blue_number_label[take_coin_number].place(x=100+40+10, y=30 + (40*6)+(40*3)+40+60+40+10)
self.made_sky_blue_coin[take_coin_number] = remade_coin
def under_room_traversal_control(self,specific_coin,number_label,number_label_x,number_label_y,path_counter,counter_coin,color_coin):
if color_coin == "red" and counter_coin >= 100:
if int(counter_coin)+int(path_counter)<=106:
counter_coin = self.room_red_traversal(specific_coin, number_label, number_label_x, number_label_y, path_counter, counter_coin)
elif color_coin == "green" and counter_coin >= 100:
if int(counter_coin) + int(path_counter) <= 106:
counter_coin = self.room_green_traversal(specific_coin, number_label, number_label_x, number_label_y,path_counter,counter_coin)
elif color_coin == "yellow" and counter_coin >= 100:
if int(counter_coin) + int(path_counter) <= 106:
counter_coin = self.room_yellow_traversal(specific_coin, number_label, number_label_x, number_label_y,path_counter,counter_coin)
elif color_coin == "sky_blue" and counter_coin >= 100:
if int(counter_coin) + int(path_counter) <= 106:
counter_coin = self.room_sky_blue_traversal(specific_coin, number_label, number_label_x, number_label_y,path_counter,counter_coin)
return counter_coin
def room_red_traversal(self, specific_coin, number_label, number_label_x, number_label_y, path_counter, counter_coin):
while path_counter>0:
counter_coin += 1
path_counter -= 1
self.make_canvas.move(specific_coin, 40, 0)
number_label_x+=40
number_label.place(x=number_label_x,y=number_label_y)
self.window.update()
time.sleep(0.2)
return counter_coin
def room_green_traversal(self, specific_coin, number_label, number_label_x, number_label_y, path_counter, counter_coin):
while path_counter > 0:
counter_coin += 1
path_counter -= 1
self.make_canvas.move(specific_coin, 0, 40)
number_label_y += 40
number_label.place(x=number_label_x, y=number_label_y)
self.window.update()
time.sleep(0.2)
return counter_coin
def room_yellow_traversal(self, specific_coin, number_label, number_label_x, number_label_y,path_counter,counter_coin):
while path_counter > 0:
counter_coin += 1
path_counter -= 1
self.make_canvas.move(specific_coin, -40, 0)
number_label_x -= 40
number_label.place(x=number_label_x, y=number_label_y)
self.window.update()
time.sleep(0.2)
return counter_coin
def room_sky_blue_traversal(self, specific_coin, number_label, number_label_x, number_label_y,path_counter,counter_coin):
while path_counter > 0:
counter_coin += 1
path_counter -= 1
self.make_canvas.move(specific_coin, 0, -40)
number_label_y -= 40
number_label.place(x=number_label_x, y=number_label_y)
self.window.update()
time.sleep(0.2)
return counter_coin
def check_winner_and_runner(self,color_coin):
destination_reached = 0 # Check for all specific color coins
if color_coin == "red":
temp_store = self.red_coord_store
temp_delete = 0# Player index
elif color_coin == "green":
temp_store = self.green_coord_store
temp_delete = 3# Player index
elif color_coin == "yellow":
temp_store = self.yellow_coord_store
temp_delete = 2# Player index
else:
temp_store = self.sky_blue_coord_store
temp_delete = 1# Player index
for take in temp_store:
if take == 106:
destination_reached = 1
else:
destination_reached = 0
break
if destination_reached == 1:# If all coins in block reach to the destination, winner and runner check
self.take_permission += 1
if self.take_permission == 1:# Winner check
messagebox.showinfo("Winner","Congrats! You are the winner")
elif self.take_permission == 2:# 1st runner check
messagebox.showinfo("Winner", "Wow! You are 1st runner")
elif self.take_permission == 3:# 2nd runner check
messagebox.showinfo("Winner", "Wow! You are 2nd runner")
self.block_value_predict[temp_delete][1]['state'] = DISABLED
self.total_people_play.remove(temp_delete)
if len(self.total_people_play) == 1:
messagebox.showinfo("Game Over","Good bye!!!!")
self.block_value_predict[0][1]['state'] = DISABLED
return False
else:
self.time_for-=1
else:
print("Winner not decided")
return True
# Input value checking
def input_filtering(self,coin_number):
try:
if (4>=int(coin_number)>=1) or type(coin_number) == int:
return True
else:
return False
except:
return False
if __name__ == '__main__':
window = Tk()
window.geometry("800x630")
window.maxsize(800,630)
window.minsize(800,630)
window.title("play ludo with gaurav")
window.iconbitmap(R"C:\Users\madde\PycharmProjects\game\pics\ludo-bg.jpg")
block_six_side = ImageTk.PhotoImage(Image.open("pics/6_block.png").resize((33, 33),))
block_five_side = ImageTk.PhotoImage(Image.open("pics/5_block.png").resize((33, 33),))
block_four_side = ImageTk.PhotoImage(Image.open("pics/4_block.png").resize((33, 33),))
block_three_side = ImageTk.PhotoImage(Image.open("pics/3_block.png").resize((33, 33),))
block_two_side = ImageTk.PhotoImage(Image.open("pics/2_block.png").resize((33, 33),))
block_one_side = ImageTk.PhotoImage(Image.open("pics/1_block.png").resize((33, 33),))
Ludo(window,block_six_side,block_five_side,block_four_side,block_three_side,block_two_side,block_one_side)
window.mainloop()
0 Comments