1 Replies - 1428 Views - Last Post: 14 December 2014 - 07:11 AM Rate Topic: -----

#1 thebaldhen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-October 14

'click' event in pygame isn't working

Posted 12 December 2014 - 09:20 AM

The module I'm using to handle my buttons in pygame is pygbutton but for some reason when I click the register button on the menu screen nothing happens. Any ideas as to why this happens will be much appreciated.

My Code for the MainMenu() function:
def MainMenu():
    #bgmusic = pygame.mixer.music.load("MenuMusic.mp3")
    #pygame.mixer.music.play(-1) 
    x=140
    pygame.mouse.set_visible(True)
    spacefighter = pygame.transform.scale(JetFighter, (500,500))
    tarantula = pygame.transform.scale(enemy, (70,65))
    tarantula = pygame.transform.flip(tarantula, True, True) 
     
    myfont = pg.font.SysFont("Calibri", 100, bold=True)
    myfont2 = pg.font.SysFont("Calibri", 30, bold=True)
    myfont3 = pg.font.SysFont("Calibri", 25, bold=True)
    myfont3.set_underline(1) 
    jetfighterx = myfont.render("JET FIGHTER X", 1, red)
    USERNAME = myfont2.render("USERNAME", 1, red)
    PASSWORD = myfont2.render("PASSWORD", 1, red)
    Forgot = myfont3.render("Forgot Username/Password?", 1, red) 

    #registerb = pygbutton.PygButton((200, 490, 80, 30), "Register")
    leaderboardb = pygbutton.PygButton((300, 490, 105, 30), "Leaderboard")
    helpb = pygbutton.PygButton((500, 570, 105, 30), "Help")
    playasguestb = pygbutton.PygButton((0, 570, 105, 30), "Play As Guest")

    while True:
            for event in pygame.event.get(): 
                    if event.type == QUIT:
                        pygame.quit()
                        sys.exit()
                    if event.type == KEYDOWN:
                            if event.key == K_ESCAPE or event.key == K_q:
                                sys.exit()
                    if 'click' in registerb.handleEvent(event):
                                print "TRUE"
                            
            screen.blit(background, (0,0))
            registerb.draw(screen)
            leaderboardb.draw(screen)
            playasguestb.draw(screen)
            helpb.draw(screen) 
            screen.blit(spacefighter, (55,-5))
            screen.blit(tarantula, (255,530)) 
            screen.blit(jetfighterx, (5, 30))
            screen.blit(USERNAME, (230, 360))
            for i in range(6):
                    blit_alpha(screen,tarantula, (x, 415), 90)
                    x+=50
            x=140
            username = inputbox.ask(screen, "")
            screen.blit(PASSWORD, (230, 415))
            password = inputbox2.ask(screen, "")
            #usernamefound,passwordfound = False,False  
            #queryCurs.execute("SELECT * FROM Users")
            #for i in queryCurs:
                #for j in i:
                        #if j == username:
                                #usernamefound = True
                        #elif j == password:
                                #passwordfound = True 
                        #if usernamefound and passwordfound:
                                #break
            #if usernamefound and passwordfound:
                    #GamePlay()
            #else:
                #print "Account not found!"     
            pygame.display.update() 


I've also commented out the code that isn't responsible for the issue I'm facing.

Is This A Good Question/Topic? 0
  • +

Replies To: 'click' event in pygame isn't working

#2 Mekire   User is offline

  • D.I.C Head

Reputation: 118
  • View blog
  • Posts: 216
  • Joined: 11-January 13

Re: 'click' event in pygame isn't working

Posted 14 December 2014 - 07:11 AM

I don't much like pygbutton (nor should you expect pygame programmers to use or even be aware of it) but anyway.

This is a minimal example that shows that the button is working as expected.
Does this work for you?

import sys
import pygame as pg
import pygbutton


pg.init()
screen = pg.display.set_mode((500,500))
clock = pg.time.Clock()

button = pygbutton.PygButton((200, 200, 100, 30), "Register")

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif 'click' in button.handleEvent(event):
            print("Button pressed.")

    screen.fill(pg.Color("darkslategray"))
    button.draw(screen)
    pg.display.update()
    clock.tick(60)

pg.quit()
sys.exit()

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1