Example #1
0
def main():
    win = g.GraphWin('Moving Stuff Around', 1000,700)
    group = GGroup(g.Point(50,50))
    print group

    quitButt = g.Rectangle(g.Point(150,480),g.Point(320,430))
    quitButt.setFill('pink');quitButt.setOutline('red')
    quitButt.draw(win)

    quitText = g.Text(quitButt.getCenter(),"Clear & Quit")
    quitText.setSize(26); quitText.setFace('helvetica');quitText.setStyle('bold'); quitText.draw(win)

    circle = g.Circle(g.Point(50,50),5)
    circle2 = g.Circle(g.Point(34,44),52)
    text = g.Text(g.Point(9,20),"WASSAP!")   

    group.addElement(circle)
    print group
    group.addElement(circle2)
    print group  
    group.addElement(text)
    print group

    group.draw(win)

    group2 = group.clone()
    print group2
    group2.addElement(g.Text(g.Point(0,0),'Clone1, Group2'))
    group2.move(500,100)
    group2.draw2(win)

    group3 = group.clone()
    group3.addElement(g.Text(g.Point(0,0),'Clone2, Group3'))
    group3.move(0,300)
    group3.draw2(win)

    first = True
    while True:
        click = win.getMouse()
        if inButton(click,quitButt):
            group.undraw()
            t.sleep(2)
            group2.move(300,300)
            t.sleep(2)
            group3.move(500,-200)
            t.sleep(1)
            win.close()
            break
        elif first:
            group.move(50,50)
            first = False
        else:
            group.move2point(click)
