0

I'm coding something with pygame but my screen doesn't update with pygame.display.flip() Can someone help me please ?

My code : Main file :

import pygame
pygame.init()
from game import Game

pygame.display.set_caption("Run !")
screen = pygame.display.set_mode((1080, 680))

game = Game()

running = True

while running:
    game.update(screen)

    pygame.display.flip()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
        elif event.type == pygame.KEYDOWN:
            game.key_pressed[event.key] = True
        elif event.type == pygame.KEYUP:
            game.key_pressed[event.key] = False

Game file :

import pygame
from player import Player

class Game(pygame.sprite.Sprite):

    def __init__(self):
        super().__init__()
        self.player = Player()
        self.key_pressed = {}

    def update(self, screen):
        screen.blit(self.player.image, self.player.rect)


        if self.key_pressed.get(pygame.K_RIGHT):
            self.player.move_right()
        if self.key_pressed.get(pygame.K_LEFT):
            self.player.move_left()
        if self.key_pressed.get(pygame.K_UP):
            self.player.move_up()
        if self.key_pressed.get(pygame.K_DOWN):
            self.player.move_down()

        pygame.display.flip()


And the player file :

import pygame

class Player(pygame.sprite.Sprite):

    def __init__(self):
        super().__init__()
        self.velocity = 3
        self.image = pygame.image.load('res/player.png')
        self.image = pygame.transform.rotozoom(self.image, 0, 0.9)
        self.rect = self.image.get_rect()

    def move_right(self):
        self.rect.x += self.velocity

    def move_left(self):
        self.rect.x -= self.velocity

    def move_up(self):
        self.rect.y -= self.velocity

    def move_down(self):
        self.rect.y += self.velocity

I can see the player move (it is a blue square), but the previous image isn't deleted. So I see a blue line instead of a square when it moves

Thank you very much !

YogS
  • 1
  • Flipping the display does not mean that you are starting over the drawing from scratch. And blitting draws on top of whatever was there before. The result is expected. The question is how to clear the display each frame, which is best answered by looking at the documentation, following a tutorial, and/or putting something like `pygame clear frame` into a search engine. When I did that search, I got the linked duplicate as the first result. (Short version: just blit over the entire screen with a background colour before you start doing anything else for the frame.) – Karl Knechtel Mar 26 '21 at 04:50
  • Of course, if you didn't understand that the screen wouldn't be cleared, then that is a separate reason to work through a tutorial. The screen *does* "update" with `pygame.display.flip()`; that's why you see anything change at all. You are "flipping" between two different buffers used for drawing. The general technique is a fundamental of computer graphics: https://en.wikipedia.org/wiki/Multiple_buffering#Double_buffering_in_computer_graphics . This is why you can do the background colour trick without flickering the display. – Karl Knechtel Mar 26 '21 at 04:50
  • So what do I have to write ? – YogS Mar 26 '21 at 05:01
  • I already gave you a link to a duplicate, which shows answers. You should try reading them. They will explain to you what to write. I also gave a rough description of the technique. – Karl Knechtel Mar 26 '21 at 05:05
  • Sorry but I don't understand it. I don't speak very well english 'cause I'm french – YogS Mar 26 '21 at 05:08
  • See at the top of this page, where now there is a blue link that says "How to clear up screen in pygame?"? You should click that, and read the next page. – Karl Knechtel Mar 26 '21 at 05:15

0 Answers0