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 运行心理学问卷的更多相关文章

  1. 趣味python编程之经典俄罗斯方块

    国庆期间闲不住,用python把经典俄罗斯方块实现了一遍,找到了些儿时的乐趣.因此突发奇想,打算用python写点经典又确实有趣的小程序形成系列.正统编程之余也给自己找点儿乐趣,换个角度写程序. 原计 ...

  2. python学习笔记05:贪吃蛇游戏代码

    贪吃蛇游戏截图: 首先安装pygame,可以使用pip安装pygame: pip install pygame 运行以下代码即可: #!/usr/bin/env python import pygam ...

  3. npm学习(五)之使用package.json

    使用package.json 管理本地安装的npm包的最佳方法是创建一个package.json文件. 一个packagejson文件: 列出项目所依赖的包. 允许使用语义版本控制规则指定项目可以使用 ...

  4. 20184302 2019-2020-2 《Python程序设计》实验四报告

    20184302 2019-2020-2 <Python程序设计>实验四报告 课程:<Python程序设计> 班级: 1843 姓名: 李新锐 学号:184302 实验教师:王 ...

  5. 少儿编程:python趣味编程第一课

    本文仅针对8-16岁的青少年,所以流程是按如何去教好中小学生走的,并不适合成人找工作学习,因为进度也是按照青少年走的 大家好,我是C大叔,从事少儿编程行业三年有余(2016年从事少儿编程行业,少儿编程 ...

  6. python pygame 安装和运行的一些问题

    1.python安装过程中可以选择自动配置环境变量,可以避免手动配置,但是缺点是如果环境变量有问题,就得自己重新学习配置环境变量. 2.我自己想用python从游戏方面入手,所以在安装pygame过程 ...

  7. 学习python及Pygame的安装及运行

    Python: 注意勾上Add Python 2.7 to PATH,然后点“Install Now”即可完成安装. 或手动修改环境变量,win7:右击我的电脑->属性->高级->环 ...

  8. linux的七大运行级别及级别修改

    运行级别     级别说明 0           所有进程将被终止,机器将有序的停止,关机时系统处于这个运行级别 1           单用户模式,用于系统维护,只有少数进程运行,同时所有服务也不 ...

  9. pygame开发PC端微信打飞机游戏

    pygame开发PC端微信打飞机游戏 一.项目简介 1. 介绍 本项目类似曾经火爆的微信打飞机游戏.游戏将使用Python语言开发,主要用到pygame的API.游戏最终将会以python源文件gam ...

随机推荐

  1. 使用Redis需要注意的几点

    Redis作为缓存中间件,被广泛应用在各类系统,用来提升系统性能和吞吐,下面总结几点开发人员在使用Redis时需要考虑的几个关键点: 一. key的设计 1. key命名规范:为了避免不必要的麻烦,我 ...

  2. Go语言实现:【剑指offer】删除链表中重复的结点

    该题目来源于牛客网<剑指offer>专题. 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有重复出现的数字. 示例 1: 输入: 1->2->3->3- ...

  3. Spring学习笔记:自动创建Proxy

    为什么需要自动创建Proxy 手动为所有需要代理的类用ProxyFactoryBean创建代理Proxy需要大量的配置. 这样如果需要代理的类很多,配置就很繁琐,而且也不便于xml配置的维护. 因此S ...

  4. ssh_key认证

    ssh认证流程步骤: 1.主机host_key认证 2.身份验证 3.身份验证通过 原理及更多知识点,请查看好友博客 http://www.cnblogs.com/f-ck-need-u/p/7129 ...

  5. drf token刷新配置、认证组件(使用)、权限组件(使用)、频率组件(使用)、异常组件(使用)

    目录 一.特殊路由映射的请求 二.token刷新机制配置(了解) 三.认证组件项目使用:多方式登录 1.urls.py 路由 2.views.py 视图 3.serializers.py 序列化 4. ...

  6. Zookeeper分布式系统协同器概念快速学习

    原文格式可以访问:https://www.rockysky.tech 分布式系统的基本操作 主节点选举:在绝大多数分布式系统中,都需要进行主节点选举.主节点负责管理协调其它节点或者同步集群中其它节点的 ...

  7. Java 中常见排序算法

    经典的排序算法总结 冒泡排序算法 算法描述: 比较相邻的元素:如果第一个比第二个大,就交换它们两个: 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数: 针 ...

  8. java 开发社区蔬菜、食品交易平台系统 源码

    开发环境:    Windows操作系统开发工具: MyEclipse+Jdk+Tomcat+Mysql数据库 运行效果图 源码及原文链接:https://javadao.xyz/forum.php? ...

  9. 实战kudu集成impala

    推荐阅读: 论主数据的重要性(正确理解元数据.数据元) CDC+ETL实现数据集成方案 Java实现impala操作kudu 实战kudu集成impala impala基本介绍 ​        im ...

  10. (转)elasticsearch collapse 折叠字段应用

    转自:https://elasticsearch.cn/article/132 在 Elasticsearch 5.x 有一个字段折叠(Field Collapsing,#22337)的功能非常有意思 ...