Number Game in Python for my daughter.

This is a game I developed for my 5 year old daughter. I used to play this when I was a kid, now with this she plays it online. Thanks to codesculptor. Its an awesome platform for using python online! I intend to develop more games for her on this platform.

Play the game by clicking the link..

Numbers Game for Kids on CodeSculptor!

Here’s how it plays.

Here’s the code:


# Numbers June 19th 2013
# For my daughter... She loves playing this.
# Sukhbinder Singh
#
# Goal: To arrange the numbers in the grid in
# the minimum moves possible.

# You the left/right/up/down arrow keys.

import simplegui
import random
x= range(0,16)
y=range(1,16)
y.append(0)

am =range(3,16,4)
random.shuffle(x)
moves=0
choice =0
win = False
def getRect(x, y, w, h):
 return [[x, y],[(x + w), y],[(x + w), (y + h)],[x, (y + h)]]

def pt(x):
 for i in range(0,len(x),4):
 print x[i],x[i+1],x[i+2],x[i+3]
def checkwin():
 global x,y
 return x == y

def moveit(ch):
 # 8
 # 4 6
 # 2
 global x,y,win,moves
 if win:
 return
 if ch == 2: m = -4
 if ch == 6: m = -1
 if ch == 8: m = 4
 if ch == 4: m = 1

 ind = x.index(0)
 if((ind in am) and (ch == 4)): m=0
 if((ind % 4 == 0) and (ch == 6)): m=0
 s = ind+m
 if ((s < 0) or (s > 15)) : return None

 temp=x[s]
 x[s]=x[ind]
 x[ind]=temp
 moves +=1
 win=checkwin()
 #pt(x)
 if(win):
 print "You completed in "+str(moves)

def clicked(key):
 if(key==simplegui.KEY_MAP["left"]): ch = 4
 if(key==simplegui.KEY_MAP["right"]): ch = 6
 if(key==simplegui.KEY_MAP["up"]): ch = 8
 if(key==simplegui.KEY_MAP["down"]): ch =2
 moveit(ch)

def drawrect(canvas):
 for i in range(4):
 for j in range(4):
 canvas.draw_polygon(getRect(25+i*50, 20+j*50, 50, 50), 2, "aqua")

# Handler to draw on canvas
def draw(canvas):
 global x
 left=40
 top=50
 drawrect(canvas)
 k=0
 for i in range(4):
 for j in range(4):
 if x[k] !=0 :canvas.draw_text(str(x[k]), (left+j*50, top+i*50), 20, "Orange")
 k +=1

canvas.draw_text("Moves: "+str(moves), (25, 260), 20, "lightgreen")

# Create a frame and assign callbacks to event handlers
frame = simplegui.create_frame("Numbers", 300, 300,10)
frame.set_draw_handler(draw)
frame.set_keydown_handler(clicked)
frame.start()

&nbsp;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s