[Python] Why is my equation rendering for a millisecond and disappearing

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    So i was making a project where an equation needed to be rendered onto the screen at a certain point in time, and i accomplished that. But i ran into a problem where the equation only gets rendered for a millisecond and then disappears. I traced the problem down to the 'screen.fill((102, 178, 255)). When this piece of code is in the main loop the equation only renders for as i said.. a millisecond, and if that piece of code is outside of the main loop the problem is solved BUT other problems emerge when you have sprites that need to move. So is there a way to solve this problem by having the 'screen.fill((102, 178, 255))' in the main loop??

    Removing the eq_done variable from the code will not work because i want only one equation rendered, and if you remove the eq_done variable, random equations will start flashing on the pygame window

    Thanks!

    import pygame
    import random

    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    clock = pygame.time.Clock()
    done = False

    equations = ['2 + 2', '3 + 1', '4 + 4', '7 - 4']


    font = pygame.font.SysFont("comicsansms", 72)

    tks = pygame.time.get_ticks()

    cloud1 = pygame.image.load('cloud.png')
    cloud1_X, cloud1_Y = 100, 50
    cloud1_Y_change = 0

    def cloud1_display(x, y):
    screen.blit(cloud1, (x, y))


    def display_equation():
    text = font.render(random.choice(list(equations)), True, (0, 128, 0))
    screen.blit(text, (320 - text.get_width() // 2, 240 - text.get_height() // 2))

    rocket_up = False
    eq_done = False
    while not done:
    screen.fill((102, 178, 255))
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    done = True
    if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
    done = True

    tks = pygame.time.get_ticks()
    if tks > 5000 and not eq_done:
    display_equation()
    eq_done = True # only render once

    cloud1_display(cloud1_X, cloud1_Y)
    pygame.display.update()
    clock.tick(60)

    Continue reading...

Compartilhe esta Página