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 ...
随机推荐
- 深入理解ASP.NET Core依赖注入
概述 ASP.NET Core可以说是处处皆注入,本文从基础角度理解一下原生DI容器,及介绍下怎么使用并且如何替换官方提供的默认依赖注入容器. 什么是依赖注入 百度百科中对 ...
- k8s系列--- dashboard认证及分级授权
http://blog.itpub.net/28916011/viewspace-2215214/ 因版本不一样,略有改动 Dashboard官方地址: https://github.com/kube ...
- Tomcat 修改日志输出配置 定期删除日志
tomcat的下的日志catalina.out 和 qc.log疯狂增长,以下是解决办法 我生产环境tomcat版本 Server version: Apache Tomcat/7.0.35 Serv ...
- Git用法(一)
一.通过本地管理推送到github(仓库本来有文件) 1.clone到本地git clone 我的GitHub地址(ssh地址)(ps:新增文件一定要切换到项目目录下再操作)2.提交到本地仓库(1)g ...
- StarUML之九、starUML的一些特殊属性的说明
UML的扩充性机制允许你在控制的方式下扩充UML语言. 这一类的机制包括:stereotype,标记值.约束. Stereotype扩充了UML的词汇表,允许你创建新的建筑块,这些建筑块从已有的继承而 ...
- 随着页面滚动,数字自动增大的jquery特效
首先为了截出gif图,我下载了一个小工具 GifCam: https://www.appinn.com/gifcam/ 随着页面滚动,数字自动增大的jquery特效 主要就是依赖这个脚本script. ...
- 学习了解CSS3发展方向和CSS样式与优先级
通过学习CSS3游戏介绍.CSS样式和优先级章节,了解到html5+css3+js不光可以实现动画,其次可以往这个游戏与建模方向发展,更多css3特效访问Gerard Ferrandez on Cod ...
- count(1)比count(*)效率高?
SELECT COUNT(*) FROM table_name是个再常见不过的统计需求了. 本文带你了解下Mysql的COUNT函数. 一.COUNT函数 关于COUNT函数,在MySQL官网中有详细 ...
- windows的启动、引导配置
Win+S -> msconfig
- CDQ分治笔记+例题
CDQ分治是一种离线分治算法,它基于时间顺序对操作序列进行分治. 看这样一个问题: 在一个三维坐标系中,有若干个点,每个点都有对应的坐标 \((X_i , Y_i , Z_i)\) ,我们要对于每个点 ...