Bottom Page

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.
Quote
#2
We would need the full code to run it ourselves.

Where did you get this code from? This appears to be using multiple states, but in a very bad way.
while thismodule.ending1:
while thismodule.ending4:

In this tutorial we explained that this code structure is asking for trouble and confusion, especially across modules. There should never be more than one game loop regardless of how many states you have. Here is a proper example of a state machine.

Also if its just a Sudoku puzzle, i dont see why you would need different states in the first place. You would just update the block. You would not need a different state unless your doing something else more complicated.
Windspar likes this post
Quote
#3
One reject code. Start over. Like metulburr bad states. Also bad ways to display text, and buttons.
Text and buttons should be objects. Not created every loop.

Check my site out. Have a different type of state machine.
Also have sprite Button, sprite Text and ClickText.
ClickText can be use for a menu system.
99 percent of computer problems exists between chair and keyboard.
hobby-programmer.com
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)