Python Forum
[PyGame] Mouse Click Problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Mouse Click Problem
#1
Hi,

I took some code from GitHub and I have a problem as when I choose from menu "Settings", and just move the mouse, the menu disappear and main menu comes back.

below is the code:

Main Screen:
def baseScreen():
    thismodule.ending1 = True
    while thismodule.ending1:
        gameDisplay.fill( ( 255,255,255 ) )
        displayText( "SUDOKU",fontSize3,red,display_width / 2,0.4 * ( display_height / 2 ) )
        displayText( "Rami Taher Wahdan",fontSize1,black,display_width / 2,0.7 * ( display_height / 2 ) )

        press( display_width * 0.1,display_height * 0.68,display_width * 0.8,display_height * 0.1,2,black,"Start!",fontSize1,black,green,[ MainScreen ] )
        press( display_width * 0.1,display_height * 0.8,display_width * 0.8,display_height * 0.1,2,black,"Settings",fontSize1,black,green,[ Settings ] )

        PollEvents()
        pygame.display.update()
        clock.tick( 60 )
Settings Code:

def Settings():
    thismodule.ending4 = True

    def Deny_End():
        thismodule.ending4 = False

    def SetDifficultyEasy():
        thismodule.numOfFreePlaces = 81 - easy #31
    def SetDifficultyMedium():
        thismodule.numOfFreePlaces = 81 - medium #36
    def SetDifficultyHard():
        thismodule.numOfFreePlaces = 81 - hard #41

    SetDifficultyEasy()

    while thismodule.ending4:
        gameDisplay.fill( ( 255,255,255 ) )

        displayText( "Settings",fontSize2,black,display_width / 2,display_height * 0.1 )
        displayText( "Difficulty",fontSize1,black,display_width / 2,display_height * 0.5 )
        press( display_width * 0.15,display_height * 0.8,display_width * 0.7,display_height * 0.1,3,black,"Main Menu",fontSize1,black,green, [ Deny_End ] )

        if thismodule.numOfFreePlaces is 81 - easy:
            press( display_width / 21,display_height * 0.57,( display_width / 21 ) * 5,display_height * 0.1,3,black,"Easy",fontSize0,green,green,[ SetDifficultyEasy ] )
            press( ( display_width / 21 ) * 8,display_height * 0.57,( display_width / 21 ) * 5,display_height * 0.1,3,black,"Medium",fontSize0,red,red,[ SetDifficultyMedium ] )
            press( ( display_width / 21 ) * 15,display_height * 0.57,( display_width / 21 ) * 5,display_height * 0.1,3,black,"hard",fontSize0,red,red,[ SetDifficultyHard ] )
        elif thismodule.numOfFreePlaces is 81 - medium:
            press( display_width / 21,display_height * 0.57,( display_width / 21 ) * 5,display_height * 0.1,3,black,"Easy",fontSize0,red,red,[ SetDifficultyEasy ] )
            press( ( display_width / 21 ) * 8,display_height * 0.57,( display_width / 21 ) * 5,display_height * 0.1,3,black,"Medium",fontSize0,green,green,[ SetDifficultyMedium ] )
            press( ( display_width / 21 ) * 15,display_height * 0.57,( display_width / 21 ) * 5,display_height * 0.1,3,black,"hard",fontSize0,red,red,[ SetDifficultyHard ] )
        elif thismodule.numOfFreePlaces is 81 - hard:
            press( display_width / 21,display_height * 0.57,( display_width / 21 ) * 5,display_height * 0.1,3,black,"Easy",fontSize0,red,red,[ SetDifficultyEasy ] )
            press( ( display_width / 21 ) * 8,display_height * 0.57,( display_width / 21 ) * 5,display_height * 0.1,3,black,"Medium",fontSize0,red,red,[ SetDifficultyMedium ] )
            press( ( display_width / 21 ) * 15,display_height * 0.57,( display_width / 21 ) * 5,display_height * 0.1,3,black,"hard",fontSize0,green,green,[ SetDifficultyHard ] )


        pygame.display.flip()
        clock.tick( 60 )
        PollEvents()
Pressed Code:
def press( borderXStart,borderYStart,width,height,borderWidth,borderColor,text,textSize,textColorDefault,textColorHover,actions ):
    def avg( a,b ):
        return ( a + b ) / 2

    pygame.draw.rect( gameDisplay,borderColor,( borderXStart,borderYStart,width,height ),borderWidth )
    mouse = pygame.mouse.get_pos()

    if borderXStart + width > mouse[ 0 ] > borderXStart:
        if borderYStart + height > mouse[ 1 ] > borderYStart:
            displayText( text,textSize,textColorHover,avg( borderXStart,borderXStart + width ),borderYStart + height / 2 )
            if pygame.MOUSEBUTTONDOWN:
                if pygame.mouse.get_pressed()[0]:
                    if actions:
                        for i in actions:
                            i()
                    pygame.event.wait()
        else:
            displayText( text,textSize,textColorDefault,avg( borderXStart,borderXStart + width ),borderYStart + height / 2 )
    else:
        displayText( text,textSize,textColorDefault,avg( borderXStart,borderXStart + width ),borderYStart + height / 2 )
problem with this line of code that will terminate the menu and gets back to main menu:
def Deny_End():
    thismodule.ending4 = False
above code will go back to settings and ends the while loop:
    while thismodule.ending4:
        gameDisplay.fill( ( 255,255,255 ) )

        displayText( "Settings",fontSize2,black,display_width / 2,display_height * 0.1 )
        displayText( "Difficulty",fontSize1,black,display_width / 2,display_height * 0.5 )
        press( display_width * 0.15,display_height * 0.8,display_width * 0.7,display_height * 0.1,3,black,"Main Menu",fontSize1,black,green, [ Deny_End ] )
Thanks for the help.
Reply


Messages In This Thread
Mouse Click Problem - by rwahdan - Jul-08-2019, 06:50 AM
RE: Mouse Click Problem - by metulburr - Jul-08-2019, 11:41 AM
RE: Mouse Click Problem - by Windspar - Jul-08-2019, 02:12 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020