У меня есть кнопка, созданная с помощью Pygame.Теперь я хочу показать текст на экране после нажатия кнопки.Но я хочу дать ему переменную вместо строки.Как я могу это сделать ?Я попытался добавить screen.blit(txtSmjerKretanja, (150,150))
к if gumbLijevo.collidepoint(mouse_pos)
и if gumbDesno.collidepoint(mouse_pos):
, но это не работает.РЕДАКТИРОВАТЬ: Я посмотрел на предложенные темы от других пользователей, но это не решает мою проблему.Проблема в том, что моя программа не отображает текст, как только я использую некоторую переменную (в данном случае smjerKretanja) вместо обычной строки вроде ('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()