Pygame

Send Desktop notification from pygame

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 = […]

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 Get the time in hour, minute and second Now got our clock time components. It will be like this import datetime current_time = datetime.datetime.now() hour = current_time.hour […]

Simulate Bird Flocking Behavior with pygame: Algorithm and Code Explained

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

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 […]

How to take screenshot in pygame

We can take screenshot in pygame with pygame.image.save(screen, “screenshot.png”) And this is the example program for this If we directly add this in the pygame loop that will continuously take the screenshot. To solve this we will add a button so when the user click the button the program will take the screenshot. This more […]

How to create bouncing ball animation in pygame

In this article we will explore and write the popular bouncing ball animation with pygame. First make the logical step in our mind. What we want? We want to create a bouncing ball animation? How it will bounce? It will bounce and move until find the edge of the display. The the ball will through […]

How to Create Sprite Animation with Pygame’s Sprite Class

Pygame’s sprite class makes it easy to animate and handle sprite easily without write the heavy complex code. You can create animation without pygame sprite class but build in features like sprite class will help you write clean code more easily. Lets get started Step 1: Project Setup Create a dedicated folder for your project.Inside […]

How to control music volume in pygame

Pygame includes a dedicated module, pygame.mixer.music, for managing background music playback within your games. The set_volume() Function: Your Primary Volume Control The pygame.mixer.music.set_volume() function serves as the central method for adjusting music volume. It accepts a floating-point value between 0.0 (representing silence) and 1.0 (representing maximum volume) to provide fine-grained control. We can set the […]

Design Awesome Pygame Menus: Create a Dropdown Menu

First import the necessary libraries import pygame import sys from pygame.locals import * pygame.init() Establish the Visual Foundation (Game Window) screen = pygame.display.set_mode((1200, 600), RESIZABLE) Set Up Visuals and Sizing (Colors & Constants) # Define colors using RGB values and hexadecimal codes colors = { “background”: (240, 240, 240), “text”: (50, 50, 50), “button_bg”: (200, […]

How to get the mouse position in pygame

We can get the mouse position with pygame.mouse.get_pos() import pygame,sys from pygame.locals import* pygame.init() screen=pygame.display.set_mode((400,400)) game_running=True while game_running: for event in pygame.event.get(): if event.type==QUIT: pygame.quit() sys.exit() mouse_position=pygame.mouse.get_pos() print(mouse_position) pygame.display.update() Copy Run this code and hover your mouse on the pygame window. This will print mouse position. We can me this interesting by moving a rectangle […]

How To Set Background Color In Pygame

import pygame,sys from pygame.locals import* pygame.init() screen=pygame.display.set_mode((600,400)) game_running=True while game_running: for event in pygame.event.get(): if event.type==QUIT: pygame.quit() sys.exit() screen.fill((255,250,240)) pygame.display.update() Copy Lets write the step by step full code and explanation First import necessary library And then initiate the pygame Set the our game display resolution Set the game in a loop so it will […]