3

When I run pygame 2.0.0.dev6 (SDL 2.0.10, python 3.8.3), there is a very short initial flicker of the pygame window. It looks like this. enter image description here

Then all runs fine.

Code is:

import pygame

#Initiate
pygame.init()

#Game constants
WIDTH = 400
HEIGHT = 300
FPS = 30
color_WHITE = (255, 255, 255)
color_BLACK = (0, 0, 0)
color_RED = (255, 0, 0)
color_GREEN = (0, 255, 0)
color_BLUE = (0, 0, 255)

#Game variables
game_running = True

#Game settings
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT), pygame.DOUBLEBUF)
pygame.display.set_caption('Simple Game')
clock = pygame.time.Clock()

#Sprites: Create group
all_sprites = pygame.sprite.Group()

#Sprites: Create sprites
class Player(pygame.sprite.Sprite):
    def __init__(self, name, color, speed):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((5, 40))
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.center = (int(WIDTH/2), int(HEIGHT/2))
        self.name = name
        self.speed = speed

    def update(self):
        if self.rect.left == WIDTH:
            self.rect.right = 0
        else:
            self.rect.x += 5 * self.speed
            self.rect.top += self.speed


#Game initiations
player1 = Player('Player1', color_GREEN, 5)
all_sprites.add(player1)
player2 = Player('Player2', color_BLUE, 1)
all_sprites.add(player2)

#Game loop
while game_running:

    #Clock management
    clock.tick(FPS)

    #Clear screen
    SCREEN.fill(color_WHITE)

    #Capture events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False

    #Sprites: update()
    all_sprites.update()

    #Sprites: draw()
    all_sprites.draw(SCREEN)

    #Screen flip
    pygame.display.flip()

pygame.quit()

I checked the other posts related to this matter. From there I added double buffering and avoided duplicity in filpping. using SCREEN = pygame.display.set_mode((WIDTH, HEIGHT), pygame.HWSURFACE | pygame.DOUBLEBUF) also did not help.

How can this flicker be eliminated ?

MattMcCann
  • 42
  • 8
Rashvin
  • 31
  • 6
  • Which OS are you using? – kaktus_car May 18 '20 at 19:00
  • Seems like it's reading garbage values from its buffer. Do you only experience it when you comment out the line `SCREEN.fill(color_WHITE)`? I didn't quite get what you mean – Ted Klein Bergman May 18 '20 at 19:01
  • @kaktus_car macOS Mojave 10.14.6 – Rashvin May 21 '20 at 15:00
  • @TedKleinBergman Yes I got it by keeping and by commenting the line `SCREEN.fill(color_WHITE)`. Sorry for this ambiguity, I removed the line from my post. – Rashvin May 21 '20 at 15:03
  • 1
    **An update:** I removed Python 3.8.3 (with pygame 2.0.0) and installed Python3.7.6 (with pygame 1.9.4). There is no more flickering and thinks are working fine. – Rashvin May 21 '20 at 15:04

0 Answers0