Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8857761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:40:13+00:00 2026-06-14T14:40:13+00:00

I want to fade in a rectangle from alpha = 0 to alpha =

  • 0

I want to fade in a rectangle from alpha = 0 to alpha = 255 over 2 seconds. Using the following code, I am able to get the rectangle fade in but I don’t know to get the duration to be exactly (as exact as possible) 2 seconds.

pygame.init()

#frames per second setting
FPS = 30
fpsClock = pygame.time.Clock()

#Set up window
screen = pygame.display.set_mode((1280,800))
shape = screen.convert_alpha()

#Colors
alpha = 0
WHITE = (255, 255, 255,alpha)
BLACK = (0,0,0)

#Stimuli
stimulus = pygame.Rect(100,250,100,100)

def fade():
    """Fades in stimulus"""
    global alpha
    alpha = alpha + 5   <--- (Do I change this increment?)
    #Draw on surface object
    screen.fill(BLACK)
    shape.fill(BLACK)
    pygame.draw.rect(shape,(255, 255, 255,alpha),stimulus)
    screen.blit(shape,(0,0))
    pygame.display.update(stimulus)
    fpsClock.tick(FPS)

while True:
    fade()

Additionally, should I use the flags HWSURFACE and DOUBLEBUF if I am using fullscreen? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:40:14+00:00Added an answer on June 14, 2026 at 2:40 pm

    I would try to measure actual time passed since the operation start and make alpha directly proportional to the remaining time.

    Something along the lines:

    import time
    
    def fade():
        DURATION = 2.0 # seconds
        start_time = time.clock()
        ratio = 0.0 # alpha as a float [0.0 .. 1.0]
        while ratio < 1.0:
            current_time = time.clock()
            ratio = (current_time - start_time) / DURATION
            if ratio > 1.0: # we're a bit late
                ratio = 1.0
            # all your drawing details go in the following call
            drawRectangle(coordinates, alpha = 255 * ratio)
            fpsClock.tick(FPS)
    

    This way, if tick works imperfectly (which it does), your alpha follows its imperfections, instead of accumulating the error. When 2 seconds are up, you’re guaranteed to have drawn a fully opaque rectangle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a kind of weird problem: I want to fade images. The Code
Hi when I add a new component using Actionscript I want it to fade
I have to make something like an accordion, but I want it to fade
There was a fade out sample in the internet.. http://docs.dojocampus.org/dojo/fadeOut?t=tundra but i want to
Assume you have a list item, <li id=foo> which you want to fade from
I want to fade an element from visible to invisible. I know I can
I want to fade some elements (rectangles and ellipses) from the accent color to
I've got a fade out animation using ENTER_FRAME. I want the fade out to
I want to fade a WPF window in, but I can't do that unless
I want to fade in a div on my website in 5 seconds. Also,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.