2

I am making a small game about chickens and I ran into something I could not find a solution for. The problem is that my text has a small rectangle behind it, as shown here: The number of coins and a rectangle behind it. I have also figured out that the error is coming from reading a text file, the code is:




import pygame
from pygame import *

pygame.init()
surface = pygame.display.set_mode((800,600))
pg = pygame

coins = 0
eggs = 0
time_left = 12
chickens = 0
grain = 0

with open('stats.txt', 'r') as f:
    a = f.readlines()
    coins = a[0]
    eggs = a[1]
    time_left = a[2]
    chickens = a[3]
    grain = a[4]

font = pg.font.Font("freesansbold.ttf", 28)
text = font.render(coins,False,(255,255,255))

run = True
while run:
    surface.fill(0)
    for event in pg.event.get():
        if event.type == pg.QUIT:
            run = False
    surface.blit(text,(20,-2))
    pg.display.update() 



The text file is:

0
0
12.0
0
0

I do not think I can change the way I am getting information from the file, but if you know a way of doing it and it works, you can let me know.

ACarrot
  • 35
  • 4

1 Answers1

3

At the end of the text there is a character that cannot be displayed. The last character of the text is \n, because each line in the text file ends with a newline. So just remove the tailing newline from the text (see How do I remove a trailing newline?):

coins = a[0]

coins = a[0].rstrip()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174