Hey! Welcome back to my blog!
In this blog we will learn to create our one of the favorite childhood game "SNAKES AND LADDERS". I hope everyone is aware about this game. I still remember I used to play this game during my summer vacation and perhaps those were the best days.
Okay, let's start python coding for the same game!
So for this particular piece of code, I have assumed the following game board and according to that numbers are assigned to the snakes and ladders.
---------------------------------------------------------------------------------------------------------------
from PIL import Image
import random
end=100
def show_board():
img=Image.open('C:\\Users\\asus01\\Desktop\\slb.jpg')
img.show()
def check_ladder(points):
if points==3:
print("Ladder")
return 51
elif points==6:
print("Ladder")
return 27
elif points==20:
print("Ladder")
return 70
elif points==36:
print("Ladder")
return 55
elif points==63:
print("Ladder")
return 95
elif points==68:
print("Ladder")
return 100
else:
#Not a ladder
return points
def check_snake(points):
if points==25:
print("Snake")
return 5
elif points==34:
print("Snake")
return 1
elif points==47:
print("Snake")
return 19
elif points==65:
print("Snake")
return 52
elif points==87:
print("Snake")
return 57
elif points==91:
print("Snake")
return 61
elif points==99:
print("Snake")
return 69
else:
return points
def reached_end(points):
if points==end:
return True
else:
return False
def play():
#input plYER 1 NAME
p1_name=input("Player 1, Enter your name: ")
#input player 2 name
p2_name=input("Player 2, Enter your name: ")
#player 1points
pp1=0
#player 2 points
pp2=0
turn=0
while(1):
if turn%2==0:
#player 1 turn
print(p1_name,'Your turn: ')
#ask player's choice to continue
c=input("Press 1 to continue or 0 to quit:")
if c==0:
print(p1_name,'scored',pp1)
print(p2_name,'scored',pp2)
print("Quitting the game thanks!")
break
#roll a die 1,2,3,4,5,6,
dice=random.randint(1,6)
print('Dice showed: ',dice)
#add points
pp1=pp1+dice
pp1=check_ladder(pp1)
pp1=check_snake(pp1)
#check if the playe goes beyond the board
if pp1>end:
pp1=end
print(p1_name,'Your score',pp1)
if reached_end(pp1):
print(p1_name,"WON")
break
else:
#player 2 turn
print(p2_name,'Your turn: ')
#ask player's choice to continue
c=input("Press 1 to continue or 0 to quit:")
if c==0:
print(p1_name,'scored',pp1)
print(p2_name,'scored',pp2)
print("Quitting the game thanks!")
break
#roll a die 1,2,3,4,5,6,
dice=random.randint(1,6)
print('Dice showed: ',dice)
#add points
pp2=pp2+dice
pp2=check_ladder(pp2)
pp2=check_snake(pp2)
#check if the playe goes beyond the board
if pp2>end:
pp2=end
print(p2_name,'Your score',pp2)
if reached_end(pp2):
print(p2_name,"WON")
break
turn=turn+1
show_board()
play()
Comments
Post a Comment