2

I've recently started learning Python3 and I was trying to make a game with Python3 by importing pygame. I tried to make a menu and I am having some struggles with it. I just tried to make it seem like its a button by letting the rectangle change color when you hover over it but its not working. I already tried some things but its not working. Anyhow here is the full code: hastebin link.

This is the part where I tried to make a button:

def game_intro():
    intro = True

    gameDisplay.fill(white)
    largeText = pygame.font.Font('freesansbold.ttf', 90)
    TextSurf, TextRect = text_objects("Run Abush Run!", largeText)
    TextRect.center = ((display_width / 2), (display_height / 2))
    gameDisplay.blit(TextSurf, TextRect)

    mouse = pygame.mouse.get_pos()

    if 150+100 > mouse[0] > 150 and 430+50 > mouse[1] > 430:
        pygame.draw.rect(gameDisplay, bright_green, (150,430,100,50))
    else:
        pygame.draw.rect(gameDisplay, green, (150, 430, 100, 50))

    smallText = pygame.font.Font('freesansbold.ttf' ,20)
    textSurf, textRect = text_objects("START!", smallText)
    textRect.center = ( (150+(100/2)), (450+(430/2)) )
    gameDisplay.blit(textSurf, textRect)

    pygame.draw.rect(gameDisplay, red, (550, 430, 100, 50))

    pygame.display.update()
    clock.tick(15)

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
CheekyEntity
  • 33
  • 1
  • 6
  • 3
    Please put code in the question, not an external link. Also, a [mcve] is much better than a complete dump of everything. – abarnert Mar 24 '18 at 18:56
  • @abarnert I did what you told me to. – CheekyEntity Mar 24 '18 at 19:05
  • Much better. Although I think you got the indentation wrong on pasting here. How much of this is part of the `game_intro` function? – abarnert Mar 24 '18 at 19:14
  • @abarnert I've edited the spacing. I assume he had it right in code, but messed it up in Markdown (only spaced the first line) – Srevilo Mar 25 '18 at 02:11
  • [Here's an answer](https://stackoverflow.com/a/47664205/6220679) in which I describe several ways to implement simple buttons in pygame. You could also check out some of the pygame GUI libraries like SGC. – skrx Mar 25 '18 at 21:32

3 Answers3

2

The problem here is that you're only doing your mouseover test once, at the start of the function. If the mouse later moves into your rectangle, it won't matter, because you never do the test again.

What you want to do is move it into the event loop. One tricky bit in PyGame event loops is which code you want to run once per event (the inner for event in… loop), and which you only want to run once per batch (the outer while intro loop). Here, I'm going to assume you want to do this once per event. So:

def game_intro():
    intro = True

    # ... other setup stuff 

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()

            mouse = pygame.mouse.get_pos()

            if 150+100 > mouse[0] > 150 and 430+50 > mouse[1] > 430:
                pygame.draw.rect(gameDisplay, bright_green, (150,430,100,50))
            else:
                pygame.draw.rect(gameDisplay, green, (150, 430, 100, 50))

It looks like some of the other stuff you do only once also belongs inside the loop, so your game may still have some problems. But this should get you past the hurdle you're stuck on, and show you how to get started on those other problems.

abarnert
  • 354,177
  • 51
  • 601
  • 671
1

Here is a button that should suit your needs:

class Button(object):
    global screen_width,screen_height,screen
    def __init__(self,x,y,width,height,text_color,background_color,text):
        self.rect=pygame.Rect(x,y,width,height)
        self.x=x
        self.y=y
        self.width=width
        self.height=height
        self.text=text
        self.text_color=text_color
        self.background_color=background_color

    def check(self):
        return self.rect.collidepoint(pygame.mouse.get_pos())

    def draw(self):
        pygame.draw.rect(screen, self.background_color,(self.rect),0)
        drawTextcenter(self.text,font,screen,self.x+self.width/2,self.y+self.height/2,self.text_color)  
        pygame.draw.rect(screen,self.text_color,self.rect,3)

Use the draw function to draw your button, and the check function to see if the button is being pressed.

Implemented into a main loop:

button=Button(x,y,width,height,text_color,background_color,text)
while not done:
    for event in pygame.event.get():
            if event.type==QUIT:
                terminate()
            elif event.type==pygame.MOUSEBUTTONDOWN:
                if button.check():
                       #what to do when button is pressed

    #fill screen with background
    screen.fill(background)

    button.draw()

    pygame.display.flip()
    clock.tick(fps)
gnawydna
  • 385
  • 1
  • 5
  • 22
1

This is what i did and it works now:

def game_intro():
    intro = True

    while intro:
        for event in pygame.event.get():
            # print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

    gameDisplay.fill(white)
    largeText = pygame.font.Font('freesansbold.ttf', 90)
    TextSurf, TextRect = text_objects("Run Abush Run!", largeText)
    TextRect.center = ((display_width / 2), (display_height / 2))
    gameDisplay.blit(TextSurf, TextRect)

    mouse = pygame.mouse.get_pos()

    # print(mouse)

    if 150 + 100 > mouse[0] > 150 and 450 + 50 > mouse[1] > 450:
        pygame.draw.rect(gameDisplay, bright_green, (150, 450, 100, 50))
    else:
        pygame.draw.rect(gameDisplay, green, (150, 450, 100, 50))
    pygame.draw.rect(gameDisplay, red, (550, 450, 100, 50))
    pygame.display.update()
    clock.tick(15)

Thank you for the help @abarnert

CheekyEntity
  • 33
  • 1
  • 6