RGB Flashing Text in Pygame

 RGB Flashing Text in  Pygame

In this post you will learn to create flashing text in Pygame. Complete source code with proper detail explanation is below. For more such useful content, share it.



Logic and Explanation

First thing we have to do is to install Pygame to our computer. For this you just have to write the below command in your terminal. You can use any Windows PowerShell or Command Prompt or terminal of your Python IDE.


pip install pygame

After installing Pygame, we will import this library in our project file.


import pygame

Then we will initialize the Pygame functions by writing the following code.


pygame.init()

Then next task is to create a Pygame window on which we will place our text. Here is the code to create a Pygame window of 400 x 400 resolution.


import pygame
pygame.init()

screen = pygame.display.set_mode((400,400))
pygame.display.set_caption("Blinking Text by Bhavik Agarwal")

runcode = True
while runcode:

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

    pygame.display.update()

pygame.quit()

On running the above code you will find that a blank window opens with black background as shown below.



Now we will define some colours like Red, Blue and Green for our text. Also, we will creating a colour list whose elements will be these colours. The logic is that we will loop through the list and take value of colour and will increase the index of colour which we are taking for the text by one after a particular period of time.


#Defining Colors
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
COLOR_LIST = [RED,GREEN,BLUE]


Now, let's define the font for our text which we will display.


font = pygame.font.SysFont("Consolas", 40)
Now, we will create a function to draw text on our Pygame window. This function takes text, font, colour of text and x and y co-ordinates of the centre of the text as parameter. We convert the text into an image using font.render  function and then by using rect property of a image we place the centre of image rectangle at x and y co-ordinates.

def draw_text(text, font, text_col, x,y):
    img = font.render(text, True, text_col)
    rect = img.get_rect()
    rect.center = (x,y)
    screen.blit(img, rect)
Our next step would be defining some important variables. Our first variable is for timer. We define a variable t which has initial value of 500 and we reduce it by one each times in our main loop. When value of t becomes zero we change the value of colour by changing our next important variable current_color  which stores the index number of the colour. Initial value of the current_color variable is set to zero.

#Project Variables
t = 500
current_color = 0
This is the main step of our project i.e. changing the colour of text by changing the value current_color variable. For this we create a timer by reducing the value of variable by one each time the loop till it becomes 0. When value of t becomes less than equal to 0 we add 1 to current_color variable and again reset the value of t to 500. We are setting too high value of t because this infinite while loop runs very fast around 400 loops in one second. Now, we check the value of current_color if it is equal to the length of color_list then we reset it to zero. In this way we process through the list of color with timer.

if t > 0:
        t = t-1
if t <= 0:
    t = 500
    current_color += 1
if current_color >=len(COLOR_LIST):
    current_color = 0

Last but the main step is to call our draw_text function in the while loop. In this we give the required parameters. First the text which I had given here as "Hello Everybody". Then, font which we have already defined above. Then color by using COLOR_LIST[current_color] and then will x and y co-ordinates which I am giving as 200, 200 as these the centre of our Pygame window.


draw_text("Hello Everybody",font,COLOR_LIST[current_color],200,200)

That's all when you combine the code and run it you will find the text flashing with Red Blue Green colours on your screen like as shown below. Complete source code for the program is given below. In case of any doubt feel free to ask in the comments below.




Source Code


import pygame
pygame.init()

screen = pygame.display.set_mode((400,400))
pygame.display.set_caption("Blinking Text by Bhavik Agarwal")
font = pygame.font.SysFont("Consolas", 40)

#Project Variables
t = 500
current_color = 0

#Defining colors
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
COLOR_LIST = [RED,GREEN,BLUE]

def draw_text(text, font, text_col, x,y):
    img = font.render(text, True, text_col)
    rect = img.get_rect()
    rect.center = (x,y)
    screen.blit(img, rect)


runcode = True
while runcode:

    if t > 0:
        t = t-1
    if t <= 0:
        t = 500
        current_color += 1
    if current_color >=len(COLOR_LIST):
        current_color = 0

    draw_text("Hello Everybody",font,COLOR_LIST[current_color],200,200)

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

    pygame.display.update()

pygame.quit()


BOOM!! the flashing text is ready. You can run this source code and you will find RGB flashing text on your screen.

I hope you liked the project and it's explanation. For any query feel free to ask in the comments below. Also your appreciation is welcomed in the comments.


Check Similar Project - Digital Clock with Pygame


Check Python Programs Question and Answer Series - Click Here


Enjoy Coding!!!



For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post