Drawing a button - ERROR


#1

So currently im making a menu for my game. I have defined each button as a separate function. So like:
def about_Button():

etc.
I have been running into problems loading all the buttons. I have made a function named 'main' and it runs the two functions 'about_Button' and 'start_Button'. When I load it the first one which is meant to load, loads. But the second one doesn't unless your press where it is meant to be. Please may you help me so it loads both instead of making it load one, then you have to click around for a while. Thanks!

P.S. Here is the code:

import pygame, time, ctypes
import pygame.freetype

#Color definitions
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0, 255, 0)
BLUE = (0,0,255)
YELLOW = (255,255,0)
SEA_BLUE =(0,41,58)
BLUE2 = (0,153,153)

def fade():
    logo = pygame.image.load("icon.png").convert()
    clock = pygame.time.Clock()

    # fade in the logo
    for i in range(255):
        screen.fill((0,0,0))
        logo.set_alpha(i)
        screen.blit(logo, (0,0))
        pygame.display.flip()
        clock.tick(40)             # limit framerate to 40 fps

def main():
    about_Button()
    start_Button()

def set_screen():
    user32 = ctypes.windll.user32
    screenSize =  user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
    screenH = user32.GetSystemMetrics(0)
    screenW = user32.GetSystemMetrics(1)
    print (screenSize)
    size = (screenSize)
    return pygame.display.set_mode((size) , pygame.FULLSCREEN)

def events():
    while True:
        ## Exit
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                exit()

def start_Button():
    button = fontLargeTitle.render_to(screen,(25,39), "Start", BLUE2) 
    pygame.display.flip()

    ## loop to check for mouse action and its position ##
    while True:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                ## if mouse is pressed get position of cursor ##
                pos = pygame.mouse.get_pos()
                ## check if cursor is on button ##
                if button.collidepoint(pos):
                    fade()
                    import pong
                    return

def about_Button():
    button = fontSmallTitle.render_to(screen,(25,150), "About", WHITE) 
    pygame.display.flip()

    ## loop to check for mouse action and its position ##
    while True:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                ## if mouse is pressed get position of cursor ##
                pos = pygame.mouse.get_pos()
                ## check if cursor is on button ##
                if button.collidepoint(pos):
                    
                    return





if __name__ == "__main__":
    pygame.init()
    # Creating the screen
    screen = pygame.display.set_mode((640, 480))
    
    # Setting up the icon and name
    pygame.display.set_caption('Game Menu')
    icon = pygame.image.load('icon.png')
    pygame.display.set_icon(icon)
    
    # Menu Picture Blit
    img = pygame.image.load("menu_icon.png")
    img_Menu = pygame.transform.rotate(img, -90)
    screen.blit(img_Menu,(400, 120))

    
    # Fonts
    fontLargeTitle = pygame.freetype.Font("earthorbiter.ttf", 100)
    fontSmallTitle = pygame.freetype.Font("earthorbiter.ttf", 50)

    # Do calling functions now;
    main()

pygame.display.update()

#2

This is a really good Event Handling tutorial that I took myself that will help you understand how to get past your error,

http://inventwithpython.com/blog/2012/10/30/creating-a-button-ui-module-for-pygame/


#3

I've skimmed over this and I still cannot find a solution. My brother is in Year 13 and he told me to do stuff like I have so its neat etc. so it kinda breaks my understanding and knowledge. Please may you guide me through how to fix it? Ive tried pygame.display.update() but to my avail it doesn't work. Im so stumped :confused:


#4

Please bandit :frowning:


#5

Lol I've just gone through your code and I cannot write for you what you are asking, but what I can do is point out your problem and how to fix it if you tell me the error you are receiving. :slight_smile:


#6

The error I am receiving is that I have to click around for the Start button to appear and I want it to appear normally like the about button. :neutral_face:


#7

Okay now you see that I can help you with,

def about_Button():
    button = fontSmallTitle.render_to(screen,(25,150), "About", WHITE) 
    pygame.display.flip()

    ## loop to check for mouse action and its position ##
    while True:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                ## if mouse is pressed get position of cursor ##
                pos = pygame.mouse.get_pos()
                ## check if cursor is on button ##
                if button.collidepoint(pos):
                    
                    return

Look at the differences between the two buttons and just duplicate your about button code to math your start button code apart from positioning because, You are happy with how your about button behaves so it is much more efficient to use your working code than to start think of an alternate long cut of doing the same thing. :slight_smile:

def start_Button():
    button = fontLargeTitle.render_to(screen,(25,39), "Start", BLUE2) 
    pygame.display.flip()

    ## loop to check for mouse action and its position ##
    while True:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                ## if mouse is pressed get position of cursor ##
                pos = pygame.mouse.get_pos()
                ## check if cursor is on button ##
                if button.collidepoint(pos):
                    fade()
                    import pong #Move this import to the top of your code along with the other imports
                    return

#8

They are the same execpt from the text change and the text in them


#9

In the about function you use,

(screen,(25,150))

but in your start function you use,

screen,(25,39)

And in your start function you have commented out some lines.


#10

the difference in that screen thing is for the positioning :confused:


#11

Ahhhh. I see! I forgot some brackets!!!


#12

Yes, you must focus on the small details more keenly than the large ones. :smile:


#13

Actually in the about function I use
button = fontSmallTitle.render_to(screen,(25,150), "About", WHITE)

And in the start button function I use
button = fontLargeTitle.render_to(screen,(25,39), "Start", BLUE2)

Where is the change? You wrote it out wrong ?


#14

Here is my project so far. Please may you fix the importing things ect. as it keeps saying video system not initialized or some other error and I cannot fix it


#15

Forgot the link :smiley:
Here it is:


#16

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.