title: Best way to load images pygame
body: <p>Currently, I am loading all images at the start of the program. Is there a better solution for this? As some images may not be used at all throughout the entire game (I'm currently making a main menu).</p>
  <p>Main.py</p>
  <pre><code>import pygame as pg
  from Menu import Menu
  
  pg.init()
  &quot;&quot;&quot;Displays screen&quot;&quot;&quot;
  winWidth = 1920
  winHeight = 1080
  size = (winWidth, winHeight)
  
  win = pg.display.set_mode(size)
  
  
  &quot;&quot;&quot;Load images&quot;&quot;&quot;
  bg_img = pg.image.load('Pic/td-gui/PNG/menu/bg.png')
  title_img = pg.image.load('transtitle.png')
  settings_img = pg.image.load('Pic/td-gui/PNG/menu/button_settings.png')
  play_img = pg.image.load('Pic/td-gui/PNG/menu/button_play.png')
  reg_img = pg.image.load('Pic/td-gui/PNG/menu/button_registration.png')
  quit_img = pg.image.load('Custom/Menu/Quit.png')
  rope_img = pg.image.load('Pic/td-gui/PNG/menu/rope_small.png')
  changeDiff_img = pg.image.load('Custom/Menu/Change_Difficulty.png')
  score_img = pg.image.load('Custom/Menu/Scoreboard.png')
  selectmap_img = pg.image.load('Custom/Menu/Select_map.png')
  tutorial_img = pg.image.load('Custom/Menu/Tutorial.png')
  
  
  
  &quot;&quot;&quot;Puts loaded images into the correct size&quot;&quot;&quot;
  bg_img = pg.transform.scale(bg_img,(winWidth, winHeight))
  title_img = pg.transform.scale(title_img, (750, 75))
  play_img = pg.transform.scale(play_img, (250, 250))
  rope_img = pg.transform.scale(rope_img, (27, 290))
  
  
  
  
  clock = pg.time.Clock()
  
  win.blit(bg_img, (0, 0))
  mainMenu = True
  settingsMenu = False
  run = True
  running = True
  while run:
      clock.tick(30)
      pg.display.update()
      for event in pg.event.get():
          if event.type == pg.QUIT:
              run = False
              if not run:
                  pg.quit()
      &quot;&quot;&quot;Places correct images in specified location&quot;&quot;&quot;
      title = Menu(winWidth // 2 - 375, winHeight // 2 - 500, title_img)
  
      if mainMenu:
          reg_button = Menu(winWidth // 2 - 150, winHeight // 2 - 100, reg_img)
          ropeR = Menu(winWidth // 2 - 100, 200, rope_img)
          ropeL = Menu(winWidth // 2 + 70, 200, rope_img)
          scoreboard_button = Menu(winWidth//2 - 330, winHeight//2 + 275, score_img)
          play_button = Menu(winWidth // 2 - 125, winHeight // 2 - 375, play_img)
          reg_button = Menu(winWidth // 2 - 161, winHeight // 2 - 100, reg_img)
          quit_button = Menu(winWidth//2 + 30, winHeight//2 + 275, quit_img)
          changeDiff_button = Menu(winWidth//2 - 330, winHeight//2 + 80, changeDiff_img)
          selectmap_button = Menu(winWidth//2 + 30, winHeight//2 + 80, selectmap_img)
          settings_button = Menu(winWidth - 200, winHeight - 200, settings_img)
  
          &quot;&quot;&quot;Draws images in specified location and makes buttons if specified&quot;&quot;&quot;
          title.draw_img(win)
          if reg_button.draw_button(win):
              print('Reg button clicked')
          ropeL.draw_img(win)
          ropeR.draw_img(win)
          if scoreboard_button.draw_button(win):
              print(&quot;options Clicked&quot;)
          if play_button.draw_button(win):
              print(&quot;Play Clicked&quot;)
          if quit_button.draw_button(win):
              pg.quit()
          if changeDiff_button.draw_button(win):
              print('Change Difficulty button pressed')
          if selectmap_button.draw_button(win):
              print('Select Map button preseed')
          if settings_button.draw_button(win):
              print('Settings button pressed')
              mainMenu = False
              settingsMenu = True
      if settingsMenu:
          win.blit(bg_img, (0, 0))
          title.draw_img(win)
  </code></pre>
  <p>Menu.py</p>
  <pre><code>import pygame as pg
  pg.init()
  
  class Menu():
      def __init__(self, x, y, img):
          self.img = img
          self.rect = self.img.get_rect()
          self.rect.y = y
          self.rect.x = x
          self.clicked = False
  
  
      def draw_button(self, win):
  
          action = False
          #get mouse position
          pos = pg.mouse.get_pos()
          #check mouseover and clicked conditions
          if self.rect.collidepoint(pos):
              if pg.mouse.get_pressed()[0] == 1 and not self.clicked:
                  action = True
                  self.clicked = True
  
          if pg.mouse.get_pressed()[0] == 0:
              action = False
          #draw image
          win.blit(self.img, (self.rect.x, self.rect.y))
          return action
  
      def draw_img(self, win):
          win.blit(self.img, (self.rect.x, self.rect.y))
  </code></pre>
  <p>Say I'm making a music button, which is accessible through the settings button, I would currently load the music button image at the start of the program. Is there a way to make it so the music image would only be loaded once the settings button has been pressed?</p>