我想使用字典中的随机键进行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()
这是我得到的错误: return seq [int(self.random()* len(seq))]#如果seq为空,则引发IndexError KeyError:1 回溯(最近一次调用最后一次):
答案 0 :(得分: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()