Thursday, 10 July 2025

Learning Pygame -1


Purpose is a profound word. This summer, I’m diving into a project to transform the physics, math, and structure of our world into code, creating visuals to see how everything connects. It might sound like a big goal, but I don’t need a complete map to start—just a clear path.

I’m most confident in Python right now, and I see it as the perfect foundation, especially since it’ll make integrating AI and machine learning into my systems seamless down the road. My first step toward this visual exploration is learning Pygame, a tool to bring my ideas to life through interactive visuals.

My strategy to learn:

The best way to learn anything is to create more and more. Most of the technical skills are procedural therefore the best way to master them is to learn by the experience of creation.

So inorder to intigrate that into my learning journey I will create a challenge for myself for each topic I learna and tackle it without any external aid.

Documents I am using:

I feel like the purest form of the content is at its source. So I will be refering to the official documentation of the Pygame library.This

Official Pygame site

First step is to ofc install the library ..... did that

import pygame

pygame.init()

pygame.quit()

Everything goes within that initialization and quiting of the library.

Things I learnt first:

1. Building a screen, with certain dimensions 
    screen = pygame.display.set_mode((width,height))

2. Recording the possible events that a user may cause
    pygame.event.get()

3. Changing backgrounds
    screen.fill(*color in (R,G,B) format*)
    pygame.display.update

Challenge 1

So with all the knowledge aquired up until now 
I should be able to change the background color of the screen to random colors on occuring of specific events like moving the  mouse or pressing any key on the keyboard.
import pygame, sys, random  
pygame.init()


running = True
screen = pygame.display.set_mode((640,240))
while running == True:
    for event in pygame.event.get():
        print(event)
       
        if event.type == pygame.MOUSEMOTION:
            R = random.randint(0,255)
            G = random.randint(0,255)
            B = random.randint(0,255)
            color = (R,G,B)
            screen.fill(color)
            pygame.display.update()

        if event.type == pygame.QUIT:
            running = False
           

pygame.quit()

Result



On pressing the different keys of the key board, we can change the background color.
For allowing the motion of mouse to be able to change the colors we just have to change 

event.type == pygame.KEYDOWN ------> to --------> event.type == pygame.MOUSEMOTION

pygame.locals is the module that contains these constants .... they have all these events, so once you import this 
import pygame
from pygame.locals import *
you can just write KEYDOWN instead of pygame.KEYDOWN

Learning Pygame -1

Purpose is a profound word. This summer, I’m diving into a project to transform the physics, math, and structure of our world into code, cre...