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 7493443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:57:36+00:00 2026-05-29T16:57:36+00:00

I wrote a little program to draw a pattern a cycle through different color

  • 0

I wrote a little program to draw a pattern a cycle through different color along with a class for moderating the frame rate. For some reason there is random flickering whenever I run it and I have no idea why. It is very simple so I doubt it has to do with the screen updating quickly enough. I would appreciate any suggestions.

import pygame, time, random

w, h = 640, 480
screen = pygame.display.set_mode((w, h))
running = 1
inc = 20
m = [1, 1, 1]
c = [random.randint(0,255), random.randint(0,255), random.randint(0,255)]


class FrameRate():
    def __init__(self, rate = 60):
        self.frame_rate = rate
        self.refresh_time = 1.0/self.frame_rate
        self.cur_time = time.time()
        self.prev_time = time.time()
        self.elapsed_time = 0

    def update(self):
        temp = self.cur_time
        self.cur_time = time.time()
        self.elapsed_time = self.cur_time - self.prev_time
        self.prev_time = temp

    def regulate_frame_rate(self):
        if self.elapsed_time < self.refresh_time:
            time.sleep(self.refresh_time - self.elapsed_time)
fr = FrameRate()
pygame.init()

while running:

    fr.update()
    fr.regulate_frame_rate()

    event = pygame.event.poll()
    if event.type == pygame.QUIT:
            running = 0

    screen.fill((0, 0, 0))

    # Reverse color direction
    for i, p in enumerate(c):
        if c[i] > 255 or c[i] < 0:
            m[i] = -m[i]
        c[i] += m[i]

    for i in range(0,w/inc):
        try:
            pygame.draw.line(screen, (c[0],c[1],c[2]), (i * inc, 0), (0, h - i*inc))
            pygame.draw.line(screen, (c[0],c[1],c[2]), (w - i * inc, 0), (w, h - i*inc))
            pygame.draw.line(screen, (c[0],c[1],c[2]), (i * inc, h), (0, i*inc))
            pygame.draw.line(screen, (c[0],c[1],c[2]), (w - i * inc, h), (w, i*inc))
        except TypeError:
            pass

    pygame.display.flip()

    pygame.quit()
  • 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-05-29T16:57:38+00:00Added an answer on May 29, 2026 at 4:57 pm

    For one thing, get that “pygame.quit()” out of your main loop.

    The flickering is due to the fact that your drawing code is periodically crashing. When the except in the try-except block is hit, then nothing will be drawn that frame, and the screen will “flicker” black.

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

Sidebar

Related Questions

I wrote a nice little program. At some point, it is required to load
I was wrote a little scratch program to check the time of looping through
I wrote a little program that solves 49151 sudoku's within an hour for an
I've been playing with Python for a while and wrote a little program to
I wrote a little Haskell program to find the area of a triangle, primarily
i wrote a little addin, which does some formatting of my C# code. in
I ported a little Haskell program I wrote from Mac to Windows. It's a
I have this simple little batch file program that I wrote but it fails
I'm a little bit confused about simple program which I wrote, can You please
I wrote a little program, which is working like a ping command(i use ICMPSendEcho2),

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.