0% found this document useful (0 votes)
13 views5 pages

Dsa Mini Project

The document outlines a mini project for implementing a Snake and Ladders game using Python and the Tkinter library for GUI. It describes the game's board setup, player mechanics, and how snakes and ladders are represented visually, along with the code implementation. The game allows two players to take turns rolling a dice and moving their tokens until one reaches the end of the board to win.

Uploaded by

ishabachhav2410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Dsa Mini Project

The document outlines a mini project for implementing a Snake and Ladders game using Python and the Tkinter library for GUI. It describes the game's board setup, player mechanics, and how snakes and ladders are represented visually, along with the code implementation. The game allows two players to take turns rolling a dice and moving their tokens until one reaches the end of the board to win.

Uploaded by

ishabachhav2410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Design a mini project to implement Snake and Ladders Game using python.

Implementation Overview
The Snake and Ladder game is implemented in Python using the Tkinter library for GUI
development. The game board is designed as a 10×10 grid using a canvas, with each cell
numbered from 1 to 100 in a zig-zag manner. Snakes and ladders are represented using
dictionaries that map start and end positions. Visually, ladders are drawn as green straight
lines, while snakes are shown as red wavy curves for a more realistic effect. Player
movements are controlled via a dice roll, and tokens are dynamically updated on the board.
The game supports two players taking alternate turns until one reaches position 100 to win.
Interactive elements such as pop-up messages for dice rolls, snake bites, and ladder climbs
enhance user engagement.

Code :
import tkinter as tk
from tkinter import messagebox
import random
# Snakes and Ladders positions
snakes = {16: 6, 48: 30, 64: 60, 79: 19, 93: 68, 95: 24, 97: 76, 98: 78}
ladders = {1: 38, 4: 14, 9: 31, 21: 42, 28: 84, 36: 44, 51: 67, 71: 91, 80: 100}
class Player:
def __init__(self, name, color):
self.name = name
self.position = 0
self.color = color
class SnakeLadderGame:
def __init__(self, root):
self.root = root
self.root.title("Snake and Ladders Game")
self.root.geometry("600x700")
self.canvas = tk.Canvas(root, width=500, height=500, bg='white')
self.canvas.pack(pady=20)
self.status_label = tk.Label(root, text="", font=("Arial", 14))
self.status_label.pack()
self.roll_button = tk.Button(root, text="Roll Dice", font=("Arial", 14),
command=self.play_turn)
self.roll_button.pack(pady=10)
self.players = [Player("Player 1", "red"), Player("Player 2", "blue")]
self.current_turn = 0
self.player_tokens = {}
self.draw_board()
self.draw_snakes_and_ladders()
self.init_tokens()
self.update_status()
def draw_board(self):
size = 50
for row in range(10):
for col in range(10):
x1 = col * size
y1 = row * size
x2 = x1 + size
y2 = y1 + size
number = 100 - (row * 10 + col) if row % 2 == 0 else 100 - (row * 10 + (9 - col))
self.canvas.create_rectangle(x1, y1, x2, y2, fill="lightyellow", outline="black")
self.canvas.create_text(x1 + 25, y1 + 25, text=str(number), font=("Arial", 10))
def get_coords(self, position):
if position < 1 or position > 100:
return -100, -100
size = 50
pos = position - 1
row = 9 - (pos // 10)
col = pos % 10 if ((pos // 10) % 2 == 0) else 9 - (pos % 10)
x = col * size + 25
y = row * size + 25
return x, y
def draw_snakes_and_ladders(self):
# Draw ladders (green arrows)
for start, end in ladders.items():
x1, y1 = self.get_coords(start)
x2, y2 = self.get_coords(end)
self.canvas.create_line(x1, y1, x2, y2, fill="green", width=4, arrow=tk.LAST)
self.canvas.create_text((x1 + x2) // 2, (y1 + y2) // 2, text="🪜", font=("Arial", 16))

# Draw snakes (wavy red curves)


for head, tail in snakes.items():
x1, y1 = self.get_coords(head)
x2, y2 = self.get_coords(tail)
# Midpoints to make snake curvy
mid_x = (x1 + x2) // 2
curve_offset = 40
# Wavy effect using smooth line
points = [
x1, y1,
mid_x + curve_offset, (y1 + y2) // 2 - 20,
x2, y2
]
self.canvas.create_line(points, fill="red", width=4, smooth=True)
self.canvas.create_text((x1 + x2) // 2, (y1 + y2) // 2, text="🐍", font=("Arial", 16))
def init_tokens(self):
for player in self.players:
x, y = self.get_coords(player.position)
token = self.canvas.create_oval(x - 15, y - 15, x + 15, y + 15, fill=player.color)
self.player_tokens[player.name] = token
def move_token(self, player):
x, y = self.get_coords(player.position)
token = self.player_tokens[player.name]
self.canvas.coords(token, x - 15, y - 15, x + 15, y + 15)
def play_turn(self):
player = self.players[self.current_turn]
roll = random.randint(1, 6)
messagebox.showinfo("Dice Rolled!", f"{player.name} rolled a {roll}")
if player.position + roll <= 100:
player.position += roll
if player.position in snakes:
messagebox.showinfo("Oh no!", f"{player.name} got bitten by a snake 🐍!")
player.position = snakes[player.position]
elif player.position in ladders:
messagebox.showinfo("Yay!", f"{player.name} climbed a ladder 🪜!")
player.position = ladders[player.position]
self.move_token(player)
if player.position == 100:
messagebox.showinfo("Game Over", f"{player.name} wins!")
self.root.quit()
return
self.current_turn = (self.current_turn + 1) % len(self.players)
self.update_status()
def update_status(self):
current = self.players[self.current_turn]
self.status_label.config(text=f"{current.name}'s Turn ({current.color})")
# Run the game
if __name__ == "__main__":
root = tk.Tk()
game = SnakeLadderGame(root)
root.mainloop()

Snapshots :

You might also like