Reputation: 1487
def _update_surfaces(self):
"""Updates rectangles in self.oldrects to reduce latency. If the list is empty, update the
entire screen."""
if self.oldrects:
pygame.display.update(self.oldrects)
self.oldrects = []
else:
pygame.display.flip()
def _check_events(self):
"""Respond to events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
# Move the ship to the right.
self.ship.rect.x += 1
def _update_screen(self):
"""Update surface rects, update individual surfaces on the screen."""
self.screen.fill(self.settings.bg_color)
self.char.blit()
self._add_old_rect(self.char) # Add surface to old list
self._update_surfaces() # Update individual surfaces
def run_game(self):
"""Start main game loop."""
self.screen.fill(self.settings.bg_color)
self._update_surfaces() # Update sceen
while True:
self._check_events()
self._update_screen()
In pygame documentation, it is said that the best practice to update the screen is to blit individual rects, rather than the entire display. I've attempted to do this here, but when the character moves on the screen, a copy at their previous position is left behind. I believe it relates to the way I'm handling the screen updates, as when I refresh the entire screen instead of individual rects, it works fine.
Here's the documentation tutorial link I was trying to impletement: Dirty rect animation (from newbieguide)
Upvotes: 1
Views: 1049
Reputation: 61
self.screen.fill(self.settings.bg_color)
fills the entire screen with a background color, however
pygame.display.update(self.oldrects)
only updates the surface with your character.
You need to update the entire screen to make the trail disappear:
def _update_surfaces(self):
pygame.display.flip()
To make it more efficient, update where your rects were previously:
def _update_surfaces(self):
"""Updates rectangles in self.oldrects to reduce latency. If the list is empty, update the
entire screen."""
if self.oldrects:
pygame.display.update(self.prevoldrects)
pygame.display.update(self.oldrects)
self.prevoldrects = self.oldrects.copy() # list.copy makes a new list instead of copying the reference
self.oldrects = []
else:
pygame.display.flip()
Upvotes: 2