Помогите пожалуйста черный экран в pygame

import pygame, sys, time
from pygame.locals import *

pygame.init()

WINDOWWIDTH=400
WINDOWHEIGHT=400

windowSurface=pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT),0,32)
pygame.display.set_caption("Анимация")

DOWNLEFT="downleft"
DOWNRIGHT="downright"
UPLEFT="upleft"
UPRIGHT="upright"

MOVESPEED=4


BLACK=(0,0,0)
WHITE =(255,255,255)
RED=(255,0,0)
GREEN=(0,255,0)
BLUE=(0,0,255)

#Создание структуры даных блока
b1={'rect' : pygame.Rect(300,80,50,100),"color":RED, "dir":UPRIGHT}
b2={"rect":pygame.Rect(200,200,20,20),"color":GREEN, "dir":UPLEFT}
b3={"rect":pygame.Rect(100,150,60,60),"color":BLUE, "dir":DOWNLEFT}

boxes=[b1, b2, b3]

while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()

windowSurface.fill(WHITE)


for b in boxes:
    if b["dir"]==DOWNLEFT:
        b["rect"].left-=MOVESPEED
        b["rect"].top +=MOVESPEED
    if b["dir"]==DOWNRIGHT:
        b["rect"].left+=MOVESPEED
        b["dir"].top+=MOVESPEED
    if b["dir"]==UPLEFT:
        b["rect"].left-=MOVESPEED
        b["rect"].top-=MOVESPEED
    if b["dir"]==UPRIGHT:
        b["rect"].left+=MOVESPEED
        b["rect"].top -=MOVESPEED
        
if b["rect"].top < 0:
    if b["dir"] == UPLEFT:
        b["dir"] = DOWNLEFT
    if b["dir"] == UPRIGHT:
        b["dir"]=  DOWNRIGHT
if b["rect"].bottom > WINDOWHEIGHT:
    if b["dir"] == DOWNLEFT:
        b["dir"] = UPLEFT
    if b["dir"] == DOWNRIGHT:
        b["dir"]=UPRIGHT
if b["rect"].left < 0:
    if b["dir"] == DOWNLEFT:
        b["dir"] = DOWNRIGHT
    if b["dir"] == UPLEFT:
        b["dir"]=UPRIGHT
if b["rect"].right > 0:
    if b["dir"] == DOWNRIGHT:
        b["dir"] = DOWNLEFT
    if b["dir"] == UPRIGHT:
        b["dir"]=UPLEFT



pygame.draw.rect(windowSurface,b["color", b["rect"]])
pygame.display.update()
time.sleep(0.02)

Ответы (2 шт):

Автор решения: FotonPC

У тебя небыли сделаны отступы для второй половины цикла:

import pygame, sys, time
from pygame.locals import *

pygame.init()

FPS = 50
WINDOWWIDTH=400
WINDOWHEIGHT=400

windowSurface=pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT),0,32)
pygame.display.set_caption("Анимация")

DOWNLEFT="downleft"
DOWNRIGHT="downright"
UPLEFT="upleft"
UPRIGHT="upright"
MOVESPEED=4
BLACK=(0,0,0)
WHITE =(255,255,255)
RED=(255,0,0)
GREEN=(0,255,0)
BLUE=(0,0,255)

clock = pygame.time.Clock()

#Создание структуры даных блока
b1={'rect' : pygame.Rect(300,80,50,100),"color":RED, "dir":UPRIGHT}
b2={"rect":pygame.Rect(200,200,20,20),"color":GREEN, "dir":UPLEFT}
b3={"rect":pygame.Rect(100,150,60,60),"color":BLUE, "dir":DOWNLEFT}

boxes=[b1, b2, b3]

