Create Ludo Game Project in Python [Source Code Included]
FREE Online Courses: Knowledge Awaits – Click for Free Access!
We all would have played the Ludo Game with our friends or family members? It would be fun to take revenge by killing someone’s token right? It is more fun to play the game built on your game. So, gear up to build the Ludo Game using Python.
What is a Ludo Game?
Ludo Game is a board game for two to four-player in which the players try to move their four tokens from start to end by rolling a die at their turns. The turns are taken circularly by each player.
In each turn, he/she throws a die and moves a token by the number appearing, aiming to reach the end and trying to prevent the other player(s) from winning.
Ludo Game, a Python project
To build this game, we will be using the PyGame and Random modules. We use the PyGame module to build the main game window and to control the game. While the random module is used to get a number from 1 to 6 on throwing a die.
Download the Ludo Game in Python
Please download the source code for the Ludo Game in Python using the link: Ludo Game Project
Project Prerequisites
The knowledge of Python and the Pygame module would be required by the developer to build the project. You can download the Pygame and the random modules using the following commands:
pip install random2
pip install pygame
Project Structure
We will follow the below steps to build the Ludo Game:
1. Import the required modules
2. Creating a class each for the tokens and the grids
3. Creating global variables
4. Creating all the token and grid objects
5. Writing function to draw grid
6. Writing functions to move the tokens
7. Function to Move the Computer Token Automatically
8. Writing functions to check id, current player and collision of two players
9. Writing the function to get next player
10. Writing the main game
11. Creating the main window
1. Import the required modules
import pygame import random from pygame.locals import * import sys from pygame import font import time
Code explanation:
a. Pygame module helps in building in window and controls for the ludo game
b. Random is used to get a number on rolling the dice
c. Pygame.locals imports all the constants in Pygame
2. Creating a class each for the tokens and the grids
class gridObj: def __init__(self, bgColor, playerList, safe, coordinate): self.bgColor = bgColor self.playerList = playerList self.safe = safe self.coordinate = coordinate class Token: def __init__(self, Id, color, state, coordinate,size): self.Id = Id self.color = color self.state = state self.coordinate = coordinate self.size=size self.original_coordinate = coordinate
Code explanation:
a. The gridObj() class is for each grid on the window that decides the color at that particular coordinate on the window. And also holds the information about the player at the location and if that location is safe for multiple players to land.
b. The Token() class creates a token for each of the four players. Each token holds the information about its color, radius state and location.
3. Creating global variables
cList = [(1, 6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 5), (6, 4), (6, 3), (6, 2), (6, 1), (6, 0), (7, 0), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (9, 6), (10, 6), (11, 6), (12, 6), (13, 6), (14, 6), (14, 7),(14, 8), (13, 8), (12, 8), (11, 8), (10, 8), (9, 8), (8, 9), (8, 10), (8, 11), (8, 12), (8, 13), (8, 14), (7, 14),(6, 14), (6, 13), (6, 12), (6, 11), (6, 10), (6, 9), (5, 8), (4, 8), (3, 8), (2, 8), (1, 8), (0, 8), (0, 7),(0, 6)] #RGBY initPos=[[[1,1],[1,4],[4,1],[4,4]],[[10,1],[13,1],[10,4],[13,4]],[[1,10],[4,10],[1,13],[4,13]],[[10,10],[10,13],[13,10],[13,13]]] pnames=['R','G','B','Y'] height = 1000 width = 800 initx = 0 inity = 0 currentPlayer = 'R' compTokensLoc=[[1,1],[1,4],[4,1],[4,4]] n=2 withComputer=False dice_clicked = False move_list = [] diceValue = 6 Game_grid = [[-1 for _ in range(15)] for _ in range(15)] colors=['white','red','green','yellow','blue','black'] colorMatrix = [[-1, -1, -1, -1, -1, -1, 0, 0, 0, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, 0, 2, 2, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, 2, 2, 0, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, 0, 2, 0, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, 0, 2, 0, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, 0, 2, 0, -1, -1, -1, -1, -1, -1], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0], [0, 1, 1, 1, 1, 1, 0, 5, 0, 4, 4, 4, 4, 4, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0], [-1, -1, -1, -1, -1, -1, 0, 3, 0, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, 0, 3, 0, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, 0, 3, 0, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, 0, 3, 3, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, 3, 3, 0, -1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1, 0, 0, 0, -1, -1, -1, -1, -1, -1]] coordinateMatrix = [[[initx + i * 50, inity + j * 50] for i in range(0, 15)] for j in range(0, 15)] safeLocs=[[6,2],[8,1],[12,6],[13,8],[8,12],[6,13],[2,8],[1,6]] safeMatrix = [[0 for i in range(15)] for j in range(15)] for i in safeLocs: safeMatrix[i[0]][i[1]] = 1 diceFaces = {1: [[0, 0, 0], [0, 1, 0], [0, 0, 0]], 2: [[0, 1, 0], [0, 0, 0], [0, 1, 0]], 3: [[0, 1, 0], [0, 1, 0], [0, 1, 0]], 4: [[1, 0, 1], [0, 0, 0], [1, 0, 1]], 5: [[1, 0, 1], [0, 1, 0], [1, 0, 1]], 6: [[1, 0, 1], [1, 0, 1], [1, 0, 1]], }
Code explanation:
a. In this step we create the global variables that can be used while creating and running the game
b. The cList holds the center locations of the grid and the initPos holds the initial positions of the tokens
c. The pnames variable has the names of the tokens, height and width decide the size of the game window. And the initx, inity are the initial x and y coordinates
d. The variable compTokensLoc holds the locations of the four tokens in case the game mode is with Computer. This will help in tracking the location of computer’s tokens for moving them automatically.
e. The currentPlayer holds the name of the player playing the game, the dice_clicked is a boolean variable that stores if the user pressed a mouse button. And the diceValue is the value of the dice showing on rolling.
f. The variable ‘n’ holds the number of players being played based on the user’s choice and the ‘withComputer’ variable is True when the game is being played with the computer else False.
g. The Game_grid is the mail game window, colorMatrix sets the color to the locations on the grid based on the colors in the colors list.
h. The list safeLocs is the locations where multiple tokens can stay. And these locations are set 1 in the safeMatrix
i. Finally, the diceFaces helps in showing the dice values on the screen. For example, for showing 1 we need a dot in the middle. So, the corresponding 3×3 matrix is [[0, 0, 0], [0, 1, 0], [0, 0, 0]], with 1 in the middle of the matrix.
4. Creating all the token and grid objects
for i in range(15): for j in range(15): ob = gridObj(colors[colorMatrix[i][j]], [], safeMatrix[i][j], coordinateMatrix[i][j]) Game_grid[i][j] = ob for j in range(4): for i in range(1,5): p=initPos[j][i-1] R= Token(pnames[j]+str(i), colors[j+1], 0, (50 *p[0] , 50 * p[1]), 20) Game_grid[p[0]][p[1]].playerList.append(R)
Code explanation:
a. Now we will create the 15×15 grid objects for the window using the loop with the color and other properties decided by the global variables above.
b. Then we create the 16 tokens, 4 of each color, with the attributes name, color, size and position.
5. Writing function to draw grid
def drawGrid(): global Game_grid newSurface = pygame.display.set_mode((height, width)) newSurface.fill('bisque') for i in range(15): for j in range(15): pygame.draw.rect(newSurface, Game_grid[i][j].bgColor, tuple(Game_grid[i][j].coordinate + [50, 50])) pygame.draw.rect(newSurface, (0, 0, 0), tuple(Game_grid[i][j].coordinate + [50, 50]), 1) # always constant pygame.draw.rect(newSurface, colors[1], (initx, inity, 300, 300)) pygame.draw.rect(newSurface, colors[0], (initx + 50, inity + 50, 200, 200)) pygame.draw.rect(newSurface, colors[2], (initx + 450, inity, 300, 300)) pygame.draw.rect(newSurface, colors[0], (initx + 500, inity + 50, 200, 200)) pygame.draw.rect(newSurface, colors[3], (initx, inity + 450, 300, 300)) pygame.draw.rect(newSurface, colors[0], (initx + 50, inity + 500, 200, 200)) pygame.draw.rect(newSurface, colors[4], (initx + 450, inity + 450, 300, 300)) pygame.draw.rect(newSurface, colors[0], (initx + 500, inity + 500, 200, 200)) for i in range(15): for j in range(15): relativeToken(Game_grid[i][j].playerList, i * 50, j * 50) for k in Game_grid[i][j].playerList: c = k.coordinates pygame.draw.circle(newSurface, k.color, (c[0] + 25, c[1] + 25), k.size) pygame.draw.circle(newSurface, colors[-1], (c[0] + 25, c[1] + 25), k.size, 1) # highlight if k.Id[0] == currentPlayer: pygame.draw.circle(newSurface, colors[0], (c[0] + 25, c[1] + 25), k.size - 2, 2) # chess_faces face = diceFaces[diceValue] for i in range(3): for j in range(3): pygame.draw.rect(newSurface, 'black', ((0 + 800) + (50 * j), (0 + 300) + (50 * i), 50, 50)) if face[i][j] == 1: cIndex=pnames.index(currentPlayer)+1 pygame.draw.circle(newSurface, colors[cIndex], ((0 + 800) + (50 * j) + 25, (0 + 300) + (50 * i) + 25), 10) pygame.draw.rect(newSurface, colors[pnames.index(currentPlayer)+1], ((0 + 798), (0 + 298), 150, 150), 4) return newSurface
Code explanation:
a. This function is used to draw the grid and this executes in the main while loop to show the updates on the screen.
b. In this, we first create the surface of the predefined height and width. Then, we place the grid objects created in the previous step.
c. After this, we draw the boxes that hold the tokens on the 4 corners and then the 16 tokens
d. Finally, we draw the dice
6. Writing functions to move the tokens
def move(initPos, value, current): i = 0 j = -1 flag = 0 while True: if cList[i] == initPos or j >= 0: if current == 'R' and i == 50: flag = 1 if current == 'G' and i == 11: flag = 2 if current == 'B' and i == 37: flag = 3 if current == 'Y' and i == 24: flag = 4 j += 1 if j == value: break i = (i + 1) % len(cList) if flag == 1: return (cList[i][0] + 1, cList[i][1] + 1) elif flag == 2: return (cList[i][0] + 1, cList[i][1] + 1) elif flag == 3: return (cList[i][0] + 1, cList[i][1] - 1) elif flag == 4: return (cList[i][0] - 1, cList[i][1] - 1) else: return (cList[i][0], cList[i][1]) def relativeToken(pList, x, y): l = len(pList) relRad = int((2 / (l + 1)) * 20) relpt = [] j = 0 if l % 2 == 0: l1 = [i + 1 for i in range((l // 2))] l2 = [i - 1 for i in range((l // 2))] relpt = l2[::-1] + l1 else: l1 = [i + 1 for i in range((l // 2))] l2 = [i - 1 for i in range((l // 2))] relpt = l2[::-1] + [0] + l1 for p in pList: p.size = relRad p.coordinates = ((x) + (relpt[j] * (relRad // 2)), (y)) j += 1
Code explanation:
a. In the function move(), we control the movement based on the value on the dice, current position of the respective token and the set of rules.
b. And the function relativeToken() returns the relative size and position based on the coordinates
7. Writing functions to check id, current player and collision of two players
def check(pos): if pos in cList: return True else: return False def gridlocation(pos): x = pos[0] y = pos[1] return (x // 50, y // 50) def checkCollision(pList): global currentPlayer global Game_grid new_list=[] for p in pList: if p.Id[0] == currentPlayer: new_list.append(p) else: p.coordinates=p.original_coordinate i=p.coordinates[0]//50 j=p.coordinates[1]//50 Game_grid[i][j].playerList.append(p) return new_list def checkId(pList): global currentPlayer for i in pList: if i.Id[0] == currentPlayer: return True return False
Code explanation:
a. The function check() returns if the position of the token is in the cList
b. The gridlocation() function makes sure the token moves within the grid and gives the corresponding position
c. checkCollision() function check if two token are ending up in the same grid location
d. checkId() checks which token of the current player is to be moved
8. Function to Move the Computer Token Automatically
def compLoc(diceValue): global compTokensLoc saveLocs=[(1, 7),(2, 7),(3, 7),(4, 7),(5, 7),(6, 7),(7, 7)] inits=[] players=[] if(compTokensLoc[0]==[1,1]): inits.append(0) else: players.append(0) if(compTokensLoc[1]==[1,4]): inits.append(1) else: players.append(1) if(compTokensLoc[2]==[4,1]): inits.append(2) else: players.append(2) if(compTokensLoc[3]==[4,4]): inits.append(3) else: players.append(3) if(diceValue==6 and len(inits)>0): tkn=random.randint(1,len(inits)) compTokensLoc[tkn-1]=cList[0] return compTokensLoc[tkn-1] cnt=len(compTokensLoc)-len(inits) print(inits) if(cnt<=0): return (1,1) if(cnt>0): tkn=random.randint(1,cnt) tkn=players[tkn-1] print(tkn) ind=cList.index(compTokensLoc[tkn]) if(compTokensLoc[tkn] in saveLocs): ind=saveLocs.index(compTokensLoc[tkn]) if((ind+diceValue) <=(len(saveLocs-1))): compTokensLoc[tkn]=saveLocs[ind+diceValue] return compTokensLoc[tkn] else: compLoc(diceValue) elif(ind+diceValue<len(cList)-1): compTokensLoc[tkn]=cList[ind+diceValue] else: stepsLeft= diceValue-(len(cList)-1) compTokensLoc[tkn]=saveLocs[stepsLeft-1] return compTokensLoc[tkn]
Code explanation:
a. In this, we check the number of the tokens at the initial positions and save them in the list inits and the others in players.
b. If the dice value is 6 then we move one of the tokens at the initial position, if there is any. If not, we move one of the 4 tokens.
c. While moving one of the tokens, we move them across the track using the cList to get the position to move to.
d. When the computer token (red token) is done moving on the main track, it moves across the path that leads to the center following the positions in the list saveLocs.
9. Writing the Function to get the Next Player
def nextPlayer(): global n,currentPlayer if(n==2): if currentPlayer == 'R': currentPlayer = 'B' elif currentPlayer == 'B': currentPlayer = 'R' elif(n==3): if currentPlayer == 'R': currentPlayer = 'G' elif currentPlayer == 'G': currentPlayer = 'Y' elif currentPlayer == 'Y': currentPlayer = 'R' elif(n==4): if currentPlayer == 'R': currentPlayer = 'G' elif currentPlayer == 'G': currentPlayer = 'Y' elif currentPlayer == 'Y': currentPlayer = 'R' elif currentPlayer == 'B': currentPlayer = 'R'
Code explanation:
a. In this, based on the number of players, we switch from one player to another.
b. If the number of players is 2, the switching is between ‘R’, and ‘B. If 3, then ‘R’, ‘G’, and ‘G’. And If all the four players are playing, then switch between all the 4 of them.
10. Writing the main game
global DISPLAYSURF def mainGame(): height = 1000 width = 800 pygame.init() DISPLAYSURF = pygame.display.set_mode((height, width)) pygame.display.set_caption('PythonGeeks Ludo Game') global cList, initPos,pnames,initx,inity,currentPlayer,n,dice_clicked,move_list,diceValue,Game_grid,colors,colorMatrix, coordinateMatrix,safeMatrix,safeLocs,diceFaces font = pygame.font.SysFont("Calibri", 30,'bold') label = font.render("LUDO GAME", 1, 'black') while (True): for event in pygame.event.get(): if(withComputer and currentPlayer=='R') or event.type == MOUSEBUTTONDOWN: if(withComputer and currentPlayer=='R'): loc=compLoc(random.randint(1, 6)) dice_clicked == True elif event.type == MOUSEBUTTONDOWN: loc = gridlocation(event.pos) if (loc[0] >= 16 and loc[0] <= 18 and loc[1] >= 6 and loc[1] <= 8 and dice_clicked == False): # dice_value = 6 diceValue = random.randint(1, 6) print("dice clicked", currentPlayer) dice_clicked = True if diceValue != 6 and dice_clicked == True: print(1) flag = 0 for i in cList: for p in Game_grid[i[0]][i[1]].playerList: if p.Id[0] == currentPlayer: flag = 1 if flag == 0: nextPlayer() dice_clicked = False if currentPlayer == 'R' and diceValue == 6 and (loc in [(1, 1), (4, 1), (4, 4), (1, 4)]) and dice_clicked == True: print(2) print(Game_grid[1][6].playerList) Game_grid[1][6].playerList.append(Game_grid[loc[0]][loc[1]].playerList[0]) Game_grid[1][6].playerList[-1].coordinates = (50 * 1, 50 * 6) for p in Game_grid[1][6].playerList: print(p.coordinates) Game_grid[loc[0]][loc[1]].playerList = [] dice_clicked = False elif currentPlayer == 'G' and diceValue == 6 and (loc in [(10, 1), (13, 1), (13, 4), (10, 4)]) and dice_clicked == True: print(3) print(Game_grid[8][1].playerList) Game_grid[8][1].playerList.append(Game_grid[loc[0]][loc[1]].playerList[0]) Game_grid[8][1].playerList[-1].coordinates = (50 * 8, 50 * 1) Game_grid[loc[0]][loc[1]].playerList = [] print(Game_grid[8][1].playerList[0].Id) dice_clicked = False elif currentPlayer == 'B' and diceValue == 6 and (loc in [(1, 10), (4, 10), (4, 13), (1, 13)]) and dice_clicked == True: print(5) print(Game_grid[6][13].playerList) Game_grid[6][13].playerList.append(Game_grid[loc[0]][loc[1]].playerList[0]) Game_grid[6][13].playerList[-1].coordinates = (50 * 6, 50 * 13) Game_grid[loc[0]][loc[1]].playerList = [] dice_clicked = False elif currentPlayer == 'Y' and diceValue == 6 and (loc in [(10, 10), (13, 10), (13, 13), (10, 13)]) and dice_clicked == True: print(4) print(Game_grid[13][8].playerList) Game_grid[13][8].playerList.append(Game_grid[loc[0]][loc[1]].playerList[0]) Game_grid[13][8].playerList[-1].coordinates = (50 * 13, 50 * 8) Game_grid[loc[0]][loc[1]].playerList = [] dice_clicked = False elif currentPlayer == 'R' and (loc in [(1, 7), (2, 7), (3, 7), (4, 7), (5, 7)]) and len( Game_grid[loc[0]][loc[1]].playerList) > 0 and dice_clicked == True: if loc[0] + diceValue <= 6: Game_grid[loc[0] + diceValue][loc[1]].playerList.append(Game_grid[loc[0]][loc[1]].playerList[-1]) Game_grid[loc[0] + diceValue][loc[1]].playerList[-1].coordinates = ( 50 * (loc[0] + diceValue), 50 * (loc[1])) Game_grid[loc[0]][loc[1]].playerList = Game_grid[loc[0]][loc[1]].playerList[:-1] dice_clicked = False elif currentPlayer == 'G' and (loc in [(7, 1), (7, 2), (7, 3), (7, 4), (7, 5)]) and len( Game_grid[loc[0]][loc[1]].playerList) > 0 and dice_clicked == True: if loc[1] + diceValue <= 6: Game_grid[loc[0]][loc[1] + diceValue].playerList.append(Game_grid[loc[0]][loc[1]].playerList[-1]) Game_grid[loc[0]][loc[1] + diceValue].playerList[-1].coordinates = ( 50 * (loc[0]), 50 * (loc[1] + diceValue)) Game_grid[loc[0]][loc[1]].playerList = Game_grid[loc[0]][loc[1]].playerList[:-1] dice_clicked = False elif currentPlayer == 'B' and (loc in [(7, 9), (7, 10), (7, 11), (7, 12), (7, 13)]) and len( Game_grid[loc[0]][loc[1]].playerList) > 0 and dice_clicked == True: if loc[1] + diceValue >= 8: Game_grid[loc[0]][loc[1] + diceValue].playerList.append(Game_grid[loc[0]][loc[1]].playerList[-1]) Game_grid[loc[0]][loc[1] + diceValue].playerList[-1].coordinates = ( 50 * (loc[0]), 50 * (loc[1] + diceValue)) Game_grid[loc[0]][loc[1]].playerList = Game_grid[loc[0]][loc[1]].playerList[:-1] dice_clicked = False elif currentPlayer == 'Y' and (loc in [(9, 7), (10, 7), (11, 7), (12, 7), (13, 7)]) and len( Game_grid[loc[0]][loc[1]].playerList) > 0 and dice_clicked == True: if loc[0] - diceValue >=8: Game_grid[loc[0] - diceValue][loc[1]].playerList.append(Game_grid[loc[0]][loc[1]].playerList[-1]) Game_grid[loc[0] - diceValue][loc[1]].playerList[-1].coordinates = ( 50 * (loc[0] - diceValue), 50 * (loc[1])) Game_grid[loc[0]][loc[1]].playerList = Game_grid[loc[0]][loc[1]].playerList[:-1] dice_clicked = False elif (check(loc)) and checkId(Game_grid[loc[0]][loc[1]].playerList) and dice_clicked == True: print(6) newpos = move(loc, diceValue, currentPlayer) new_list = [] flg = 0 for i in Game_grid[loc[0]][loc[1]].playerList: if i.Id[0] == currentPlayer and flg == 0: Game_grid[newpos[0]][newpos[1]].playerList.append(i) Game_grid[newpos[0]][newpos[1]].playerList[-1].coordinates = (50 * newpos[0], 50 * newpos[1]) #eating pieces Game_grid[newpos[0]][newpos[1]].playerList=checkCollision(Game_grid[newpos[0]][newpos[1]].playerList) flg = 1 else: new_list.append(i) Game_grid[loc[0]][loc[1]].playerList = new_list dice_clicked = False if diceValue != 6: nextPlayer() # DISPLAYSURF.blit(drawGrid(), (0, 0)) # pygame.display.update() font1 = pygame.font.SysFont("Calibri", 50) label1 = font1.render(str(diceValue), 1, 'black') DISPLAYSURF.blit(drawGrid(), (0, 0)) DISPLAYSURF.blit(label, (800, 10)) DISPLAYSURF.blit(label1, (850, 500)) pygame.display.update() if event.type == QUIT: pygame.quit() sys.exit()
Code explanation:
a. This part of the code controls the whole game with the help of the program we wrote in the previous steps
b. In this, first we create the game window. Then run the code in an infinite while loop that runs till the exit button is pressed by the user.
c. We check if the user presses the button and then we get a random diceValue.
d. And if the player is playing with the computer, the dice value is randomly choosen when computer’s chance arrives. And use the compLoc() function to get the location where the token needs to be moved.
e. After this, we check if the dice value is 6 and if the token selected is in the starting position. Based on that we move the corresponding token to the main track.
f. And if the above conditions are not met, then we check if the token is in the track area that points to the center final location and move the token if the dice value is valid.
g. If not, then we move the token normally on the main track. After this, we give the chance to the next player to roll the dice.
h. Finally, we update the whole grid by calling drawGrid() function. And then adding labels to show the value on the dice
i. And if the user presses the exit button, then we close the game.
11. Code of the Main Window
pygame.init() screen = pygame.display.set_mode((1000, 600)) pygame.display.set_caption('PythonGeeks Ludo Game') clock = pygame.time.Clock() font = pygame.font.SysFont("Arial", 20) class Button: """Create a button, then blit the surface in the while loop""" def __init__(self, text,num,comp,pos, font, bg="blue", feedback=""): self.x, self.y = pos self.font = pygame.font.SysFont("Arial", font) self.no=num self.text=text self.text = self.font.render(self.text, 1, pygame.Color("White")) self.cmp=comp self.change_text(bg) def change_text(self, bg="blue"): self.size = self.text.get_size() self.surface = pygame.Surface(self.size) self.surface.fill(bg) self.surface.blit(self.text, (0, 0)) self.rect = pygame.Rect(self.x, self.y, self.size[0], self.size[1]) def show(self): screen.blit(self.text , (self.x, self.y)) def click(self, event): x, y = pygame.mouse.get_pos() font1 = pygame.font.SysFont("Calibri", 50) label1 = font1.render("PythonGeeks Ludo Game", 1, 'yellow') screen.blit(label1, (230, 30)) if event.type == pygame.MOUSEBUTTONDOWN: if pygame.mouse.get_pressed()[0]: if self.rect.collidepoint(x, y): global n,withComputer n=self.no withComputer=self.cmp print(n,",",withComputer) pygame.quit() mainGame() def mainloop(): """ The infinite loop where things happen """ while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() button1.click(event) button2.click(event) button3.click(event) button4.click(event) button1.show() button2.show() button3.show() button4.show() clock.tick(30) pygame.display.update() button1 = Button( "Play with Computer",2,True, (400, 150), font=30, bg="navy") button2 = Button( "2 Players",2,False, (400, 250), font=30, bg="navy") button3 = Button( "3 Players",3,False, (400, 350), font=30, bg="navy",) button4 = Button( "4 Players",4,False, (400, 450), font=30, bg="navy",) mainloop()
Code explanation:
a. In this we create a window and add the buttons
b. A Button class is created to name the buttons, place them on screen based on the position given. And then run the respective function based on the button clicked by modifying n, withComputer, and running mainGame() function.
c. The click of any of the buttons is checked continuously using a while loop.
Python Ludo Game Output
Summary
Congratulations! We have successfully built the Ludo game using Python. In this project, we got to deal with the random and Pygame modules of Python. Hope this project was helpful. What are you waiting for? Pull your friends and family members to play the game!
Sir muje apna ludu gem banana he
There us no logic implemented to win the game.
The peice keeps on moving in the same circle even if the peice travels through the board.
Escod
Code sahiye