Snake & Ladder Game in Python

I’ve had countless hours working on this university project, to develop a simple python snake and ladder game. I probably wont touch python once this semester is finished, but I rather post it up here for my own reference and to help others to learn how to speak python!

I believe publishing codes as GPLv3 is best way to help all other beginners save time and learn basics of python. I’ve spend hours and hours searching in Google to learn about functions and how to properly use them in Python, so you can instantly save all those hours and learn how to code basic GUI in python using tkinter’s grid and how to properly use arrays, while loops and for loops. It even goes as deep as implementing a multi-threaded timer!

Here you go. Learn first, thank me later: (UPDATE: After publishing this, I noticed WordPress is removing python’s tabbed spaces before each line, so below code don’t work out of box. if you wish to have code with correct intents, use this link instead)
#License: GPLv3
#Developed using Python 3.4.2

#Load tkinter module. it is available by default by python and we just need to import it
from tkinter import *

#this is used for color cycle when player reach end of the game
import threading

#try to load PIL (Pillow) module if it is available and drop an error if it is not available
try:
from PIL import ImageTk, Image
except:
print(“Python PIL package not found. Please install it to be able load images correctly.”)
import random

def colorCycle():
global PlayerMovesLabel, time_elapsed
#put it inside try, so that if in middle of color switching, main thread is closed, it don’t crash
try:
#assign our timer
mytimer = threading.Timer(0.2, colorCycle)
#link timer to main thread, so that if main thread is closed, timer get stopped
mytimer.daemon = True
#start timer
mytimer.start()

#if player is in position 50, start the cycle
if player_1_pos==50:
randomColor()
else:
#otherwise keep background white
PlayerMovesLabel.config(bg=’white’)

time_elapsed=time_elapsed+1
except:
#if error, do nothing. error means program has been exited but thread is running for one more last time.
return

#get our old and new player positions
old_player_pos=player_1_pos
new_player_pos=player_1_pos+dice_num

#if there is snake bite or ladder climb, show feedback message
additional_message=””

#code to take care when move is more than 50
if new_player_pos>50:
new_player_pos=50-(new_player_pos-50)

#detect if player go to snake hole
#enumerate snake holes to get their Index ID and
#go through list of snake holes
for idx,val in enumerate(SNAKE_HOLES):
#in our array, first value (even index number) is snake head, and second value (odd index number) is snake tail, detect head
if idx % 2 == 0:
#if it is head, and player is on head
if new_player_pos==SNAKE_HOLES[idx]:
#move player to tail number
new_player_pos=SNAKE_HOLES[idx+1]
#Update player bites counter
player_bites=player_bites+1
additional_message=”Bitten | ”

#detect if player go to ladder bottom
#enumerate ladder bridges to get their Index ID and
#go through list of ladders
for idx,val in enumerate(LADDER_BRIDGES):
#in our array, first value (even index number) is ladder bottom, and second value (odd index number) is ladder top, detect bottom
if idx % 2 == 0:
#if it is ladder bottom, and player is on bottom of ladder
if new_player_pos==LADDER_BRIDGES[idx]:
#climb the ladder!
new_player_pos=LADDER_BRIDGES[idx+1]
#Update player climb counter
player_climb=player_climb+1
additional_message=”Climb | ”

#change old position to white
if old_player_pos>0:
grid_array[old_player_pos-1].config(bg=”white”)

#change new position to yellow
grid_array[new_player_pos-1].config(bg=”yellow”)

#apply change to player position variable
player_1_pos=new_player_pos
player_moves=player_moves+1