Digital Clock with Python Pygame

 Digital Clock with Pygame


In this project we will be creating a digital clock with python using Pygame. Source Code for the project is provided. Major logic behind this is explained below.


Explanation of Code

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


For creating a digital clock, we have to take current time using datetime module. So, we will import the datetime module.


import datetime

Then we will create a basic pygame window. The code for creating a pygame window is below.


import pygame
import datetime
pygame.init()

screen = pygame.display.set_mode((400,200))
pygame.display.set_caption("Clock")

run_clock = True

#Game Loop
while run_clock:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run_clock = False
    
    pygame.display.update()

pygame.quit()

We will define some colors at this point. Color is basically a tuple with it's RGB value.


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

Now let's define the font for our text.


font = pygame.font.SysFont("Consolas",60)


Now, we will create a python function which will take text as parameter and will blit it on the pygame screen on the desired location.

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

Then we will create a function to take current time using the datetime module. In this function, we will blit the time onto the pygame window at centre using the draw_text function which we created before.


def draw_time():
    strTime = datetime.datetime.now().strftime("%H:%M:%S")
    draw_text(strTime, font, WHITE, 20,20)

Now, at this stage we have to create one more function draw_bg() and the use of this function to fill the screen with black color again and again after the time is displayed. In simple word this function will work to clean the old time and thereafter the new time will placed on the screen. If we do not create this function, then time will get printed one over the other on the screen.


def draw_bg():
    screen.fill(BLACK)

Finally we will call our function in game loop.
Here is the complete source code for this project.


Source Code

import pygame
import datetime
pygame.init()

WHITE = (255,255,255)
BLACK = (0,0,0)

screen = pygame.display.set_mode((400,200))
pygame.display.set_caption("Clock")

font = pygame.font.SysFont("Consolas",60)

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

def draw_bg():
    screen.fill(BLACK)

def draw_time():
    strTime = datetime.datetime.now().strftime("%H:%M:%S")
    draw_text(strTime, font, WHITE, 20,20)


run_clock = True

while run_clock:

    draw_bg()
    draw_time()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run_clock = False
    
    pygame.display.update()

pygame.quit()

BOOM!! the clock is ready. You can run this and source code on your pc and you will find a digital clock running 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.


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