I'm making a simple game where there's a moveable, in my case, image. It is just controlled by the arrow keys, and this functionality works fine. However I've got the issue of it leaving behind its trail, of which one should solve by adding screen.fill((0, 0, 0)) so it removes the previous position. However, drawing my background involves reading 900 lines of data and displaying 900 32x32p images, so when I also have to redraw my background it seems python can't handle doing this constantly crashes. Is there another way I can have a moving image while still retaining my background?
Some relevant code:
For drawing the background
while Scene1Read == False:
read = open("testgraphics.txt", "r")
xcoords = -32
ycoords = 0
squarenum = 0
for x in read:
squarenum += 1
if xcoords < 1024:
xcoords += 32
else:
xcoords = -32
ycoords += 32
squarecolorformatting = x[-6:]
squarecolor = squarecolorformatting[:-1]
if squarecolor == "METAL":
img = pygame.image.load("metal.png")
screen.blit(img, (xcoords,ycoords))
elif squarecolor == "SANDx":
img = pygame.image.load("sand.png")
screen.blit(img, (xcoords,ycoords))
elif squarecolor == "TRAPD":
img = pygame.image.load("trapdoor.png")
screen.blit(img, (xcoords,ycoords))
else:
pygame.draw.rect(screen,colorsdict[squarecolor], [xcoords,ycoords,32,32])
terminalfont = pygame.font.Font("falloutterminal.ttf", 16)
testtext1 = terminalfont.render(str(squarenum), False, (255, 0, 255))
screen.blit(testtext1,(xcoords,ycoords))
Scene1Read = True
read.close()
For moving the character
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -16
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = 16
y1_change = 0
elif event.key == pygame.K_UP:
x1_change = 0
y1_change = -16
elif event.key == pygame.K_DOWN:
x1_change = 0
y1_change = 16
x1 += x1_change
y1 += y1_change