Pygame Tutorial : Our first program

Mashiur Rahman
2 min readApr 10, 2023

--

We will open a pygame window in our first lesson.

  • will will import pygame first in our program
  • set up the display size width and height in pixel number
  • and then we will run the program

the code

import pygame
pygame.display.set_mode((400,400))

Close the program by closing the terminal
You have noticed a problem on quieting the pygame window.
we have to add an if statement so that we can close the game by clicking on the close button.

import pygame,sys                           #import necessary module
from pygame.locals import*
pygame.init() #initialize imported pygame modules
pygame.display.set_mode((400,400))

game_running=True
while game_running: #game loop keeps the game running until you exit the game.
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()

Lets Make Pygame window resizable so that we can adjust with display size

import pygame, sys
from pygame.locals import*
pygame.init()
pygame.display.set_mode((400,400),RESIZABLE) #click on the maximize button to see the full screen

game_running=True
while game_running:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
# there's something wrong. we made a little change but that is not correctly appeared on the screen.
#as we made the little change(resized the display) we have to update the display so that our little change
#can appear on the screen. ..check the next program..

There’s something wrong. we made a little change but that is not correctly appeared on the screen.
As we made the little change(resized the display) we have to update the display so that our little change
can appear on the screen. ..check the next program..

import pygame, sys
from pygame.locals import*
pygame.init()
pygame.display.set_mode((400,400),RESIZABLE) #click on the maximize button to see the full screen

game_running=True
while game_running:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update() # now the display 'll continuously update until you exit the game.

Now the display ‘ll continuously update until you exit the game.

Set your program title(your program name)

import pygame,sys
from pygame.locals import*
pygame.init()
pygame.display.set_mode((400,400),RESIZABLE)
pygame.display.set_caption("Your Program Name") #pygame.display.set_caption("your program title")

game_running=True
while game_running:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update()

Visit github and view and download all of my pygame examples

https://github.com/01one/Pygame-Examples

--

--