2017-01-09 53 views
0

我想使用字典中的隨機密鑰來blit,但我無法弄清楚我做錯了什麼。從字典中選擇一個隨機密鑰,以便它可以顯示

# This is a skeleton code for small interactive programs using PyGame 
# interaction is handled in main() in a structured way. 
# "while true" has four main elements: 
# 1) declaring the STATE variable (and other useful variables) 
# 2) the event loop: 
#  - which only cycles if new events arrive 
#  - which contains the interactive transition conditionals 
# 3) a number of ATCs, handling the automatic transitions 
#  - are continuously checked to allow for timing conditions 
# 4) a number of drawing conditionals 


# Development Information 
# TODO: change ITC from E-S-E to E-E-S 

import pygame 
import sys 
from time 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(mydict['sp1'],(248,148)) 

     # 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) 

mydict = {'sp1': pygame.image.load('smartphone1.jpg'), 
      'sp2': pygame.image.load('smartphone2.jpg'), 
      'sp3': pygame.image.load('smartphone3.jpg'), 
      'tb1': pygame.image.load('tablet1.jpg'), 
      'tb2': pygame.image.load('tablet2.jpg'), 
      'tb3': pygame.image.load('tablet3.jpg')} 

# RUN 
main() 

這是錯誤我得到: 回報序列[INT(self.random()* LEN(SEQ))]#引發IndexError如果序列是空的 KeyError異常:1 回溯(最近通話最後):

+0

顯然,''mydict''在你試圖從中隨機選擇一個圖像的時間空。你發佈的內容顯然不是一個完整的程序,問題可能出現在你沒有發佈的區域。請注意,您正在使用字典*鍵*(「sp1」等)調用'screen.blit',而不是圖像。而且,每次通過循環重新載入所有圖像效率相當低下! – jasonharper

+0

嘿賈斯珀,謝謝你看我的問題。我用完整的程序更新了這篇文章。我確實把字典從循環中刪除了,謝謝你的提示:)儘管如此,我仍然陷入困境,你知道我需要做什麼來從mydict(或另一種容器? –

回答

0

解決它

# This is a skeleton code for small interactive programs using PyGame 
# interaction is handled in main() in a structured way. 
# "while true" has four main elements: 
# 1) declaring the STATE variable (and other useful variables) 
# 2) the event loop: 
#  - which only cycles if new events arrive 
#  - which contains the interactive transition conditionals 
# 3) a number of ATCs, handling the automatic transitions 
#  - are continuously checked to allow for timing conditions 
# 4) a number of drawing conditionals 


# Development Information 
# TODO: change ITC from E-S-E to E-E-S 

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() 
相關問題