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