Black screen when running Pygame

3

Previously to ask this question it is worth noting that I have consulted several sources looking for some answer to my problem. None of them seems to have served. Despite trying to solve my problem on my own, rereading the code, the error still exists.

Here is my code:

import pygame,sys
from pygame.locals import *
from random import randint

pygame.init()
ventana = pygame.display.set_mode((1000,1000))
#abajo se encuentra el fondo de la ventana
#ventana.fill(ColorDos)
pygame.display.set_caption("Retouch")

Mi_imagen = pygame.image.load("Image/meflag.png")
Imagen_Dos = pygame.image.load("Image/Kool-Aid.gif")
posX= randint (100, 300)
posY = randint (101, 200)

velocidad=4
Color = (66, 133 ,244)#Azul
ColorDos = pygame.Color(244, 180, 0)#Amarillo
ColorTres = (244,67,54)#Rojo
ColorCuatro = (0,135,68) #verde
derecha=True

rectangulo = pygame.Rect(0,0,100,50)
print posY, posX

# Color = (244,180,0)
#primero donde, despues, color(tupla u objeto), tupla de cordenadas en X, Y. El ultimo parametro es el tamano del radio.
pygame.draw.circle(ventana, Color, (200, 300), 500)
"""Primero donde, despues que color, despues tupla con cuatro valores.
Los primeros dos valores (X,Y) son la esquina izquierda superior.
El tercer valor es el ancho de nuestro rectangulo.
Y el cuarto valor es el alto del rectangulo:"""
pygame.draw.rect(ventana, ColorCuatro,(100,100,100,50) )
"""Primero donde, despues color, despues el tercer parametro es una tupla con tuplas dentro de esta.
Dentro de esa tupla se encuentras las cordenadas, posiciones (X,Y) de los puntos que al final pygame une."""
pygame.draw.polygon(ventana, ColorTres, ((80,90),(200,400), (80,10))  )


while True:
    ventana.fill(ColorDos)
    # ventana.blit(Mi_imagen,(posX, posY))  
    ventana.blit(Imagen_Dos,(posX, posY))
    pygame.draw.rect(ventana, ColorTres,rectangulo)
    rectangulo.left, rectangulo.top = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
"""Este de abajo mueve a Kool-Aid"""
# posX,posY=pygame.mouse.get_pos()
# posX=posX-100
# posY=posY-50

"""Lo de abajo(comentado) mueve la imagen sin parar"""
if derecha==True:
    if posX <400:
        posX+=velocidad
    else:
        derecha=False
else: 
    if posX>1:
        posX-=velocidad
    else:
        derecha=True

#       elif event.type == pygame.KEYDOWN:
#           if event.key == K_LEFT:
#               posX-=velocidad
#           elif event.key == K_RIGHT:
#               posX+=velocidad




pygame.display.update()

How can I resolve this error (black screen)? How can I avoid this kind of error?

    
asked by G. Michel 01.02.2016 в 14:07
source

1 answer

1

It is not an error, you are simply showing elements on the screen but you are not updating it.

You have to update the screen in each iteration. You can do this using pygame.display.update() :

while True:
    # ...
    pygame.display.update()
    for event in pygame.event.get():
        # ...

Update

I noticed that the call to the function you were using in the wrong place, it should be inside the game loop of your program, not outside. Maybe it was just a problem of indentation of your code but the above should work.

For you to be a guide, I leave you the link to the code of a game I did a few years ago, it is a memory game that I hope will help you:

See how all the logic of the game happens within the game loop and how to use the other functions to change the state of the game.

    
answered by 01.02.2016 / 15:49
source