How do I use blit() in the proper way in pygame?

Question:

I was trying to make a texture game, but it doesn’t work with no errors. I’ll show the code.

import time
import pygame
from pygame.locals import *

def Main():
    pygame.mixer.init()

    pygame.mixer.music.load(r"C:...OMFG+-+Ice+Cream.mp3")
    pygame.mixer.music.play(-1)

    pygame.init()

    screen = pygame.display.set_mode((640, 480))
    screen.fill((0,0,0))
    pygame.display.set_caption("3 Minutes Left!!!")

    font = pygame.font.Font('freesansbold.ttf', 18)

    intro_1 = font.render('3', True, (255,255,255))
    intro_2 = font.render('Minutes', True, (255,255,255))
    intro_3 = font.render('Left!!!', True, (255,255,255))

    time.sleep(1)
    screen.blit(intro_1, (140, 30)) #This isn't working.
    time.sleep(1)
    screen.blit(intro_2, (340, 30)) #This isn't working.
    time.sleep(1)
    screen.blit(intro_3, (540, 30)) #This isn't working.

Main()

I thought that the code screen.blit would work with while loops. So I tried.

import time
import pygame
from pygame.locals import *

def Main():
    pygame.mixer.init()

    pygame.mixer.music.load(r"C:...OMFG+-+Ice+Cream.mp3")
    pygame.mixer.music.play(-1)

    pygame.init()

    screen = pygame.display.set_mode((640, 480))
    screen.fill((0,0,0))
    pygame.display.set_caption("3 Minutes Left!!!")

    font = pygame.font.Font('freesansbold.ttf', 18)

    intro_1 = font.render('3', True, (255,255,255))
    intro_2 = font.render('Minutes', True, (255,255,255))
    intro_3 = font.render('Left!!!', True, (255,255,255))

    while True: #This isn't working.
        time.sleep(1)
        screen.blit(intro_1, (140, 30)) #This isn't working.
        time.sleep(1)
        screen.blit(intro_2, (340, 30)) #This isn't working.
        time.sleep(1)
        screen.blit(intro_3, (540, 30)) #This isn't working.

Main()

And I started the program. But pygame tab had no response. So i tried the screen.blit in a definition.

import time
import pygame
from pygame.locals import *

def Main():
    pygame.mixer.init()

    pygame.mixer.music.load(r"C:...OMFG+-+Ice+Cream.mp3")
    pygame.mixer.music.play(-1)

    pygame.init()

    screen = pygame.display.set_mode((640, 480))
    screen.fill((0,0,0))
    pygame.display.set_caption("3 Minutes Left!!!")

    font = pygame.font.Font('freesansbold.ttf', 18)

    intro_1 = font.render('3', True, (255,255,255))
    intro_2 = font.render('Minutes', True, (255,255,255))
    intro_3 = font.render('Left!!!', True, (255,255,255))

    while True: #This isn't working.
        screen_blit(screen, intro_1, intro_2, intro_3) #This isn't working.

def screen_blit(screen, intro_1, intro_2, intro_3): #This isn't working.
    time.sleep(1)
    screen.blit(intro_1, (340, 30)) #This isn't working.
    time.sleep(1)
    screen.blit(intro_2, (340, 30)) #This isn't working.
    time.sleep(1)
    screen.blit(intro_3, (340, 30)) #This isn't working.

Main()

Again, No Response. How can I fix this???

Asked By: jinwon2

||

Answers:

The window is not responding, because you don’t handle the events. You don’t see anything in the window, because you don’t update the display.
A minimal typical PyGame application

e.g:

def Main():
    # [...]

    while True: #This isn't working.

        # handle the events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.quit()

        # draw the scene
        screen.blit(intro_1, (340, 30))

        # update the display
        pygame.display.flip()
Answered By: Rabbid76

You need to update the screen every time you draw/blit something to it, otherwise the changes won’t show.

It doesn’t matter whether you change things inside a loop or in a function, what matters is that you update the screen by calling either pygame.display.flip(), which updates the entire screen, or pygame.display.update(), which updates the entire screen or the rectangle passed to it. In this example I used the rectangle returned by blitting to only update the specific area, and also added a simple game loop that handles events, so your game will be responsive and you can exit it:

import time
import pygame
from pygame.locals import *

def Main():
    pygame.mixer.init()

    pygame.mixer.music.load(r"C:...OMFG+-+Ice+Cream.mp3")
    pygame.mixer.music.play(-1)

    pygame.init()

    screen = pygame.display.set_mode((640, 480))
    screen.fill((0,0,0))
    pygame.display.set_caption("3 Minutes Left!!!")
    pygame.display.update() # Update screen after filling it

    font = pygame.font.Font('freesansbold.ttf', 18)

    intro_1 = font.render('3', True, (255,255,255))
    intro_2 = font.render('Minutes', True, (255,255,255))
    intro_3 = font.render('Left!!!', True, (255,255,255))

    time.sleep(1)
    pygame.display.update(screen.blit(intro_1, (140, 30)))
    time.sleep(1)
    pygame.display.update(screen.blit(intro_2, (340, 30))) 
    time.sleep(1)
    pygame.display.update(screen.blit(intro_3, (540, 30)))

    while pygame.event.wait().type != QUIT:
        pass

Main()
Answered By: chuck