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 instead of just scrolling across it. I have made a MCV Example just for Stack Overflow that only displays colored squares. It has the same problem.
How can I make the display scroll without smearing the screen?
This is what the map generator displays after I scroll around a bit:
MCV Example:
""" This example just displays colored squares. """
import pygame
import sys
def main():
pygame.init()
screen = pygame.display.set_mode((800, 800))
x_pos = 0
y_pos = 0
yellow = (255, 255, 0)
blue = (0, 0, 255)
# draw pattern of yellow and blue squares
for y in range(100):
for x in range(100):
if (y + x) % 2 == 0:
pygame.draw.rect(screen, yellow, (x_pos, y_pos, 50, 50))
else:
pygame.draw.rect(screen, blue, (x_pos, y_pos, 50, 50))
x_pos += 50
y_pos += 50
x_pos = 0
while True: # <-- the pyGame loop
event = pygame.event.poll()
pressed = pygame.key.get_pressed()
# handle window closing
if event.type == pygame.QUIT:
break
# handle scrolling
if pressed[pygame.K_UP]:
screen.scroll(0, 2)
elif pressed[pygame.K_DOWN]:
screen.scroll(0, -2)
elif pressed[pygame.K_LEFT]:
screen.scroll(2, 0)
elif pressed[pygame.K_RIGHT]:
screen.scroll(-2, 0)
# updates what the window displays
pygame.display.update()
pygame.quit()
sys.exit(0)
if __name__ == "__main__":
# runs the pyGame loop
main()
Initial MCVE image
MCVE Display After Scrolling: