The screen appears black every time I run it

Question:

I’m making a wordle game with pygame but when I run it, the screen appears black even though I tried to fill it with white. I can’t tell if there are any errors since I’m just starting to use pygame and it doesn’t tell there are any.

I’ve looked at other posts to see if they have my problem but I haven’t found anything, or maybe I just can’t tell similarities. I expected to see 30 white boxes appear, ready to be filled with letters. but I got a black screen instead

import pygame
import os
import json

pygame.init()
WIDTH, HEIGHT = 560, 700


BACKGROUND = pygame.display.set_mode((WIDTH,HEIGHT))
FPS = 60
pygame.display.set_caption("Wordle")

#COLORS
WHITE = (255,255,255)
BLACK = (0,0,0)
GREY = (150,150,150)
YELLOW = (255,255,0)
GREEN = (0,255,0)
BACKGROUND.fill(WHITE)

#FONTS
FONT = pygame.font.SysFont("arial", 40)

class VARS:
    LETTER = 0
    ROW = 0

WORD = ["h","e","l","l","o"]
grid = [
    ["","","","",""],
    ["","","","",""],
    ["","","","",""],
    ["","","","",""],
    ["","","","",""],
    ["","","","",""]
]

AlphaB = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

def ASSIGN(word):
    a = 0
    for i in word:
        if i in WORD:
            if WORD[a] == word[a]:
                grid[VARS.ROW][a]+="g"
            else:
                grid[VARS.ROW][a]+="y"
        a+=1
    return grid

def LETTERS(key):
    if key in AlphaB:
        if VARS.LETTER<5:
            grid[VARS.ROW][VARS.LETTER] = key
            VARS.LETTER+=1
            return VARS.LETTER
    elif key == "backspace":
        if VARS.LETTER > 0:
            VARS.LETTER-=1
            grid[VARS.ROW][VARS.LETTER] = ""
            return VARS.LETTER, grid
    else:
        return


def UPDATE():
    BACKGROUND.fill(WHITE)
    z = 0
    w = 0
    for y in grid:
        w = 0
        for x in y:
            if len(x) == 0:
                pygame.draw.rect(BACKGROUND, BLACK, pygame.Rect(30+w*126, 30+z*126, 96, 96),2)
            elif len(x) == 1:
                pygame.draw.rect(BACKGROUND, BLACK, pygame.Rect(30+w*126, 30+z*126, 96, 96),2)
                letter = FONT.render(x, True, BLACK)
                letterRect = letter.get_rect()
                letterRect.center = (78+w*126, 78+z*126)
                BACKGROUND.blit(letter,letterRect)
            elif len(x) == 2:
                if x[1] == "g":
                    pygame.draw.rect(BACKGROUND, GREEN, pygame.Rect(30+w*126, 30+z*126, 96, 96))
                    letter = FONT.render(x[0], True, WHITE)
                    letterRect = letter.get_rect()
                    letterRect.center = (78+w*126, 78+z*126)
                    BACKGROUND.blit(letter,letterRect)
                elif x[1] == "y":
                    pygame.draw.rect(BACKGROUND, YELLOW, pygame.Rect(30+w*126, 30+z*126, 96, 96))
                    letter = FONT.render(x[0], True, WHITE)
                    letterRect = letter.get_rect()
                    letterRect.center = (78+w*126, 78+z*126)
                    BACKGROUND.blit(letter,letterRect)
                elif x[1] == "r":
                    pygame.draw.rect(BACKGROUND, GREY, pygame.Rect(30+w*126, 30+z*126, 96, 96))
                    letter = FONT.render(x[0], True, WHITE)
                    letterRect = letter.get_rect()
                    letterRect.center = (78+w*126, 78+z*126)
                    BACKGROUND.blit(letter,letterRect)
            w += 1
        z+=1
def main():
    Running = True
    clock = pygame.time.Clock()
    while Running:
        clock.tick(FPS)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                Running = False
                pygame.quit()
            if event.type == pygame.KEYUP:
                LETTERS(pygame.key.name(event.key))

        UPDATE()
