1

I am trying to make a program that displays a still image(IM1) on a screen all the time, then when a gpio port(Relay 1) is brought to ground have a new image(IM2) pop up and alternate between IM2 and its counterpart(IM3). Here is my code as of now:

import RPi.GPIO as GPIO
import pygame
from pygame.locals import *
clock = pygame.time.Clock()
pygame.init()
clock.tick(60)
screen = pygame.display.set_mode((1080, 1920))

IM1 = pygame.image.load("/home/pi/Desktop/Slides/Logo.jpg")

IM2 = pygame.image.load("/home/pi/Desktop/Slides/Works-1.jpg")

IM3 = pygame.image.load("/home/pi/Desktop/Slides/Works-2.jpg")

GPIO.setmode(GPIO.BOARD)
Relay1 = 11
GPIO.setup(Relay1, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while(1):
    if(GPIO.input(Relay1) == 0):
        screen.blit(IM2, (0,0))
        sleep(.5)
        screen.blit(IM3, (0,0))
        sleep(.5)
        pygame.display.update()
    else:
        screen.blit(IM1, (0,0))
        pygame.display.update()

#I can get IM1 to function but when Relay 1 is triggered only IM3 is displayed. I have tried using sprite and cant get that to work i was hoping to get this method to work.

2 Answers2

1

You need to update the display after each blit. Do this:

screen.blit(IM2, (0,0))
pygame.display.update()
sleep(.5)
screen.blit(IM3, (0,0)
pygame.display.update()
sleep(.5)
Armadillan
  • 530
  • 3
  • 15
  • This is not enough, you also need to handle the events. See [`pygame.event.pump()`](https://www.pygame.org/docs/ref/event.html#pygame.event.pump) – Rabbid76 Jan 16 '21 at 07:51
0

The display is updated only if either pygame.display.update() or pygame.display.flip() is called. See pygame.display.flip():

This will update the contents of the entire display.

Further you've to handles the events with pygame.event.pump(), before the update of the display becomes visible in the window.

See pygame.event.pump():

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

If you want to display an image and delay the game, then you've to update the display and handle the events.

screen.blit(IM2, (0,0))
pygame.display.flip()
pygame.event.pump()
pygame.time.delay(delay * 500) # 0.5 second == 500 milliseconds
Rabbid76
  • 202,892
  • 27
  • 131
  • 174