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
Does your background scroll or have dynamic elements that move around? If not and it's just a static background, one solution that I did for my game was I made the background into one image file so each time it was redrawing the background it only had to deal with one blit function call instead of blitting each tile to redraw the background. There may be other solutions I don't know about, but I would recommend thinking about that option
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With