0
Ich möchte einen zufälligen Schlüssel aus dem Wörterbuch zu blit verwenden, aber ich kann nicht herausfinden, was ich falsch mache.Wählen Sie einen zufälligen Schlüssel aus einem Wörterbuch, so dass es angezeigt werden kann
# 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()
Dies ist der Fehler, den ich bekommen: return f [int (self.random() * len (f))] # wirft Indexerror wenn seq KeyError leer: 1 Traceback (jüngste Aufforderung zuletzt):
Offenbar '' mydict'' an der Zeit, die Sie ein zufälliges Bild zur Auswahl versucht, leer war. Was du gepostet hast, ist offensichtlich kein vollständiges Programm, das Problem ist wahrscheinlich in einem Bereich, den du nicht gepostet hast. Beachten Sie, dass Sie '' screen.blit'' mit dem Wörterbuch * key * ("sp1" usw.) aufrufen, NICHT das Bild. Außerdem ist es ineffizient, alle Bilder jedes Mal neu zu laden. – jasonharper
Hey Jasper, danke für mein Problem. Ich habe den Beitrag mit meinem vollen Programm aktualisiert. Ich habe das Wörterbuch aus der Schleife genommen, danke für diesen Tipp:) Obwohl ich immer noch feststecke, weißt du, was ich tun muss, um ein zufälliges Bild von Mydict (oder einer anderen Art von Container?) –