Home > database >  why is the picture not displayed on the pygame window? the code does not give errors
why is the picture not displayed on the pygame window? the code does not give errors

Time:02-24

import pygame    
import sys    
from pygame.locals import *    
import random    
import time    
GREY = pygame.Color(128, 128, 128)        
RED = pygame.Color(255, 0, 0)    
BLUE = pygame.Color(0, 0, 255)    
GREEN = pygame.Color(0, 255, 0)    
BLACK = pygame.Color(0, 0, 0)    
WHITE = pygame.Color(255, 255, 255)    
PINK = pygame.Color(255, 192, 203)    
FPS = 60     
FramePerSec = pygame.time.Clock()    
screen = pygame.display.set_mode((1024, 526))    
screen.fill(GREY)    
to_up = False    
to_down = False    
speed_gg_up = 132    
speed_gg_down= 56    
gg = pygame.image.load('gg.png')    
gg_x = 56    
gg_y = 264    
while True:    
    for event in pygame.event.get():    
        if event.type == pygame.KEYDOWN:    
            if event.key== pygame.K_UP:    
                to_up = True    
            if event.key == pygame.K_DOWN:    
                to_down = True    
        if event.type == QUIT:    
            pygame.quit()    
            sys.exit()    
    FramePerSec.tick(FPS)    
    if to_up:    
        gg_y -= speed_gg_up    
        time.sleep(2)    
        gg_y  = speed_gg_up    
        to_up = False    
    if to_down:    
        gg_y  =speed_gg_down    
        time.sleep(2)     
        gg_y -= speed_gg_down    
        to_down = False    
    screen.blit(gg, (gg_x, gg_y))     
    pygame.display.update()    

the code works without errors, but gives only an empty window. no matter how much I change the code, nothing changes please help me find and fix the error so that the code starts showing the image

CodePudding user response:

You have to clear the display in every frame. Do not make the game sleep. Use pygame.time.get_ticks() to measure the time in milliseconds:

import pygame    
import sys    
from pygame.locals import *    
 
GREY = pygame.Color(128, 128, 128)        
FPS = 60     
clock = pygame.time.Clock()    
screen = pygame.display.set_mode((1024, 526))         
speed = 1  
gg = pygame.image.load('gg.png') 
gg_x = 56    
gg_y = 264    
to_up = False    
to_down = False    
speed_gg_up = 132    
speed_gg_down= 56 
move_back_time = 0

run = True
while run:    

    current_time = pygame.time.get_ticks()
    for event in pygame.event.get():            
        if event.type == QUIT:  
            run = False  
        if event.type == pygame.KEYDOWN:    
            if event.key== pygame.K_UP and not to_up and not to_down:
                to_up = True    
                gg_y -= speed_gg_up
                move_back_time = current_time   2000
            if event.key == pygame.K_DOWN and not to_up and not to_down:    
                to_down = True    
                gg_y  = speed_gg_down
                move_back_time = current_time   2000

    if move_back_time < current_time:
        if to_up:
            to_up = False
            gg_y  = speed_gg_up
        if to_down:
            to_down = False
            gg_y -= speed_gg_down
     
    screen.fill(GREY)     
    screen.blit(gg, (gg_x, gg_y))     
    pygame.display.update()  
    clock.tick(FPS)  

pygame.quit()    
sys.exit()  

You can also store the movement in a list an move back after some time:

import pygame    
import sys    
from pygame.locals import *    
 
GREY = pygame.Color(128, 128, 128)        
FPS = 60     
clock = pygame.time.Clock()    
screen = pygame.display.set_mode((1024, 526))         
speed = 1  
gg = pygame.image.load('gg.png') 
gg_x = 56    
gg_y = 264      
speed_gg_up = 132    
speed_gg_down= 56 
movement = []
run = True
while run:    

    current_time = pygame.time.get_ticks()
    for event in pygame.event.get():            
        if event.type == QUIT:  
            run = False  
        if event.type == pygame.KEYDOWN:    
            if event.key== pygame.K_UP:
                gg_y -= speed_gg_up
                movement.append((-speed_gg_up, current_time))
            if event.key == pygame.K_DOWN:    
                gg_y  = speed_gg_down
                movement.append((speed_gg_down, current_time))

    if len(movement) > 0 and current_time >= movement[0][1]   2000:
        gg_y -= movement[0][0]
        del movement[0]
     
    screen.fill(GREY)     
    screen.blit(gg, (gg_x, gg_y))     
    pygame.display.update()  
    clock.tick(FPS)  

pygame.quit()    
sys.exit()  
  • Related