1

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)
  • 1
    you need to redraw the complete screen each frame – rioV8 Aug 31 '20 at 11:47
  • `eq_done` causes that the `display_equation` is invoked only once. Do you want to render the `equations` more than once? Just remove `eq_done` from your code. – Rabbid76 Aug 31 '20 at 11:54
  • No i want to render the equation only once. I am pretty sure the eq_done prevents the function to be reiterated over and over again, and makes sure that only one equation is rendered. – Shokhrukh Valiev Sep 01 '20 at 00:43

2 Answers2

0

You need to redraw the complete screen each frame.

Draw the equations just like you draw the clouds.

    if pygame.time.get_ticks() > 5000:
        display_equation()
rioV8
  • 24,506
  • 3
  • 32
  • 49
0

If you want to render the equation for a period of time, then you have to remove eq_done, but you have to test if the current time is less than a certain point of time. For instance:

while not done:
    # [...]

    tks = pygame.time.get_ticks()
    if 5000 < tks < 6000:
        display_equation()

    # [...]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174