1

I have a button created with pygame. Now i want to show some text on screen once the button is pressed. But i want to give it a varibale instead of string. How do i do that ? I tried adding the screen.blit(txtSmjerKretanja, (150,150)) to if gumbLijevo.collidepoint(mouse_pos) and if gumbDesno.collidepoint(mouse_pos): but it doesn't work. EDIT: I looked at the suggested topics from other users, but it doesn't really solve my problem. Problem is that my program doesn't show text as soon as i use some varibale (in this case smjerKretanja) instead of usual string like ('some string..').

import pygame
import sys

def main():
    pygame.init()
    myfont = pygame.font.SysFont('Arial', 20)
    clock = pygame.time.Clock()
    fps = 60
    size = [500, 500]
    bg = [255, 255, 255]

    smjerKretanja = 0

    screen = pygame.display.set_mode(size)
    gumbLijevo = pygame.Rect(20, 20, 100, 30)
    gumbDesno = pygame.Rect(150, 20, 100, 30)
    txtLijevo = myfont.render('Lijevo', False, (0,0,0))
    txtDesno = myfont.render('Desno', False, (0,0,0))
    txtSmjerKretanja = myfont.render(str(smjerKretanja), False, (0,0,0))

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return False

            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = event.pos  # gets mouse position

                # checks if mouse position is over the button

                if gumbLijevo.collidepoint(mouse_pos):
                    # prints current location of mouse
                    # print('button was pressed at {0}'.format(mouse_pos))
                    smjerKretanja = smjerKretanja - 10
                    if smjerKretanja < 0:
                        smjerKretanja = 350
                    print smjerKretanja
                    screen.blit(txtSmjerKretanja, (150,150))

                if gumbDesno.collidepoint(mouse_pos):
                    # prints current location of mouse
                    # print('button was pressed at {0}'.format(mouse_pos))
                    smjerKretanja = smjerKretanja + 10
                    if smjerKretanja > 360:
                        smjerKretanja = 10
                    print smjerKretanja
                    screen.blit(txtSmjerKretanja, (150,150))



        screen.fill(bg)

        pygame.draw.rect(screen, [255, 0, 0], gumbLijevo)
        pygame.draw.rect(screen, [255, 0, 0], gumbDesno)
        screen.blit(txtLijevo,(30,20))
        screen.blit(txtDesno,(160,20))



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

    pygame.quit()
    sys.exit


if __name__ == '__main__':
    main()
Bogy
  • 13
  • 6
  • Possible duplicate of [How to display text in pygame?](https://stackoverflow.com/questions/20842801/how-to-display-text-in-pygame) – busybear Dec 31 '18 at 13:22
  • I edited my original post to show whats the problem and why suggested posts from other users cant help me. – Bogy Dec 31 '18 at 13:52
  • It is weird, indeed... the statement `txtSmjerKretanja = myfont.render(str(smjerKretanja), False, (0,0,0))` should do the work, it's just converting a `0` value to string... **that said**, that line itself has a [train wreck](http://wiki.c2.com/?TrainWreck) that might contribute to hide the error cause. Perhaps if you split that line in two by using something like `strSmjerKretanja = str(smjerKretanja)` before using `myfont.render`, you might obtain a more useful error feedback while executing it in a CLI. – SebasSBM Dec 31 '18 at 14:06
  • I managed to fix something by putting the textSmjerKretanja inside the "while" loop. I will try to experiment with some things now, and will report if everything works fine. Thank you for the help. – Bogy Dec 31 '18 at 14:34

1 Answers1

0

You only update txtSmjerKretanja at the start of the code, in order to update it constantly you need to add

txtSmjerKretanja = myfont.render(str(smjerKretanja), False, (0,0,0))

the while loop