Exemplo n.º 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)
Exemplo n.º 2
0
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
Exemplo n.º 3
0
def main():
    win = g.GraphWin("d",500,500)

    star = g.Polygon(g.Point(30,0),g.Point(5,50),g.Point(60,20),g.Point(0,20),g.Point(55,50))
    star.setFill('orange')
    starG = GGroup(g.Point(0,0)); starG.addElement(star)
    starG.draw(win)

    circle = g.Circle(g.Point(0,0),36); circle.setFill('blue'); circle.setOutline('blue')
    circleG = GGroup(g.Point(0,0)); circleG.addElement(circle)
    circleG.draw(win)

    square = g.Rectangle(g.Point(0,0),g.Point(76,76)); square.setFill('dark green'); square.setOutline('dark green')
    squareG = GGroup(g.Point(0,0)); squareG.addElement(square)
    squareG.draw(win)


    triangle = g.Polygon(g.Point(0,58),g.Point(74,58),g.Point(37,0))
    triangle.setFill('pink'); triangle.setOutline('pink')
    triangleG = GGroup(g.Point(0,0)); triangleG.addElement(triangle)
    triangleG.draw(win)

    rombus = g.Polygon(g.Point(36,72),g.Point(0,36),g.Point(36,0),g.Point(72,36))
    rombus.setFill('beige'); rombus.setOutline('beige')
    rombusG = GGroup(g.Point(0,0)); rombusG.addElement(rombus)
    rombusG.draw(win)

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