1

Hi I'm trying to follow these instructions from my last question: Pygame: how to load 150 images and present each one for only .5sec per trial

This is the code I currently have and I'm unsure where I'm going wrong.

import pygame, glob
pygame.init()
pygame.mixer.init()
clock=pygame.time.Clock()

#### Set up window
window=pygame.display.set_mode((0,0),pygame.FULLSCREEN)
centre=window.get_rect().center
pygame.mouse.set_visible(False)

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


types= '*.tif'
artfile_names= []
for files in types:
    artfile_names.extend(glob.glob(files))

image_list = []
for artwork in artfile_names:
    image_list.append(pygame.image.load(artwork).convert())

##Randomizing
index=0
current_image = image_list[index]

if image_list[index]>= 150:
    index=0

stimulus= pygame.image.load('image_list')
soundPlay=True
def artwork():
    window.blit(stimulus,pygame.FULLSCREEN)

while not soundPlay:
    for imageEvent in pygame.event.get():
        artwork()
        pygame.display.update()
        clock.tick()
Delly
  • 123
  • 1
  • 10

2 Answers2

1

Near the bottom you have the line:

stimulus= pygame.image.load('image_list')

Here you are trying to load an image titled image_list. There's no file extension there, so your OS doesn't recognize the filetype. But even if you did include a file extension, I think you're trying to load the individual images in the list image_list, and that's a whole other story.

paper man
  • 488
  • 5
  • 19
  • Yes, I am trying to load inidividual images in image_list. I thought the indexing would do that. Is there an error in how I set it up? – Delly Nov 15 '17 at 22:05
1

The first mistake is the same as in your previous question: types= '*.tif'. That means types is a string, but you actually wanted a tuple with the allowed file types: types = '*.tif', (the comma turns it into a tuple). So you iterate over the letters in '*.tif' and pass them to glob.glob which gives you all files in the directory and of course image_list.append(pygame.image.load(artwork).convert()) can't work if you pass it for example a .py file.

The next mistake is the stimulus = pygame.image.load('image_list') line which doesn't work because you need to pass a complete file name or path to the load function. I think your stimulus variable should actually be the current_image.

Here's a complete example that also shows you how to implement a timer.

import glob
import random
import pygame


pygame.init()
clock = pygame.time.Clock()
window = pygame.display.set_mode((640, 480))

file_types = '*.tif',  # The comma turns it into a tuple.
# file_types = ['*.tif']  # Or use a list.
artfile_names = []
for file_type in file_types:
    artfile_names.extend(glob.glob(file_type))

image_list = []
for artwork in artfile_names:
    image_list.append(pygame.image.load(artwork).convert())

random.shuffle(image_list)

index = 0
current_image = image_list[index]
previous_time = pygame.time.get_ticks()

done = False

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

    # Calcualte the passed time, then increment the index, use
    # modulo to keep it in the correct range and finally change
    # the image.
    current_time = pygame.time.get_ticks()
    if current_time - previous_time > 500: # milliseconds
        index += 1
        index %= len(image_list)
        current_image = image_list[index]
        previous_time = current_time

    # Draw everything.
    window.fill((30, 30, 30))  # Clear the screen.
    # Blit the current image.
    window.blit(current_image, (100, 100))

    pygame.display.update()
    clock.tick(30)
skrx
  • 19,980
  • 5
  • 34
  • 48