def main():
    
    # Creating Joyas                                                                                           
    joyas = ['star','circle','square','triangle','rombus','X']

    star = g.Polygon(g.Point(0,-25),g.Point(-25,25),g.Point(30,-5),g.Point(-30,-5),g.Point(25,25))
    star.setFill('orange')
    starG = GGroup(g.Point(0,0)); starG.addElement(star)

    circle = g.Circle(g.Point(0,0),32); circle.setFill('blue'); circle.setOutline('yellow')
    circleG = GGroup(g.Point(0,0)); circleG.addElement(circle)

    square = g.Rectangle(g.Point(-32,-32),g.Point(32,32)); square.setFill('dark green'); square.setOutline('white')
    squareG = GGroup(g.Point(0,0)); squareG.addElement(square)


    triangle = g.Polygon(g.Point(-26,29),g.Point(26,29),g.Point(0,-29))
    triangle.setFill('pink'); triangle.setOutline('red')
    triangleG = GGroup(g.Point(0,0)); triangleG.addElement(triangle)

    rombus = g.Polygon(g.Point(-32,0),g.Point(0,32),g.Point(32,0),g.Point(0,-32))
    rombus.setFill('beige'); rombus.setOutline('purple')
    rombusG = GGroup(g.Point(0,0)); rombusG.addElement(rombus)

    equis = g.Text(g.Point(0,0),'X'); equis.setFill('white'); equis.setStyle('bold'); equis.setSize(36)
    equisG = GGroup(g.Point(0,0)); equisG.addElement(equis)

    # Create Window and Tools
    winW = 820
    winH= 670
    
    win = g.GraphWin('Jewels', winW,winH)
    win.setBackground("dodger blue")
    groupTools = GGroup(g.Point(int(winW/2),int(winH/2)))

    print groupTools###############################################################################################

    quitButt = g.Rectangle(g.Point(winW-100,50),g.Point(winW-10,10))
    quitButt.setFill('pink');quitButt.setOutline('red')
    groupTools.addElement(quitButt)

    quitText = g.Text(quitButt.getCenter(),"Quit")
    quitText.setSize(26); quitText.setFace('helvetica');quitText.setStyle('bold')
    groupTools.addElement(quitText)
    
    gameTitle = g.Text(g.Point(int(winW/2),50), "Joyas")
    gameTitle.setSize(36); gameTitle.setFace('helvetica');gameTitle.setStyle('bold')
    groupTools.addElement(gameTitle)

    # Create Frame
    frame = g.Rectangle(g.Point(10,winH-10),g.Point(winW-10,100))
    frame.setFill("SpringGreen")
    groupTools.addElement(frame)

    # Frame line Vertical
    x = 10
    increase  = 80
    x += increase
    while x < winW:
        line = g.Line(g.Point(x,winH-10),g.Point(x,100))
        groupTools.addElement(line)
        print groupTools##############################################################################################
        x += increase

    # Frame line Horizontal
    y = 100
    y += increase
    while y < winH:
        line = g.Line(g.Point(10,y),g.Point(winW-10,y))
        groupTools.addElement(line)
        print groupTools##############################################################################################
        y += increase

    print groupTools##############################################################################################

    groupTools.draw(win)

    # Create Board
    board = {}
    for x in range(1,11):
        for y in range(1,8):
            joya = r.choice(joyas)
            board[str(x)+str(y)] = joya
            
            while True:
                repeatX = repeatY = False
                if x > 2:
                    if (board[str(x)+str(y)] == board[str(x-1)+str(y)] and board[str(x)+str(y)] == board[str(x-2)+str(y)]):
                        repeatX = False
                        repeatY = True
                    else:
                        repeatY = False
                        
                    while (board[str(x)+str(y)] == board[str(x-1)+str(y)] and board[str(x)+str(y)] == board[str(x-2)+str(y)]):
                        board[str(x)+str(y)] = randExcept(joyas,board[str(x)+str(y)])
                    
        
                if y > 2:
                    if (board[str(x)+str(y)] == board[str(x)+str(y-1)] and board[str(x)+str(y)] == board[str(x)+str(y-2)]):
                        repeatX = True
                        repeatY = False
                    else:
                        repeatX = False
                        
                    while (board[str(x)+str(y)] == board[str(x)+str(y-1)] and board[str(x)+str(y)] == board[str(x)+str(y-2)]):
                        board[str(x)+str(y)] = randExcept(joyas,board[str(x)+str(y)])
                    
                    
                if repeatX or repeatY:
                    continue
                else:
                    break
                
    print board,"Length = ", len(board)##############################################################################################

    #Draw Board
    boardObjects = {}
    for x in range(1,11):
        for y in range(1,8):
            joya = board[str(x)+str(y)]
            if joya == 'star':
                boardObjects[str(x)+str(y)] = starG.clone()
            elif joya == 'circle':
                boardObjects[str(x)+str(y)] = circleG.clone()
            elif joya == 'square':
                boardObjects[str(x)+str(y)] = squareG.clone()
            elif joya == 'triangle':
                boardObjects[str(x)+str(y)] = triangleG.clone()
            elif joya == 'rombus':
                boardObjects[str(x)+str(y)] = rombusG.clone()
            else:
                boardObjects[str(x)+str(y)] = equisG.clone()
            boardObjects[str(x)+str(y)].move(10+(increase/2)+(increase*(x-1)),100+(increase/2)+(increase*(y-1)))
            boardObjects[str(x)+str(y)].draw2(win)
    


    
    # getting clicks
    one = True

    while True:
        click = win.getMouse()

        # Quitting
        if inButton(click,quitButt):
            groupTools.undraw()
            t.sleep(1)
            for joya in boardObjects.values():
                joya.undraw()
                t.sleep(0.02)
            win.close()
            break
        # Check for clicks inside Fram
        elif not inButton(click,frame):
            continue
        else:

            # First Click
            if one:
                click1 = click
                x1 = 1+(click1.getX()-10)/increase
                y1 = 1+(click1.getY()-100)/increase

                print "X1",x1,"Y1",y1##############################################################################################

                one = False
                boardObjects[str(x1)+str(y1)].undraw()
                square1 =g.Rectangle(g.Point(10+((x1-1)*increase),100+((y1-1)*increase)),g.Point(10+increase+((x1-1)*increase),100+increase+((y1-1)*increase)))
                square1.setFill('red')
                square1.draw(win)
                boardObjects[str(x1)+str(y1)].draw2(win)

            else: # Second Click
                click2 = click
                x2 = 1+(click2.getX()-10)/increase
                y2 = 1+(click2.getY()-100)/increase

                print "X2",x2,"Y2",y2##############################################################################################
                if (((x2-x1)**2)**0.5 + ((y2-y1)**2)**0.5) !=1:
                    print "ERROR: Equal click or distanced box."##############################################################################################
                    continue

                boardObjects[str(x2)+str(y2)].undraw()
                square2 =g.Rectangle(g.Point(10+((x2-1)*increase),100+((y2-1)*increase)),g.Point(10+increase+((x2-1)*increase),100+increase+((y2-1)*increase)))
                square2.setFill('blue')
                square2.draw(win)
                boardObjects[str(x2)+str(y2)].draw2(win)

                one = True

            #Check for Matching Jewels (three in a row)
                if board[str(x2)+str(y2)]==board[str(x1)+str(y1)]:
                    t.sleep(0.5)
                    square1.setFill('blue');square2.setFill('red')
                    t.sleep(1)
                    square1.setFill('red');square2.setFill('blue')
                    
                    print "Equal jewels"##############################################################################################
            # Change jewels places
                else:
                    square1.setFill('blue');square2.setFill('red')
                    
                    joyaTemp = board[str(x1)+str(y1)]
                    board[str(x1)+str(y1)] = board[str(x2)+str(y2)]
                    board[str(x2)+str(y2)] = joyaTemp

                    listPointsXY = [x1,y1,x2,y2]
                    for i in range(0,1+len(listPointsXY)/2,2):
                        x = listPointsXY[i]
                        y = listPointsXY[i+1]
                        boardObjects[str(x)+str(y)].undraw()
                        
                    print "ReDrawing Board"##############################################################################################                            
                    boardObjects = drawOnBoard(win,listPointsXY,board,starG,circleG,squareG,triangleG,rombusG,equisG,increase,boardObjects)
                    print "DONE ReDrawing"############################################################################################## 

                    # Check if Move makes a match
                    matches = True
                    while matches:
                        matchX = matchY = False
                        if not matches:
                            print "NO MATCH"
                        # Re-draw Board
                            listPointsXY = [x1,y1,x2,y2]
                            t.sleep(1)
                            for i in range(0,1+len(listPointsXY)/2,2):
                                x = listPointsXY[i]
                                y = listPointsXY[i+1]
                                boardObjects[str(x)+str(y)].undraw()

                            square1.setFill('red');square2.setFill('blue')
                            joyaTemp = board[str(x1)+str(y1)]
                            board[str(x1)+str(y1)] = board[str(x2)+str(y2)]
                            board[str(x2)+str(y2)] = joyaTemp
                           
                            boardObjects = drawOnBoard(win,listPointsXY,board,starG,circleG,squareG,triangleG,rombusG,equisG,increase,boardObjects)
                            t.sleep(1)
                            break
                        
                        for x in range(1,11):
                            for y in range(1,8):                                                           
                                
                                print "INSIDE LOOP"
                                if x > 2:
                                    if (board[str(x)+str(y)] == board[str(x-1)+str(y)] and board[str(x)+str(y)] == board[str(x-2)+str(y)]):
                                        matchX = True
                                        print "Horizontal Match." ###############################################################
                                        board[str(x)+str(y)] = r.choice(joyas)
                                        board[str(x-1)+str(y)] = r.choice(joyas)
                                        board[str(x-2)+str(y)] = r.choice(joyas)

                                        # Re-draw Board
                                        listPointsXY = [x,y,x-1,y,x-2,y]
                                        for i in range(0,1+len(listPointsXY)/2,2):
                                            x = listPointsXY[i]
                                            y = listPointsXY[i+1]
                                            boardObjects[str(x)+str(y)].undraw()
                                        boardObjects = drawOnBoard(win,listPointsXY,board,starG,circleG,squareG,triangleG,rombusG,equisG,increase,boardObjects)
                                                                                    
                                if y > 2:
                                    if (board[str(x)+str(y)] == board[str(x)+str(y-1)] and board[str(x)+str(y)] == board[str(x)+str(y-2)]):
                                        matchY = True
                                        print "Vertical Match." ###############################################################
                                        board[str(x)+str(y)] = r.choice(joyas)
                                        board[str(x)+str(y-1)] = r.choice(joyas)
                                        board[str(x)+str(y-2)] = r.choice(joyas)
                                        # Re-draw Board
                                        listPointsXY = [x,y,x,y-1,x,y-2]
                                        for i in range(0,1+len(listPointsXY)/2,2):
                                            x = listPointsXY[i]
                                            y = listPointsXY[i+1]
                                            boardObjects[str(x)+str(y)].undraw()
                                        boardObjects = drawOnBoard(win,listPointsXY,board,starG,circleG,squareG,triangleG,rombusG,equisG,increase,boardObjects)
                                
                                        

                                if matchX and matchY:
                                    print "Double Match: vertical and horizontal." ###############################################################
                                    continue
                                elif matchX or matchY:
                                    print "Single Match." ###############################################################
                                    continue
                                else:
                                    
                                    matches = False
                                    break
                    
                    
                    
                    print "out of loop"##############################################################################################
                    
                t.sleep(0.5)
                square1.undraw();square2.undraw()
            continue
