mardi 10 janvier 2017

How to show a random chosen image for a specific amount of time

The images keep on cycling, probably because of the while loop(?). How do I make it so that 1 random chosen picture stays for a while (5seconds)?

Do I have to put it in the for loop? I want it to happen automatically, so that after 5 seconds I can go to another state or show another image.

import pygame
import sys
import time
import random
from pygame.locals import *
from pygame.compat import unichr_, unicode_


# Colors
col_white = (250, 250, 250)
col_black = (0, 0, 0)
col_gray = (220, 220, 220)
col_red = (250, 0, 0)
col_green = (0, 200, 0)
col_blue = (0, 0, 250)
col_yellow = (250,250,0)
BACKGR_COL = col_white

SCREEN_SIZE = (700, 500)

# Preparing the PyGame window
pygame.init()
pygame.display.set_mode(SCREEN_SIZE) 
pygame.display.set_caption("Skeleton")
screen = pygame.display.get_surface()
screen.fill(BACKGR_COL)
font = pygame.font.Font(None, 80)
font_small = pygame.font.Font(None, 40) 


def main():
    # Variables
    STATE = "welcome" ## list, possible, states

    # screen refresh loop
    while True:
        # setting the background color
        pygame.display.get_surface().fill(BACKGR_COL)        

        # event loop (is only entered when an event occured)
        for event in pygame.event.get():
            # Interactive transition conditionals (ITC)
            if STATE == "welcome":
                if event.type == KEYDOWN and event.key == K_SPACE:
                    STATE = "prepare_next_trial"
                    print(STATE)
            # always include transition for quit events
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        # automatic transition conditionals (ATC)
        if STATE == "prepare_next_trial":
            screen.blit(random.choice(picture_list),(248,102))
        # Drawing conditionals
        if STATE == "welcome":
            draw_welcome()
        pygame.display.update()
        # Picture dictionary


    # end screen refresh loop

# define draw functions and other functions
def draw_welcome():
    text_surface = font.render("STROOP Experiment", True, col_black, BACKGR_COL)
    text_rectangle = text_surface.get_rect()
    text_rectangle.center = (SCREEN_SIZE[0]/2.0,150)
    screen.blit(text_surface, text_rectangle)
    text_surface = font_small.render("Press Spacebar to continue", True, col_black, BACKGR_COL)
    text_rectangle = text_surface.get_rect()
    text_rectangle.center = (SCREEN_SIZE[0]/2.0,300)
    screen.blit(text_surface, text_rectangle)

# picture list
picture_list = []    
SP1 = pygame.image.load("smartphone1.jpg").convert()
picture_list.append(SP1)
SP2 = pygame.image.load("smartphone2.jpg").convert()
picture_list.append(SP2)
SP3 = pygame.image.load("smartphone3.jpg").convert()
picture_list.append(SP3)
SP4 = pygame.image.load("tablet1.jpg").convert()
picture_list.append(SP4)
SP5 = pygame.image.load("tablet2.jpg").convert()
picture_list.append(SP5)
SP6 = pygame.image.load("tablet3.jpg").convert()
picture_list.append(SP6)
SP7 = pygame.image.load("laptop1.jpg").convert()
picture_list.append(SP7)
SP8 = pygame.image.load("laptop2.jpg").convert()
picture_list.append(SP8)
SP9 = pygame.image.load("laptop3.jpg").convert()
picture_list.append(SP9)
# RUN
main()




Aucun commentaire:

Enregistrer un commentaire