Pygame Project: Create the simplest digital clock

We will use python datetime module for our clock;

We get the current time and separate the time into hour minutes and second.

First get the current time

import datetime
current_time = datetime.datetime.now()
print(current_time)

Get the time in hour, minute and second

current_hour = current_time.hour
current_minute = current_time.minute
current_second = current_time.second

Now got our clock time components. It will be like this



import datetime

current_time = datetime.datetime.now()

hour = current_time.hour
minute = current_time.minute
second = current_time.second

clock_time = f"{hour}:{minute}:{second}"

print(clock_time)




The code will print the time

Now if we loop the program in every second it will show the the updated time in every second


import time
import datetime
while True:
	current_time = datetime.datetime.now()

	hour = current_time.hour
	minute = current_time.minute
	second = current_time.second

	clock_time = f"{hour}:{minute}:{second}"
	print(clock_time)
	time.sleep(1)


We have completed the main structure of our digital clock

Now more improvement and applying to pygame

In the program instead of this line

clock_time = f"{hour}:{minute}:{second}"

we will use this

clock_time = f"{hour:02}:{minute:02}:{second:02}"

because it will always show the time in two digit . for example if the current second is 9 it will show 09

Now our complete code


import datetime
import pygame,sys
from pygame.locals import*
pygame.init()


clock=pygame.time.Clock()

screen=pygame.display.set_mode((800,600))

clock_font=pygame.font.Font(pygame.font.get_default_font(), 150)



game_running=True
while game_running:
	# It will run 1 frame per second. So the time will update every second
	clock.tick(1)
	for event in pygame.event.get():
		if event.type==QUIT:
			pygame.quit()
			sys.exit()
	current_time = datetime.datetime.now()

	hour = current_time.hour
	minute = current_time.minute
	second = current_time.second

	clock_time = f"{hour:02}:{minute:02}:{second:02}"
	screen.fill((255,255,255))
	clock_text=clock_font.render(clock_time,True,(0,0,0))
	screen.blit(clock_text,(100,200))
	pygame.display.update()



You can Get this and also more pygame example from github ( 50+ pygame example)

https://github.com/01one/pygameExamples

Related Posts

Send Desktop notification from pygame
April 29, 2024

For notification handling we will use plyer library We will add this functionality in our pygame code The complete code import pygame import sys from pygame.locals import * from plyer import notification pygame.init() clock = pygame.time.Clock() user_display = pygame.display.Info() width, height = 800, 600 screen = pygame.display.set_mode((width, height)) background = (255, 255, 255) red = […]

Simulate Bird Flocking Behavior with pygame: Algorithm and Code Explained
March 23, 2024

Bird flocking is a wonderful example of how simple behaviors can lead to complex, beautiful patterns in nature. We’ll learn the key principles behind this flocking behavior and write a python program to simulate this wonderful phenomenon. The principles Bird Flocking Simulation Algorithm Setting Up the Simulation Each Frame of the Simulation The pygame example […]

Display Matplotlib Plots in Pygame: A Step-by-Step Guide
March 16, 2024

Our tasks for this Import necessary module import sys import pygame import matplotlib from pygame.locals import * import matplotlib.pyplot as plt from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas This code imports a tool from Matplotlib that lets you save plots as image files (like PNG or JPG). Use this for […]