pygame 运行心理学问卷
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
pygame 运行心理学问卷的更多相关文章
- 趣味python编程之经典俄罗斯方块
国庆期间闲不住,用python把经典俄罗斯方块实现了一遍,找到了些儿时的乐趣.因此突发奇想,打算用python写点经典又确实有趣的小程序形成系列.正统编程之余也给自己找点儿乐趣,换个角度写程序. 原计 ...
- python学习笔记05:贪吃蛇游戏代码
贪吃蛇游戏截图: 首先安装pygame,可以使用pip安装pygame: pip install pygame 运行以下代码即可: #!/usr/bin/env python import pygam ...
- npm学习(五)之使用package.json
使用package.json 管理本地安装的npm包的最佳方法是创建一个package.json文件. 一个packagejson文件: 列出项目所依赖的包. 允许使用语义版本控制规则指定项目可以使用 ...
- 20184302 2019-2020-2 《Python程序设计》实验四报告
20184302 2019-2020-2 <Python程序设计>实验四报告 课程:<Python程序设计> 班级: 1843 姓名: 李新锐 学号:184302 实验教师:王 ...
- 少儿编程:python趣味编程第一课
本文仅针对8-16岁的青少年,所以流程是按如何去教好中小学生走的,并不适合成人找工作学习,因为进度也是按照青少年走的 大家好,我是C大叔,从事少儿编程行业三年有余(2016年从事少儿编程行业,少儿编程 ...
- python pygame 安装和运行的一些问题
1.python安装过程中可以选择自动配置环境变量,可以避免手动配置,但是缺点是如果环境变量有问题,就得自己重新学习配置环境变量. 2.我自己想用python从游戏方面入手,所以在安装pygame过程 ...
- 学习python及Pygame的安装及运行
Python: 注意勾上Add Python 2.7 to PATH,然后点“Install Now”即可完成安装. 或手动修改环境变量,win7:右击我的电脑->属性->高级->环 ...
- linux的七大运行级别及级别修改
运行级别 级别说明 0 所有进程将被终止,机器将有序的停止,关机时系统处于这个运行级别 1 单用户模式,用于系统维护,只有少数进程运行,同时所有服务也不 ...
- pygame开发PC端微信打飞机游戏
pygame开发PC端微信打飞机游戏 一.项目简介 1. 介绍 本项目类似曾经火爆的微信打飞机游戏.游戏将使用Python语言开发,主要用到pygame的API.游戏最终将会以python源文件gam ...
随机推荐
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- php curl 检测网页是否被百度收录
<?php /* * 检测百度是否收录网页 curl模式 * @ param string $url传入的url* return int (1 收录 0 不收录) */ function che ...
- k8s调度器kube-scheduler
kube-scheduler简介 调度是容器编排的重要环节,需要经过严格的监控和控制,现实生产通常对调度有各类限制,譬如某些服务必须在业务独享的机器上运行,或者从灾备的角度考虑尽量把服务调度到不同机器 ...
- Mysql 字符问题
先看一下mysql支持的字符范围 *数值类型:1.整形: 类型 大小 范围 ...
- [Redis-CentOS7]Redis哈希操作(五)
哈希相当于下面Python代码 { "person":{ "name": "peigy", "age": "1 ...
- React之props、state和render函数的关系
1.当组件中的state或者props发生改变的的时候,render函数就会被重新执行 2.当父组件的render函数被运行时,它的子组件的render都将被重新运行一次 3.子组件作为父组件里的一个 ...
- GitHub当作私密的版本控制系统远端版本库私有化
目的 我打算把所有服务器的配置文件用git管理起来,这样可以记录配置变更状况. 但是有一个问题是,如何多人协作?服务器配置信息非常敏感,如果这个版本库泄漏,整个公司的服务器架构就彻底泄漏了. 这个版本 ...
- Mac下升级ruby至最新版本
Mac自身的ruby 版本 2.x,通过ruby -v可以查看版本号. 为更新到ruby的最新版本,可通过以下命令解决: brew update brew install ruby 执行完命令后,ru ...
- Spark应用开发调优要点总结
调试Spark应用性能的时候,首先应该理解spark是如何工作以及你的spark应用需要何种类型的资源.比如说,机器学习相关的spark应用更依赖cpu计算能力,ETL应用更依赖I/O能力,以此进行有 ...
- workerman离线推送方案
方案一:目前网上比较流行的 方案二:参考腾迅IM