if __name__ == "__main__":
    main()
Asked By: Phat Duck

||

Answers:

It was as simple as adding

pygame.display.flip()

To the update() function.

Full code:

import pygame
import os
import json

pygame.init()
WIDTH, HEIGHT = 560, 700

BACKGROUND = pygame.display.set_mode((WIDTH, HEIGHT))
FPS = 60
pygame.display.set_caption("Wordle")

# COLORS
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREY = (150, 150, 150)
YELLOW = (255, 255, 0)
GREEN = (0, 255, 0)
BACKGROUND.fill(WHITE)

# FONTS
FONT = pygame.font.SysFont("arial", 40)


class VARS:
    LETTER = 0
    ROW = 0


WORD = ["h", "e", "l", "l", "o"]
grid = [
    ["", "", "", "", ""],
    ["", "", "", "", ""],
    ["", "", "", "", ""],
    ["", "", "", "", ""],
    ["", "", "", "", ""],
    ["", "", "", "", ""]
]

AlphaB = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
          "w", "x", "y", "z"]


def ASSIGN(word):
    a = 0
    for i in word:
        if i in WORD:
            if WORD[a] == word[a]:
                grid[VARS.ROW][a] += "g"
            else:
                grid[VARS.ROW][a] += "y"
        a += 1
    return grid


def LETTERS(key):
    if key in AlphaB:
        if VARS.LETTER < 5:
            grid[VARS.ROW][VARS.LETTER] = key
            VARS.LETTER += 1
            return VARS.LETTER
    elif key == "backspace":
        if VARS.LETTER > 0:
            VARS.LETTER -= 1
            grid[VARS.ROW][VARS.LETTER] = ""
            return VARS.LETTER, grid
    else:
        return


def UPDATE():
    BACKGROUND.fill(WHITE)
    z = 0
    w = 0
    for y in grid:
        w = 0
        for x in y:
            if len(x) == 0:
                pygame.draw.rect(BACKGROUND, BLACK, pygame.Rect(30 + w * 126, 30 + z * 126, 96, 96), 2)
            elif len(x) == 1:
                pygame.draw.rect(BACKGROUND, BLACK, pygame.Rect(30 + w * 126, 30 + z * 126, 96, 96), 2)
                letter = FONT.render(x, True, BLACK)
                letterRect = letter.get_rect()
                letterRect.center = (78 + w * 126, 78 + z * 126)
                BACKGROUND.blit(letter, letterRect)
            elif len(x) == 2:
                if x[1] == "g":
                    pygame.draw.rect(BACKGROUND, GREEN, pygame.Rect(30 + w * 126, 30 + z * 126, 96, 96))
                    letter = FONT.render(x[0], True, WHITE)
                    letterRect = letter.get_rect()
                    letterRect.center = (78 + w * 126, 78 + z * 126)
                    BACKGROUND.blit(letter, letterRect)
                elif x[1] == "y":
                    pygame.draw.rect(BACKGROUND, YELLOW, pygame.Rect(30 + w * 126, 30 + z * 126, 96, 96))
                    letter = FONT.render(x[0], True, WHITE)
                    letterRect = letter.get_rect()
                    letterRect.center = (78 + w * 126, 78 + z * 126)
                    BACKGROUND.blit(letter, letterRect)
                elif x[1] == "r":
                    pygame.draw.rect(BACKGROUND, GREY, pygame.Rect(30 + w * 126, 30 + z * 126, 96, 96))
                    letter = FONT.render(x[0], True, WHITE)
                    letterRect = letter.get_rect()
                    letterRect.center = (78 + w * 126, 78 + z * 126)
                    BACKGROUND.blit(letter, letterRect)
            w += 1
        z += 1
    pygame.display.flip()


def main():
    Running = True
    clock = pygame.time.Clock()
    while Running:
        clock.tick(FPS)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                Running = False
                pygame.quit()
            if event.type == pygame.KEYUP:
                LETTERS(pygame.key.name(event.key))

        UPDATE()


if __name__ == "__main__":
    main()
Answered By: Blue Robin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.