while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()

    windowSurface.fill(WHITE)

    for b in boxes:
        if b["dir"]==DOWNLEFT:
            b["rect"].left-=MOVESPEED
            b["rect"].top +=MOVESPEED
        if b["dir"]==DOWNRIGHT:
            b["rect"].left+=MOVESPEED
            b["dir"].top+=MOVESPEED
        if b["dir"]==UPLEFT:
            b["rect"].left-=MOVESPEED
            b["rect"].top-=MOVESPEED
        if b["dir"]==UPRIGHT:
            b["rect"].left+=MOVESPEED
            b["rect"].top -=MOVESPEED
    if b["rect"].top < 0:
        if b["dir"] == UPLEFT:
            b["dir"] = DOWNLEFT
        if b["dir"] == UPRIGHT:
            b["dir"]=  DOWNRIGHT
    if b["rect"].bottom > WINDOWHEIGHT:
        if b["dir"] == DOWNLEFT:
            b["dir"] = UPLEFT
        if b["dir"] == DOWNRIGHT:
            b["dir"]=UPRIGHT
    if b["rect"].left < 0:
        if b["dir"] == DOWNLEFT:
            b["dir"] = DOWNRIGHT
        if b["dir"] == UPLEFT:
            b["dir"]=UPRIGHT
    if b["rect"].right > 0:
        if b["dir"] == DOWNRIGHT:
            b["dir"] = DOWNLEFT
        if b["dir"] == UPRIGHT:
            b["dir"]=UPLEFT

    pygame.draw.rect(windowSurface,b["color", b["rect"]])
    pygame.display.update()
    clock.tick(FPS)
→ Ссылка
Автор решения: S. Nick

Проверьте отступы и некоторые опечатки, но алгоритм я ваш не проверял.

import sys
import time
import pygame
from pygame.locals import *

pygame.init()

WINDOWWIDTH = 400
WINDOWHEIGHT = 400

windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption("Анимация")

DOWNLEFT = "downleft"
DOWNRIGHT = "downright"
UPLEFT = "upleft"
UPRIGHT = "upright"

MOVESPEED = 4


BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

#Создание структуры даных блока
b1 = {
      'rect': pygame.Rect(300, 80, 50, 100), 
      "color": RED,                              
      "dir": UPRIGHT                            
     }
b2 = {"rect": pygame.Rect(200, 200, 20, 20), "color": GREEN, "dir": UPLEFT}
b3 = {"rect": pygame.Rect(100, 150, 60, 60), "color": BLUE, "dir": DOWNLEFT}

boxes = [b1, b2, b3]

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    windowSurface.fill(WHITE)

    for b in boxes:
        print(b["dir"])
        if b["dir"] == DOWNLEFT:
            b["rect"].left -= MOVESPEED
            b["rect"].top += MOVESPEED
        elif b["dir"] == DOWNRIGHT:
            b["rect"].left += MOVESPEED
#            b["dir"].top += MOVESPEED                      # ---
            b["rect"].top += MOVESPEED                      # rect
        elif b["dir"] == UPLEFT:
            b["rect"].left -= MOVESPEED
            b["rect"].top -= MOVESPEED
        elif b["dir"] == UPRIGHT:
            b["rect"].left += MOVESPEED
            b["rect"].top -= MOVESPEED
            
    if b["rect"].top < 0:
        if b["dir"] == UPLEFT:
            b["dir"] = DOWNLEFT
        elif b["dir"] == UPRIGHT:
            b["dir"] = DOWNRIGHT
    elif b["rect"].bottom > WINDOWHEIGHT:
        if b["dir"] == DOWNLEFT:
            b["dir"] = UPLEFT
        elif b["dir"] == DOWNRIGHT:
            b["dir"] = UPRIGHT
    elif b["rect"].left < 0:
        if b["dir"] == DOWNLEFT:
            b["dir"] = DOWNRIGHT
        elif b["dir"] == UPLEFT:
            b["dir"] = UPRIGHT
    elif b["rect"].right > 0:
        if b["dir"] == DOWNRIGHT:
            b["dir"] = DOWNLEFT
        elif b["dir"] == UPRIGHT:
            b["dir"] = UPLEFT

#    pygame.draw.rect(windowSurface, b["color", b["rect"]])     # ---
    pygame.draw.rect(windowSurface, b["color"], b["rect"])      # +++
    pygame.display.update()
    time.sleep(0.02)

введите сюда описание изображения

→ Ссылка