pygame 运行心理学问卷
2024-09-24 01:12:49
import pygame
import sys
from pygame.locals import * # wait for keys to putdown
def waitForKeys(keysAllowed):
key = False # initialize the key
pygame.event.clear() # clear the event
while not key:
for i in pygame.event.get(): # for every event in the event list
if i.type == pygame.KEYDOWN or i.type == pygame.KEYUP: # if the event type is keydown
if i.key in keysAllowed : # if key is the special key
# 如果是 if i.key == K_0 or K_1 or K_2 ,那么按任意一个键都将退出,因为 or 的逻辑成了(if i.key == K_0) or K_1 这种形式
print(i.key, pygame.key.name(i.key))
key = True # break the while loop return 0 # reture the value # draw a two sentences
def displayOneQuestion(myString):
pygame.init() # initialize pygame
mywin = pygame.display.set_mode((800,600), DOUBLEBUF|HWSURFACE) # create the window
myFont = pygame.font.SysFont("华文宋体", 25,False, False) # create the font
myStringSurface = myFont.render(myString, True, (255,255,255), False) # create the string
myString1 = "1 有点 2 中等 3 很多"
myStringSurface1 = myFont.render(myString1, True, (255,255,255), None)
mywin.blit(myStringSurface, (1,1), None, 0) # draw the font surface to another surface
mywin.blit(myStringSurface1, (1,100), None, 0) # draw another surface
pygame.display.flip() # flip the window
return 0 # return 0 myString = ["你好啊","我不好","吃了吗","枯井柘城","没有呢","那好吧"]
keysAllowed = [pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4, pygame.K_5]
for element in myString:
displayOneQuestion(element)
waitForKeys(keysAllowed) # 注意点1 : 如果 pygame.event.clear() # clear the event 这一句放在 while :的开头,那么程序会非常慢,而且有的还会跳过,不知为什么。
# 注意点2: 如果是 if i.key == K_0 or K_1 or K_2 ,那么按任意一个键都将退出,因为 or 的逻辑成了(if i.key == K_0) or K_1 这种形式 ,而且if i.key == (K_0 or K_1)也是不行的。
这是一张问卷的问题的界面(当然问题是我瞎编的,并且也不是很美观,后期可以调整)
需要被试按键才能看下一题,按的键包括1,2,3,4,5
这是第二个问题的界面
这是完成5道题后的结果,我打印出了按键的ASC值,以及键的名称,可以看出名称为键盘上的数字按键。
总结:界面上的问题,问题的位置,答案的内容,答案的位置都可以设置,由于现在没有拿到问卷的内容,所以问卷内容是瞎写的。用pygame的主要优势是跳过了纸笔测验,而且可以将被试的反应(按键)存入数据库。
最近看到,做心理学问卷,用pyqt5,以及tkinter写GUI,要比我上面写的心理学问卷好的多。悲哀。
继续用的pygame , 但是对自己之前的工作,做了优化。
代码如下:
import pygame
import sys
from pygame.locals import * # wait for keys to putdown
def waitForKeys(keysAllowed,theQuestion):
key = False # initialize the key
pygame.event.clear() # clear the event
while not key:
for i in pygame.event.get(): # for every event in the event list
if i.type == pygame.KEYDOWN or i.type == pygame.KEYUP: # if the event type is keydown
if i.key in keysAllowed : # if key is the special key
# 如果是 if i.key == K_0 or K_1 or K_2 ,那么按任意一个键都将退出,因为 or 的逻辑成了(if i.key == K_0) or K_1 这种形式
result = "{0}_{1}_{2}_{3:,<30}{4}{5}".format(participantName,participantGender,participantAge,theQuestion,"按键是: ",pygame.key.name(i.key)) # format the string , the format not only can use print() function
print(result) # print the result
key = True # break the while loop return result # reture the value # draw a two sentences
def displayOneQuestion(myString):
pygame.init() # initialize pygame
mywin = pygame.display.set_mode((800,600), DOUBLEBUF|HWSURFACE) # create the window
myFont = pygame.font.SysFont("华文宋体", 25,False, False) # create the font
myStringSurface = myFont.render(myString, True, (255,255,255), False) # create the string
myString1 = "1 非常不符合 2 比较不符合 3 难以确定 4 比较符合 5 非常符合"
myStringSurface1 = myFont.render(myString1, True, (255,255,255), None) surfaceHeight = myStringSurface.get_height() # get the suface width of picture one
surfaceWidth = myStringSurface.get_width() # get the suface height of picture one
surfaceHeight1 = myStringSurface1.get_height() # get the suface width of picture two
surfaceWidth1 = myStringSurface1.get_width() # get the suface height of picture two
finalSurfaceHeight = 250 - (surfaceHeight/2) # compute the half height of the picture one
finalSurfaceWidth = 400 - (surfaceWidth/2) # compute the half width of the picture one
finalSurfaceHeight1 = 250 - surfaceHeight1/2 # compute the half height of the picture two
finalSurfaceWidth1 = 400 - surfaceWidth1/2 #compute the half width of the picture two mywin.blit(myStringSurface, (finalSurfaceWidth, 200), None, 0)
# mywin.blit(myStringSurface, (400 - finalSurfaceWidth, 250 - finalSurfaceHeight), None, 0) # draw the font surface to another surface and put the font to the middle of the window
mywin.blit(myStringSurface1, (finalSurfaceWidth1,310), None, 0) # draw another surface and put the font to the middle of the window
pygame.display.flip() # flip the window
return 0 # return 03 myString = ["第1题: 在社交场合,我总是显得不够自然()",
"第2题: 我有话就说,从来憋不住()",
"第3题: 在集体活动中,我总表现得很活跃()", ]
participantNumber = input("请输入您的编号:") # collect participant number
participantName = input("请输入您的名字:") # collect participant name
participantGender = input("请输入您的性别:") # collect participant gender
participantAge = input("请输入您的年龄:") # collect participant age
keysAllowed = [pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4, pygame.K_5] # set the allowed keys which participant can push
resultList = [] # create a empty list to collect element for writing file
for element in myString: # every element in the qestionaires
displayOneQuestion(element) # display the image
result = waitForKeys(keysAllowed, element) # the image will stay on the screen until participant push the keyboard
resultList.append(result + '\n') # add the key which participant push to the empty list # open a file
with open("C:/users/mike1/desktop/data/wechatCrawlData/personality/{0}_{1}.txt".format(participantNumber,participantName), "w", encoding = "utf-8") as f1:
f1.writelines(resultList) # write the list to the file # 注意点1 : 如果 pygame.event.clear() # clear the event 这一句放在 while :的开头,那么程序会非常慢,而且有的还会跳过,不知为什么。
# 注意点2: 如果是 if i.key == K_0 or K_1 or K_2 ,那么按任意一个键都将退出,因为 or 的逻辑成了(if i.key == K_0) or K_1 这种形式 ,而且if i.key == (K_0 or K_1)也是不行的。
# 注意点3 : format()函数不仅可以用于print()函数中,还可以用于文本的写入 with open()函数,比如本例中,object = format(), write(object)
上面的问卷题目只有三道题。
题目的呈现如下:
打印出结果:
写入文件:
2019.12.27
最新文章
- git学习(五):克隆和推送远程仓库
- Macbook被格式化之后
- 8. apache服务实验笔记
- Linux 休眠,挂起(待机),关机等几个命令的区别及如何实现;如何启用Ubuntu的休眠模式
- 初识DeepLearning4j
- DEV:GridControl 筛选复选框 Checked Dropdown更改数据源
- redmine问题
- “PEDIY CrackMe 2007” 下载地址
- contentWindow 和contentDocument区别 及iframe访问
- 证明Dijkstra中加入S的点已经最优
- PowerDesigner使用详解
- cf471A MUH and Sticks
- git上传代码到github
- SpringMVC加载.roperties文件属性值的方法?
- python 函数运算先于单目运算
- wxpython实现界面跳转
- sklearn learn preprocessing
- linux上部署engineercms、docker和onlyoffice实现文档协作
- Install and Configure Apache Kafka
- jmeter读取csv文件