Why Is the Pygame Animation Is Flickering

Why is the PyGame animation is flickering

The problem is caused by multiple calls to pygame.display.update(). An update of the display at the end of the application loop is sufficient. Multiple calls to pygame.display.update() or pygame.display.flip() cause flickering.

Remove all calls to pygame.display.update() from your code, but call it once at the end of the application loop:

while running:
screen.fill((225, 0, 0))
# pygame.display.update() <---- DELETE

# [...]

player(playerX, playerY)
pygame.display.update()

If you update the display after screen.fill(), the display will be shown filled with the background color for a short moment. Then the player is drawn (blit) and the display is shown with the player on top of the background.

How to fix flickering of objects and figures in pygame?

Your game is flickering because of the multiple calls of pygame.display.flip() and pygame.display.update(). One update of the display at the end of the application loop is sufficient. Multiple calls to pygame.display.update() or pygame.display.flip() cause flickering.

Remove all calls of pygame.display.update() and pygame.display.flip() from your code, but call it once at the end of the application loop. However, clear the display before drawing the objects:

running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
ybox += 5
if (ybox >= 45):
ybox = 45

# clear disaply
screen.fill(color)

# draw objects
pygame.draw.rect(screen, box_color, pygame.Rect(xbox, ybox, 50, 50), 2)
pygame.draw.rect(screen, box_color, pygame.Rect(20, 95, 50, 50), 2)

# update dispaly
pygame.display.update()

The typical PyGame application loop has to:

  • handle the events by either pygame.event.pump() or pygame.event.get().
  • update the game states and positions of objects dependent on the input events and time (respectively frames)
  • clear the entire display or draw the background
  • draw the entire scene (blit all the objects)
  • update the display by either pygame.display.update() or pygame.display.flip()

Why does the image flicker when displayed?

The problem is caused by multiple calls to pygame.display.update(). An update of the display at the end of the application loop is sufficient. Multiple calls to pygame.display.update() or pygame.display.flip() cause flickering.

Remove all calls to pygame.display.update() from your code, but call it once at the end of the application loop:

while running:
# [...]

pygame.display.update()

Button on main screen flickering in pygame

Only perform one display update at the end of the application loop, not multiple updates during the frame. Multiple updates of the display cause flickering. One single pygame.display.update() or pygame.display.flip() at the end of the frame is sufficient.

Remove all the (6) calls to pygame.display.update() from Start_button Exit_Button and call pygame.display.update() once in welcome.

Code duplications like in Start_button Exit_Button are a code smell. Avoid them. Implement a button class which can represent and animate the button. Remove start_count and exit_count, but add a count attrubute to Button:

class Button:
def __init__(self, color, x, y, width, height, text=''):
self.color = color
self.text = text
self.rect = pygame.Rect(x, y, width, height)
self.normal = pygame.Surface((width, height))
self.normal.fill(self.color)
self.highlight = [pygame.Surface((width, height)), pygame.Surface((width + 10, height + 10))]
self.highlight[0].fill((255, 255, 255))
self.highlight[1].fill((255, 255, 255))
small_font = pygame.font.Font('Stargaze Regular.otf', 20)
large_font = pygame.font.Font('Stargaze Regular.otf', 23)
text_small = small_font.render(self.text, 1, (0, 0, 0))
text_large = large_font.render(self.text, 1, (0, 0, 0))
self.normal.blit(text_small, text_small.get_rect(center = self.normal.get_rect().center))
self.highlight[0].blit(text_small, text_small.get_rect(center = self.highlight[0].get_rect().center))
self.highlight[1].blit(text_large, text_large.get_rect(center = self.highlight[1].get_rect().center))
self.count = 0

def draw(self, win, pos, outline = None):
text_surf = self.normal
if self.isOver(pos):
text_surf = self.highlight[0]
self.count += 1
if self.count < 200:
text_surf = self.highlight[self.count // 20 % len(self.highlight)]
else:
self.count = 0
text_rect = text_surf.get_rect(center = self.rect.center)
if outline:
pygame.draw.rect(win, outline, text_rect.inflate(2, 2), 2)
win.blit(text_surf, text_rect)

def isOver(self, pos):
return self.rect.collidepoint(pos)

With this class you can simplify the function welcome a nd you don't need the functions Start_button Exit_Button at all:

def welcome():
exit_button = Button((255, 0, 255), 10, 425, 80, 40, 'EXIT')
start_button = Button((255, 0, 255), 10, 370, 80, 40, 'PLAY')

run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
if exit_button.isOver(event.pos):
run = False
if start_button.isOver(event.pos):
gameloop()
mouse = pygame.mouse.get_pos()

screen.fill((0, 0, 0))
exit_button.draw(screen, mouse)
start_button.draw(screen, mouse)
pygame.display.update()

pygame.quit()
quit()

sprite flickers when i switch between different animations in pygame

This is the problem:

def player_behaviour(self):
self.animation(1, 0, self.idle_right_frames)
self.animation(0, -1, self.idle_front_frames)
self.animation(0, 1, self.idle_back_frames)
self.animation(-1, 0, self.idle_left_frames)
#... omitted code
else:
self.idleanim = True

self.animation requires self.idleanim to be true to be true to draw the sprites. So if none of the keys are pressed, i.e after you release the key, there will be one frame where the drawing will be skipped.

This is what's happening right now.

idleanim = False
while True:
if idleanim:
DrawSprites()
idleanim = True

You can see why it would skip drawing once. What you want to do is to check the if statements first and then call self.animate

def player_behaviour(self):
#... omitted code
else:
self.idleanim = True
self.animation(1, 0, self.idle_right_frames)
self.animation(0, -1, self.idle_front_frames)
self.animation(0, 1, self.idle_back_frames)
self.animation(-1, 0, self.idle_left_frames)

pygame text flickers

Just update the display once at the end of the application loop. Multiple updates of the display causes flickering:

while fonidet:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

print_text('Test.', 200, 900, font_color=(255, 255, 255), font_type='01.ttf', font_size = 30)
print_text('Test!', 200, 950, font_color=(255, 255, 255), font_type='01.ttf', font_size=30)
print_text('(Нажмите на ">>>" слева)', 200, 1000, font_color=(255, 255, 255), font_type='01.ttf', font_size=30)

#pygame.display.update() <-- DELETE
#clock.tick(15)

display.blit(fon1, (0, 0))
playgame_btn.draw(80, 930, '>>>')
display.blit(diktor, (1150, 58))
pygame.display.update()
clock.tick(60)


Related Topics



Leave a reply



Submit