Home > database >  how to remove square after 2 seconds
how to remove square after 2 seconds

Time:01-03

I have this code and I don't know how to make the red cube disappear after 2 seconds.

import pygame
import sys
from pygame.locals import *
pygame.init()

a=0

#display
prozor = pygame.display.set_mode((800,800))
FPS = pygame.time.Clock()
FPS.tick(60)

#boje
green=pygame.Color(0 ,255 , 0)
red=pygame.Color(255, 0, 0)
yellow=pygame.Color(255, 255, 0)
blue=pygame.Color(0, 0, 255)
black=pygame.Color(0, 0, 0)
white=pygame.Color(255, 255, 255)


#class

class Cube:
    def update(self):
        self.cx, self.cy = pygame.mouse.get_pos()
        self.square = pygame.Rect(self.cx, self.cy, 50, 50)

    def draw(self): 
        pygame.draw.rect(prozor, (255, 0, 0), self.square)

cube = Cube()
drawing_cube = False

#objekt
for j in range(16):
    for i in range(800):
            if iP == 0:
                pygame.draw.rect(prozor, green, ((0 i, a), (50, 50),),1)
    a=a 50


#gameloop
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit
        if event.type == pygame.MOUSEBUTTONDOWN:
            cube.update()
            drawing_cube = True
    if drawing_cube:
        cube.draw()
        pygame.display.flip()
    pygame.display.update()

Adding it to the draw class will just freeze the game and shut it.

def draw(self): 
    pygame.draw.rect(prozor, red, self.square)
    time.sleep(3)
    pygame.draw.rect(prozor, black, self.square

I tried making another class called delete which would delete the cube after 3 seconds using the time module.

def delete(self):
    time.sleep(3)
    pygame.draw.rect(prozor, black, self.square)

and added it here

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit
        if event.type == pygame.MOUSEBUTTONDOWN:
            cube.update()
            drawing_cube = True
    if drawing_cube:
        cube.draw()
        pygame.display.flip()
        **cube.delete**
        pygame.display.flip()
    pygame.display.update()

but the cube is not disappearing.

CodePudding user response:

Use pygame.time.get_ticks to measure the time in milliseconds. Calculate the time when the cube must disappear again and hide the cube if the current time is greater than the calculated time. You also need to clear the display (prozor.fill(0)) and redraw the scene in each frame:

drawing_cube = False
hide_cube_time = 0

clock = pygame.time.Clock()
run = True
while run:
    clock.tick(100)
    current_time = pygame.time.get_ticks()

    for event in pygame.event.get():
        if event.type == QUIT:
            run = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            cube.update()
            drawing_cube = True
            hide_cube_time = current_time   2000 # 2000 milliseconds == 2 sconds

    if current_time > hide_cube_time:
        drawing_cube = False

    prozor.fill(0)
    a=0
    for j in range(16):
        for i in range(800):
            if iP == 0:
                pygame.draw.rect(prozor, green, (0 i, a, 50, 50), 1)
        a=a 50
    if drawing_cube:
        cube.draw()
    pygame.display.update()

pygame.quit()
sys.exit()

Note, the typical PyGame application loop has to:

  • Related