python心理学实验程序(pygame)

编程入门 行业动态 更新时间:2024-10-08 06:25:44

python<a href=https://www.elefans.com/category/jswz/34/1766315.html style=心理学实验程序(pygame)"/>

python心理学实验程序(pygame)

python心理学实验程序

使用pygame做了个小实验。
任务主要包括:选词任务;呈现图片和字母,对字母进行判断;选数任务。

选词任务:10个词语呈现顺序随机,选择其中3个。
字母判断任务:字母Q或O出现在图片的左边或右边,判断字母为哪个。20张图片字母q/o字母位置左右 = 80种条件。80种随机,同时不能连续呈现一样的图片。
选数任务:从1-7选择一个数字。

程序中涉及实验材料的部分被省略。

本实验的psychopy版本在我的另一篇文章中,
原文链接:

pygame写的程序可以很方便地打包为exe程序,
而psychopy写的程序打包时总会遇到各种问题。

(欢迎读者与我交流)

# import libraries
from base64 import b64decode
from random import shuffle
import pygame
from pygame.locals import *# trial list
group_list = ['11', '12', '22', '21']wordtrials = [……] # 略
letter_trials = [['L', 'Q'],['L', 'O'],['R', 'Q'],['R', 'O']]
pictrials_prac = [[ele7, 'ele', '7'],[ele27, 'ele', '27'],[sex3, 'sex', '3'],[sex15, 'sex', '15']]
pictrials = [[ele2, 'ele', '2'],……] # 略# wait for a key press
def wait4key(duration, keys_allowed):""" wait for a key response.duration: wait for how longkeys_allowed: which keys are allowed,keys_allowed should be list, such as [K_z, K_SLASH]. """got_key = Falsetime_out = Falset_start = pygame.time.get_ticks()pygame.event.clear()while not (got_key or time_out):# check for time outt_now = pygame.time.get_ticks()if t_now - t_start >= duration:time_out = True# check for key responsesfor ev in pygame.event.get():if ev.type == KEYDOWN:if ev.key in keys_allowed:resp_time = t_now - t_startresp_key = pygame.key.name(ev.key)got_key = Trueif got_key:return [resp_key, resp_time]else:return [None, None]# run a single trial
def run_pictrial(pars, letter_pars, data_file):""" pars should be list that specifies thelocation and color of the stimulus, as well asthe correct key response, such as ['R', 'B', '/']"""file, catog, num = parslll, letter = letter_parsif lll == 'R': loo = rightif lll == 'L': loo = leftimg_data = b64decode(file) # 图片的编码此处略with open('001.jpg', 'wb') as f:f.write(img_data)# present the fixationwin.fill(black)text_msg('+', (scr_width/2, scr_height/2), 48, False)pygame.display.update()pygame.time.wait(1000)# picturepic = pygame.image.load('001.jpg')win.fill(black)win.blit(pic, (scr_width/2 - pic.get_width() / 2, scr_height/2 - pic.get_height() / 2))pygame.display.update()pygame.time.wait(50)# picture & lettertext_msg(letter, loo, 48, False)pygame.display.update()pygame.time.wait(50)# present picturewin.fill(black)win.blit(pic, (scr_width/2 - pic.get_width() / 2, scr_height/2 - pic.get_height() / 2))pygame.display.update()tar_resp = wait4key(1200, [K_q, K_o, K_z, K_0])# K_SLASHwin.fill(black)pygame.display.update()pygame.time.wait(500)# write data to fileif tar_resp[0] == None:acc = '0'rrr = 0else:rrr = tar_resp[0]#keyletter = list(tar_resp[0][0])if tar_resp[0] == '0':ans = 'O'if tar_resp[0] == 'q':ans = 'Q'if tar_resp[0] == 'o':ans = 'O'if tar_resp[0] == 'z':ans = 'z'if ans == letter:acc = '1'else:acc = '0'trial_data = list([catog]) + list([num]) + letter_pars + tar_resp + list(acc)trial_data = map(str, trial_data)trial_data = ','.join(trial_data) + '\n'data_file.write(trial_data)return rrrdef text_msg_left(msg, loc,):""" display a short text message"""txt_font = pygame.font.SysFont('simsunnsimsun', 30)txt_msg = txt_font.render(msg, True, white)win.blit(txt_msg, (loc[0], loc[1]))def text_msg(msg, loc, size, font_bold):""" display a short text message"""txt_font = pygame.font.SysFont('simsunnsimsun', size)txt_font.set_bold(font_bold)txt_msg = txt_font.render(msg, True, white)win.blit(txt_msg, (loc[0] - txt_msg.get_width() / 2, loc[1] - txt_msg.get_height() / 2))def get_subj_info():subj_group = input('Subject group: ')while subj_group not in group_list:subj_group = input('组(刚刚的输入不符合要求,请重新输入): ')subj_id = input('Subject ID: ')subj_age = input('Subject Age: ')return [subj_group, subj_id, subj_age]# real experiment starts here
# initialize pygame
pygame.init()
subj = get_subj_info()
# create a window
scr_width = pygame.display.list_modes()[0][0]
scr_height = pygame.display.list_modes()[0][1]
center_w = scr_width / 2
center_h = scr_height / 2
# some constants
left = (scr_width/2 - 230, scr_height/2)
right = (scr_width/2 + 230, scr_height/2)
red = (255, 0, 0)
black = (0, 0, 0)
white = (255, 255, 255)win = pygame.display.set_mode((scr_width, scr_height), FULLSCREEN)
pygame.mouse.set_visible(False)
# open a data file
d_file = open('_'.join(subj[:]) + '.csv', 'w')# show the instructions
text_msg(u'您好,欢迎参加本次实验!', (scr_width/2, scr_height/2-70), 24, False)
text_msg(u'接下来将呈现一段故事描述,该故事设定在实验中还需要再次回忆。', (scr_width/2, scr_height/2-20), 30, False)
text_msg(u'请仔细阅读,并想象此经历正在你身上发生。', (scr_width/2, scr_height/2+20), 30, True)
text_msg(u'准备好后,请按“空格键”开始。', (scr_width/2, scr_height/2+70), 24, False)
pygame.display.flip()
wait4key(999999, [K_SPACE])
win.fill(black)
if subj[0] == '11' or subj[0] == '12':text_msg_left(u'故事内容', (center_w-330, scr_height / 2 - 150))
else:text_msg_left(u'故事内容', (center_w-330, scr_height / 2 - 150))text_msg(u'请仔细阅读,想象此情景。', (center_w, scr_height/2+100), 24, False)
text_msg(u'准备好后,按“空格键”进行下一步。', (center_w, scr_height/2+130), 24, False)
pygame.display.flip()
wait4key(999999, [K_SPACE])
shuffle(wordtrials)
win.fill(black)
text_msg(u'请从以下词语中挑选出3个你认为……', (center_w, center_h - 110), 24, False)
text_msg(u'(在键盘上按下对应数字选择,选满3个后按“空格键”进行下一步)', (center_w, center_h - 80), 24, False)
strr = '\t'
text_msg(strr.join(wordtrials[0:5]),  (center_w, center_h - 20), 30, False)
text_msg(u'0\t\t1\t\t\t2\t\t\t3\t\t4',  (center_w, center_h + 10), 30, False)
text_msg(strr.join(wordtrials[5:]),  (center_w,center_h + 50), 30, False)
text_msg(u'5\t\t6\t\t\t7\t\t\t8\t\t9',  (center_w, center_h + 80), 30, False)
pygame.display.flip()
num1 = wait4key(999999, [K_0, K_1, K_2, K_3, K_4, K_5, K_6, K_7, K_8, K_9,K_KP0, K_KP1, K_KP2, K_KP3, K_KP4, K_KP5, K_KP6, K_KP7, K_KP8, K_KP9])
num2 = wait4key(999999, [K_0, K_1, K_2, K_3, K_4, K_5, K_6, K_7, K_8, K_9,K_KP0, K_KP1, K_KP2, K_KP3, K_KP4, K_KP5, K_KP6, K_KP7, K_KP8, K_KP9])
num3 = wait4key(999999, [K_0, K_1, K_2, K_3, K_4, K_5, K_6, K_7, K_8, K_9,K_KP0, K_KP1, K_KP2, K_KP3, K_KP4, K_KP5, K_KP6, K_KP7, K_KP8, K_KP9])
wait4key(999999, [K_SPACE])
# write data to file
if num1[0] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:n1 = num1[0]
else:n1 = num1[0][1]
if num2[0] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:n2 = num2[0]
else:n2 = num2[0][1]
if num3[0] in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:n3 = num3[0]
else:n3 = num3[0][1]          
trial_data = subj + list([wordtrials[int(n1)]]) + list([wordtrials[int(n2)]]) + list([wordtrials[int(n3)]])
trial_data = map(str, trial_data)
trial_data = ','.join(trial_data) + '\n'
d_file.write(trial_data)
# exp 2
# exp 2 practice
win.fill(black)
text_msg(u'屏幕中央首先会出现一个“+”注视点,提醒你开始实验。', (center_w, center_h - 60), 24, False)
text_msg(u'接下来你将看到一张图片,在图片的左侧或右侧会出现一个字母。', (center_w, center_h - 30), 24, False)
text_msg(u'请判断该字母是O还是Q,是O则按“O”键,是Q则按“Q键”。', (center_w, center_h), 24, True)
text_msg(u'请集中注意,又快又准地作出判断。', (center_w, center_h + 40), 24, False)
text_msg(u'准备好后,按“空格键”开始练习。', (center_w, center_h + 70), 24, False)
pygame.display.flip()
wait4key(999999, [K_SPACE])
aa = list(range(4))
shuffle(aa)
for t in aa:ll = letter_trials[t]pp = pictrials_prac[t]rr = run_pictrial(pp, ll, d_file)if rr == 'z':break
win.fill(black)
text_msg(u'练习完毕,准备好后按“空格键”进入正式实验。', (center_w, center_h), 24, False)
pygame.display.flip()
wait4key(999999, [K_SPACE])
# exp 2 real
aa = list(range(80))
shuffle(aa)
trial_data = 'experi2' + '\n'
d_file.write(trial_data)
for aaa in range(79):if aa[aaa]//4 == aa[aaa+1]//4:zz = 0while zz == 0:bb = aa[aaa+1:]shuffle(bb)aa = aa[0:aaa+1]aa.extend(bb)if aa[aaa]//4 != aa[aaa+1]//4:zz = 1
for t in aa:ll = letter_trials[t%4]pp = pictrials[t//4]rr = run_pictrial(pp, ll, d_file)if rr == 'z':break
# exp 3
text_msg(u'请稍作休息,回忆实验开始时阅读过的场景。', (center_w, center_h - 60), 24, False)
text_msg(u'接下来将出现一段补充描述,你有4秒时间阅读。', (center_w, center_h - 20), 24, False)
text_msg(u'材料消失后,请并根据指示进行评定。', (center_w, center_h + 10), 24, False)
text_msg(u'准备好后,按“空格键”进行下一步。', (center_w, center_h + 50), 24, False)
pygame.display.flip()
wait4key(999999, [K_SPACE])
win.fill(black)
text_msg(u'情境中你……', (center_w - 120, center_h), 24, False)
if subj[0] == '11' or subj[0] == '21':text_msg(u'描述语', (center_w+80, center_h), 24, True)
else:text_msg(u'描述语', (center_w+80, center_h), 24, True)
text_msg(u'……', (center_w + 190, center_h), 24, False)
pygame.display.flip()
pygame.time.wait(4000)
win.fill(black)
text_msg(u'……符合你心目中理想形象的程度为?', (center_w, center_h - 70), 24, True)
text_msg(u'1-7代表“非常不符合”到“非常符合”。', (center_w, center_h - 40), 24, False)
text_msg(u'1     2     3     4     5     6     7', (center_w, center_h + 10), 24, False)
text_msg(u'请在键盘上按下对应数字选择,再按“空格键”进行下一步。', (center_w, center_h+60), 24, False)
pygame.display.flip()
respo1 = wait4key(999999, [K_1, K_2, K_3, K_4, K_5, K_6, K_7, K_KP1, K_KP2, K_KP3, K_KP4, K_KP5, K_KP6, K_KP7])
wait4key(999999, [K_SPACE])
win.fill(black)
if respo1[0] in ['1', '2', '3', '4', '5', '6', '7']:re = respo1[0]
else:re = respo1[0][1]
trial_data = map(str, list(re))
trial_data = ','.join(trial_data) + '\n'
d_file.write(trial_data)
text_msg(u'情境中你……', (center_w - 120, center_h), 24, False)
if subj[0] == '11' or subj[0] == '21':text_msg(u'描述语', (center_w + 80, center_h), 24, True)
else:text_msg(u'描述语', (center_w+80, center_h), 24, True)
text_msg(u'……', (center_w + 190, center_h), 24, False)
text_msg(u'\n……', (center_w - 230, center_h + 40), 24, False)
text_msg(u'……', (center_w + 155, center_h + 40), 24, True)
pygame.display.flip()
pygame.time.wait(4000)
win.fill(black)
text_msg(u'\n……符合你心目中理想形象的程度为?', (center_w, center_h - 80), 24, True)
text_msg(u'\n1-7代表“非常不符合”到“非常符合”。', (center_w, center_h - 40), 24, False)
text_msg(u'\n1     2     3     4     5     6     7', (center_w, center_h + 10), 24, False)
text_msg(u'\n请在键盘上按下对应数字选择,再按“空格键”进行下一步。', (center_w, center_h+60), 24, False)
pygame.display.flip()
respo2 = wait4key(999999, [K_1, K_2, K_3, K_4, K_5, K_6, K_7, K_KP1, K_KP2, K_KP3, K_KP4, K_KP5, K_KP6, K_KP7])
wait4key(999999, [K_SPACE])
if respo2[0] in ['1', '2', '3', '4', '5', '6', '7']:re = respo2[0]
else:re = respo2[0][1]
trial_data = map(str, list(re))
trial_data = ','.join(trial_data) + '\n'
d_file.write(trial_data)
win.fill(black)
text_msg(u'实验结束!感谢参与!请按空格键退出。', (center_w, center_h), 24, False)
pygame.display.flip()
wait4key(999999, [K_SPACE])
# close data file
d_file.close()
# exit
pygame.quit()                                         

更多推荐

python心理学实验程序(pygame)

本文发布于:2024-02-17 18:13:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1694990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:心理学   程序   python   pygame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!