Zayed Azam
Zayed Azam

Reputation: 83

Increase displayed number after clicking appropriate button [pygame]

For my pygame 'score' board, I'm trying to make it so that when I click one of the coloured rectangles, a number next to it will increase. How would I a) display the number, b)make the rectangles clickable, and c) make it so that the number increases when I left-click the corresponding rect., and decreases when I right-click?

import pygame
pygame.init()

white = 255,255,255 #This block defines all the colors in (R,G,B) format
black = 0,0,0
green = 0,255,0
blue = 0,76,153
red = 255,0,0
orange = 255,128,0

height = 1366 #The height and width of our window
width = 768
window = pygame.display.set_mode((height,width)) 
pygame.display.set_caption("Score") #Edit the text between quotes to change 
window title

title_font = pygame.font.SysFont('Comic Sans MS', 70) 
button_font = pygame.font.SysFont('Times New Roman', 12)


title = title_font.render("The Activity", True, (black))

clock = pygame.time.Clock()
crashed = False

flask_img_right = pygame.image.load('flask.jpg').convert() 
flask_img_left = pygame.image.load('flask1.jpg').convert()

def flask_right(x_fr,y_fr):
    window.blit(flask_img_right,(x_fr,y_fr))
x_fr = (1000)
y_fr = (200)

def flask_left(x_fl,y_fl):
    window.blit(flask_img_left,(x_fl,y_fl))
x_fl = (100)
y_fl = (200)

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def things_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Dodged: "+str(count), True, black)
    gameDisplay.blit(text,(0,0))


while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
    #Increasing the first number moves text to the right
    #Increasing the second number moves text lower
    #Assume this for everything below unless otherwise stated

    window.fill(white)
    flask_right(x_fr,y_fr)
    flask_left (x_fl, y_fl)
    window.blit(title,(500,50))   

    mouse = pygame.mouse.get_pos()

    b1_surf, b1_rect = text_objects("", button_font)
    b1_rect.center = ((550),(220))
    window.blit(b1_surf, b1_rect)
    pygame.draw.rect(window, blue, (500,200,100,50))
    b1_text, b1t_rect = text_objects("Team 1", button_font)
    b1t_rect.center = ((550),(220))
    window.blit(b1_text, b1t_rect)

    b2_surf, b2_rect = text_objects("",button_font)
    b2_rect.center = ((550,270))
    window.blit(b2_surf, b2_rect)
    pygame.draw.rect(window, red,(500,250,100,50))
    b2_text, b2t_rect = text_objects("Team 2", button_font)
    b2t_rect.center = ((550,270))
    window.blit(b2_text, b2t_rect)

    b3_surf, b3_rect = text_objects("",button_font)
    b3_rect.center = ((550,320))
    window.blit(b3_surf, b3_rect)
    pygame.draw.rect(window, orange,(500,300,100,50))
    b3_text, b3t_rect = text_objects("Team 3", button_font)
    b3t_rect.center = ((550,320))
    window.blit(b3_text, b3t_rect)

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

pygame.quit()
quit()
end_command = input("any: ")

Upvotes: 0

Views: 1121

Answers (2)

gnaw
gnaw

Reputation: 395

Use a button class

class Button(object):
    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)
        drawText(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)

Implemented into code(borrowed from skrx):

def main():
    clock = pg.time.Clock()
    font = pg.font.Font(None, 30)
    button=Button(parameters)

    score = 0
    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.MOUSEBUTTONDOWN:
                if button.check()==True:
                    if event.button == 1:  # Left button.
                        print('Increment.')
                        score += 1
                    elif event.button == 2:  # Right button.
                        print('Decrement.')
                        score -= 1

        screen.fill(GRAY)
        button.draw()
        txt = font.render(str(score), True, BLUE)
        screen.blit(txt, (260, 206))

        pg.display.flip()
        clock.tick(10)


if __name__ == '__main__':
    main()
    pg.quit()

Upvotes: 1

skrx
skrx

Reputation: 20438

Here's an answer which shows you two solutions for buttons with a simple label. To increment or decrement the number, just check if the left or right mouse button was pressed.

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))

GRAY = pg.Color('gray15')
BLUE = pg.Color('dodgerblue1')


def main():
    clock = pg.time.Clock()
    font = pg.font.Font(None, 30)
    button_rect = pg.Rect(200, 200, 50, 30)

    score = 0
    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            if event.type == pg.MOUSEBUTTONDOWN:
                if button_rect.collidepoint(event.pos):
                    if event.button == 1:  # Left button.
                        print('Increment.')
                        score += 1
                    elif event.button == 3:  # Right button.
                        print('Decrement.')
                        score -= 1

        screen.fill(GRAY)
        pg.draw.rect(screen, BLUE, button_rect)
        txt = font.render(str(score), True, BLUE)
        screen.blit(txt, (260, 206))

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    main()
    pg.quit()

Upvotes: 1

Related Questions