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()
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)
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)
#Project Variables
t = 500
current_color = 0
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!!!
Post a Comment
For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.