1

I am practicing my pygame knowledge but I have got a problem here. I have two rectangles but when I run the program one of them is blinking. I tried to swap zombi.appear() and player.appear() and noticed that the blinking rect is always the second one (therefore in the case below it's player.appear()). So I assume they somehow interrupt each other. Please help me solve this.

Thanks in advance!

import pygame
import random

# Variables
inProcess = True
width, height = 500, 500
red = (255, 0, 0)
blue = (0, 0, 255)

# Pygame Settings
pygame.init()
screen = pygame.display.set_mode([width, height])
clock = pygame.time.Clock()


class Mob:
    def __init__(self, x, y, speed, type, color):
        self.x = x
        self.y = y
        self.speed = speed
        self.type = type
        self.color = color

    def render(self):
        pygame.display.flip()

    def appear(self):
        pygame.draw.rect(screen, self.color, (self.x, self.y, 64, 64))
        if self.type == 'player':
            self.move_player()
        if self.type == 'zombi':
            self.move_zombi()

        self.render()

    def move_player(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_w] and self.y > 0:
            self.y -= self.speed
        elif keys[pygame.K_s] and self.y < height - 64:
            self.y += self.speed
        elif keys[pygame.K_a] and self.x > 0:
            self.x -= self.speed
        elif keys[pygame.K_d] and self.x < width - 64:
            self.x += self.speed

    def move_zombi(self):
        if self.y < 0 or self.y + 64 > height:
            self.speed *= -1

        self.y += self.speed


def main():
    player = Mob(64, 64, 1, 'player', red)
    zombi = Mob(256, 256, 2, 'zombi', blue)

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

        screen.fill((255, 255, 255))

        zombi.appear()
        player.appear()

        clock.tick(60)


main()

1 Answers1

0

Remove pygame.display.flip() from the class Mob. And do a single pygame.display.flip()at the end of the application loop. Multiple calls to pygame.display.flip() causes flickering.
(See Why is the PyGame animation is flickering). When you update the display after the first sprite has been drawn, only the first sprite appears in the window. The second time the display is updated, both sprites are shown. Hence the second sprite is "blinking".

class Mob:
   # [...]

   # DELETE
   # def render(self):
   #      pygame.display.flip()

    def appear(self):
        pygame.draw.rect(screen, self.color, (self.x, self.y, 64, 64))
        if self.type == 'player':
            self.move_player()
        if self.type == 'zombi':
            self.move_zombi()

        # self.render() <--- DELETE
def main():
    player = Mob(64, 64, 1, 'player', red)
    zombi = Mob(256, 256, 2, 'zombi', blue)

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

        screen.fill((255, 255, 255))

        zombi.appear()
        player.appear()

        pygame.display.flip() # <-- ADD

        clock.tick(60)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174