Python Forum
[pygame] transparent rects
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[pygame] transparent rects
#1
I made a block rectangle that is supposed to slowly go from 255 transparency to 0. I am having problems though. I don't understand this because I can slowly make it darken, but I can't get it to slowly lighten. Thanks in Advance. Here is the code -
For lightening it slowly. BTW - the indentations are weird because this is all inside a function.
#Presets
    interval = time.time()
    transparency = 255
    cover = pygame.Surface((500, 500), pygame.SRCALPHA)
    
    #Cover Screen
    while time.time() - interval < 10:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                interval -= 10

        cover.fill((0, 0, 0, transparency))
        Screen.blit(cover, (0, 0))
        if transparency != 0:
            transparency -= 1
        time.sleep(0.05)
        pygame.display.update()
        Clock.tick(fps)
That code won't work, but this code for darkening the transparency does work.
#Presets
    interval = time.time()
    transparency = 0
    cover = pygame.Surface((500, 500), pygame.SRCALPHA)
    
    #Cover Screen
    while time.time() - interval < 10:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                interval -= 10

        cover.fill((0, 0, 0, transparency))
        Screen.blit(cover, (0, 0))
        if transparency != 255:
            transparency += 1
        time.sleep(0.05)
        pygame.display.update()
        Clock.tick(fps)
Reply
#2
Your code failed do to not clearing background first.
You need to clear background with a solid color then blits cover.
Lets also get ride of that time.sleep.
#Presets
    interval = 100 # 100 milliseconds = 0.1 seconds
    next_tick = interval + pygame.time.get_ticks()
    trans_direction = -1
    cover = pygame.Surface((500, 500), pygame.SRCALPHA)
    cover_color = pygame.Color('black')
     
    #Cover Screen
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        
        ticks = pygame.time.get_ticks()
        if ticks > next_tick:
            next_tick += interval

            cover_color.a += trans_direction
            if cover_color.a < 1:
                trans_direction = 1
            if cover_color.a > 254:
                trans_direction = -1

            cover.fill(cover_color)

        # Need to clear screen.
        Screen.fill(pygame.Color('white'))
        Screen.blit(cover, (0, 0))

        pygame.display.update()
        Clock.tick(fps)
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Thanks, I don't quite understand it now, but I'll try to break it all down.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pygame, sprites, and rects menator01 12 1,707 Dec-07-2023, 02:37 AM
Last Post: Benixon
  display error, png with transparent background flash77 4 2,121 Mar-19-2022, 10:20 AM
Last Post: flash77

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020