Example #3
0
def chooseColor():
    colorList = cleanTextFile()

    width = 1020
    height = 410
    sqrBox = 20
    xF1 = 10
    xF2 = width - xF1
    yF1 = height - 10
    yF2 = 100

    win = g.GraphWin("Python Color Palette Retriever",width,height)
    frame = g.Rectangle(g.Point(xF1,yF1),g.Point(xF2,yF2))
    frame.draw(win)

    x1 = xF1
    y1 = yF2 + sqrBox
    x2 = xF1 + sqrBox
    y2 = yF2

    for i in range(len(colorList)-1):
 
        pixel = g.Rectangle(g.Point(x1,y1),g.Point(x2,y2))
        pixel.setFill(colorList[i]); pixel.setOutline(colorList[i])
        pixel.draw(win)

        if x2 == xF2:
            x1 = xF1
            y1 += sqrBox
            x2 = xF1 + sqrBox
            y2 += sqrBox
            continue
        
        x1 += sqrBox
        x2 += sqrBox

    lastBox = g.Rectangle(g.Point(xF1,10),g.Point(xF1+(2*sqrBox),10+(2*sqrBox)))
    lastBox.setFill(colorList[-1]); lastBox.setOutline(colorList[-1])
    lastBox.draw(win)

    msg = 'Click on a box to get its color name.'
    label = g.Text(g.Point(frame.getCenter().getX(),50),msg)
    label.setSize(26)
    label.draw(win)

    randButt = g.Rectangle(g.Point(xF2-120,50),g.Point(xF2,10))
    randButt.setFill('pink');randButt.setOutline('red')
    randButt.draw(win)

    randText = g.Text(randButt.getCenter(),"Random")
    randText.setSize(26); randText.setFace('helvetica');randText.setStyle('bold')
    randText.draw(win)

    while True:
        click = win.getMouse()
        
        if inButton(click,randButt):
            chosenColor = r.choice(colorList)
            break
        elif not inButton(click,frame) and not inButton(click,lastBox):
            continue
        else:
            if inButton(click,lastBox):
                chosenColor = colorList[-1]
                break
            else:
                xClick = click.getX()
                yClick = click.getY()
            
                if xF1 <= xClick < xF1+sqrBox:
                    xNum = 0
                elif xF1+sqrBox <= xClick < xF1+2*sqrBox:
                    xNum = 1
                elif xF1+2*sqrBox <= xClick < xF1+3*sqrBox:
                    xNum = 2
                elif xF1+3*sqrBox <= xClick < xF1+4*sqrBox:
                    xNum = 3
                elif xF1+4*sqrBox <= xClick < xF1+5*sqrBox:
                    xNum = 4
                elif xF1+5*sqrBox <= xClick < xF1+6*sqrBox:
                    xNum = 5
                elif xF1+6*sqrBox <= xClick < xF1+7*sqrBox:
                    xNum = 6
                elif xF1+7*sqrBox <= xClick < xF1+8*sqrBox:
                    xNum = 7
                elif xF1+8*sqrBox <= xClick < xF1+9*sqrBox:
                    xNum = 8
                elif xF1+9*sqrBox <= xClick < xF1+10*sqrBox:
                    xNum = 9
                elif xF1+10*sqrBox <= xClick < xF1+11*sqrBox:
                    xNum = 10
                elif xF1+11*sqrBox <= xClick < xF1+12*sqrBox:
                    xNum = 11
                elif xF1+12*sqrBox <= xClick < xF1+13*sqrBox:
                    xNum = 12
                elif xF1+13*sqrBox <= xClick < xF1+14*sqrBox:
                    xNum = 13
                elif xF1+14*sqrBox <= xClick < xF1+15*sqrBox:
                    xNum = 14
                elif xF1+15*sqrBox <= xClick < xF1+16*sqrBox:
                    xNum = 15
                elif xF1+16*sqrBox <= xClick < xF1+17*sqrBox:
                    xNum = 16
                elif xF1+17*sqrBox <= xClick < xF1+18*sqrBox:
                    xNum = 17
                elif xF1+18*sqrBox <= xClick < xF1+19*sqrBox:
                    xNum = 18
                elif xF1+19*sqrBox <= xClick < xF1+20*sqrBox:
                    xNum = 19
                elif xF1+20*sqrBox <= xClick < xF1+21*sqrBox:
                    xNum = 20
                elif xF1+21*sqrBox <= xClick < xF1+22*sqrBox:
                    xNum = 21
                elif xF1+22*sqrBox <= xClick < xF1+23*sqrBox:
                    xNum = 22
                elif xF1+23*sqrBox <= xClick < xF1+24*sqrBox:
                    xNum = 23
                elif xF1+24*sqrBox <= xClick < xF1+25*sqrBox:
                    xNum = 24
                elif xF1+25*sqrBox <= xClick < xF1+26*sqrBox:
                    xNum = 25
                elif xF1+26*sqrBox <= xClick < xF1+27*sqrBox:
                    xNum = 26
                elif xF1+27*sqrBox <= xClick < xF1+28*sqrBox:
                    xNum = 27
                elif xF1+28*sqrBox <= xClick < xF1+29*sqrBox:
                    xNum = 28
                elif xF1+29*sqrBox <= xClick < xF1+30*sqrBox:
                    xNum = 29
                elif xF1+30*sqrBox <= xClick < xF1+31*sqrBox:
                    xNum = 30
                elif xF1+31*sqrBox <= xClick < xF1+32*sqrBox:
                    xNum = 31
                elif xF1+32*sqrBox <= xClick < xF1+33*sqrBox:
                    xNum = 32
                elif xF1+33*sqrBox <= xClick < xF1+34*sqrBox:
                    xNum = 33
                elif xF1+34*sqrBox <= xClick < xF1+35*sqrBox:
                    xNum = 34
                elif xF1+35*sqrBox <= xClick < xF1+36*sqrBox:
                    xNum = 35
                elif xF1+36*sqrBox <= xClick < xF1+37*sqrBox:
                    xNum = 36
                elif xF1+37*sqrBox <= xClick < xF1+38*sqrBox:
                    xNum = 37
                elif xF1+38*sqrBox <= xClick < xF1+39*sqrBox:
                    xNum = 38
                elif xF1+39*sqrBox <= xClick < xF1+40*sqrBox:
                    xNum = 39
                elif xF1+40*sqrBox <= xClick < xF1+41*sqrBox:
                    xNum = 40
                elif xF1+41*sqrBox <= xClick < xF1+42*sqrBox:
                    xNum = 41
                elif xF1+42*sqrBox <= xClick < xF1+43*sqrBox:
                    xNum = 42
                elif xF1+43*sqrBox <= xClick < xF1+44*sqrBox:
                    xNum = 43
                elif xF1+44*sqrBox <= xClick < xF1+45*sqrBox:
                    xNum = 44
                elif xF1+45*sqrBox <= xClick < xF1+46*sqrBox:
                    xNum = 45
                elif xF1+46*sqrBox <= xClick < xF1+47*sqrBox:
                    xNum = 46
                elif xF1+47*sqrBox <= xClick < xF1+48*sqrBox:
                    xNum = 47
                elif xF1+48*sqrBox <= xClick < xF1+49*sqrBox:
                    xNum = 48
                else:
                    xNum = 49

                if yF1 >= yClick > yF1-sqrBox:
                    yNum = 700
                elif yF1-sqrBox >= yClick > yF1 -2*sqrBox:
                    yNum = 650
                elif yF1-2*sqrBox >= yClick > yF1 -3*sqrBox:
                    yNum = 600
                elif yF1-3*sqrBox >= yClick > yF1 -4*sqrBox:
                    yNum = 550
                elif yF1-4*sqrBox >= yClick > yF1 -5*sqrBox:
                    yNum = 500
                elif yF1-5*sqrBox >= yClick > yF1 -6*sqrBox:
                    yNum = 450
                elif yF1-6*sqrBox >= yClick > yF1 -7*sqrBox:
                    yNum = 400
                elif yF1-7*sqrBox >= yClick > yF1 -8*sqrBox:
                    yNum = 350
                elif yF1-8*sqrBox >= yClick > yF1 -9*sqrBox:
                    yNum = 300
                elif yF1-9*sqrBox >= yClick > yF1 -10*sqrBox:
                    yNum = 250
                elif yF1-10*sqrBox >= yClick > yF1 -11*sqrBox:
                    yNum = 200
                elif yF1-11*sqrBox >= yClick > yF1 -12*sqrBox:
                    yNum = 150
                elif yF1-12*sqrBox >= yClick > yF1 -13*sqrBox:
                    yNum = 100
                elif yF1-13*sqrBox >= yClick > yF1 -14*sqrBox:
                    yNum = 50
                else:
                    yNum = 0
                
                chosenColor = colorList[xNum+yNum]
                break
                        
    label.undraw()
    label = g.Text(g.Point(frame.getCenter().getX(),50),chosenColor)
    label.setStyle('bold'); label.setSize(26)
    label.draw(win)            
    colorBox = g.Rectangle(g.Point(xF1,yF1),g.Point(xF2,yF2))
    colorBox.setFill(chosenColor), colorBox.setOutline(chosenColor)
    colorBox.draw(win)
    t.sleep(1)
    win.close()
    return chosenColor