I am trying to set up scrolling in my pyGame-based random map generator. However, I\'m having trouble figuring out how to keep the display from \"smearing\" the background i
pygame.Surface.scroll() doesn't do what you expect. It doesn't seamless roll the surface. See the documentation:
scroll()
Shift the surface image in place[...] Areas of the surface that are not overwritten retain their original pixel values. [...]
You've to write your own scroll functions, which place the part of the surface, which was shifted out, at the other side of thee surface.
e.g. write 2 functions (scrollX
and scrollY
), which can roll along an axis:
def scrollX(screenSurf, offsetX):
width, height = screenSurf.get_size()
copySurf = screenSurf.copy()
screenSurf.blit(copySurf, (offsetX, 0))
if offsetX < 0:
screenSurf.blit(copySurf, (width + offsetX, 0), (0, 0, -offsetX, height))
else:
screenSurf.blit(copySurf, (0, 0), (width - offsetX, 0, offsetX, height))
def scrollY(screenSurf, offsetY):
width, height = screenSurf.get_size()
copySurf = screenSurf.copy()
screenSurf.blit(copySurf, (0, offsetY))
if offsetY < 0:
screenSurf.blit(copySurf, (0, height + offsetY), (0, 0, width, -offsetY))
else:
screenSurf.blit(copySurf, (0, 0), (0, height - offsetY, width, offsetY))
if pressed[pygame.K_UP]:
scrollY(screen, 2)
elif pressed[pygame.K_DOWN]:
scrollY(screen, -2)
elif pressed[pygame.K_LEFT]:
scrollX(screen, 2)
elif pressed[pygame.K_RIGHT]:
scrollX(screen, -2)