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((配列)lenはself.random()*)] seqが空 KeyError例外の場合#はIndexErrorを返した:1 トレースバック(最新の呼び出し最後):
を解決し、 '' mydict''は、あなたがそれからランダムに画像を選択しようとした時に空でした。投稿した内容は明らかに完全なプログラムではありませんが、あなたが投稿していない地域に問題がある可能性があります。画像ではなく、辞書*キー*( "sp1"など)で '' screen.blit''を呼び出すことに注意してください。また、ループを繰り返すたびに、すべての画像をリロードするのはむしろ非効率的です! – jasonharper
ちょっとジャスパー、私の問題を見てくれてありがとう。私は私のフルプログラムで投稿を更新しました。私は辞書をループから外しました、そのヒントありがとうございました:)私はまだ固まっていますが、mydict(または別の種類のコンテナ)からランダムなイメージをblitするために必要なことを知っていますか? –