logo
TOOLS TO HELP YOU LEARN
teachers, students, everyone
Tools | Blog | Account | Learn

Functioning code to display images in a directory randomly

import pygame
from random import randint
import time
import os
import glob

pygame.init()
#screen = pygame.display.set_mode((500, 500), HWSURFACE | DOUBLEBUF | RESIZABLE)

display_width = 800
display_height = 600

path = os.path.abspath(__file__)
dir_path = os.path.dirname(path)
print (dir_path)
files=glob.glob(dir_path+"/*.jpg")
for file in files:
print(file)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Image Slideshow')

black = (0,0,0)
white = (255,255,255)

clock = pygame.time.Clock()
crashed = False
carImg = pygame.image.load('clouds.jpg')

def car(x,y,nname):
arImg = pygame.image.load(nname)
arImg = pygame.transform.scale(arImg, (display_width, display_height))
gameDisplay.blit(arImg, (x,y))

x = (display_width * 0.01)
y = (display_height * 0.01)

while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True

gameDisplay.fill(white)
myRint = randint(0,(len(files)-1))

print (myRint)
car(x,y,files[myRint])
time.sleep( 5 )

pygame.display.update()
clock.tick(60)

pygame.quit()
quit()

'''
if myRint == 1:
car(x,y,"CatherineHall.jpg")
if myRint == 2:
car(x,y,"MarcEripret.jpg")
'''