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. pytorch之 activation funcion

    import torch import torch.nn.functional as F from torch.autograd import Variable import matplotlib.p ...

  2. ASP.NET Core 借助 Helm 部署应用至K8S

    前言 玩K8S也有一段时间了,借助云服务提供商的K8S控制台,已经可以很方便的快速部署应用至K8S.通过简单的点击,可以一次性帮忙创建K8S 对象:Deployment.Service.Ingress ...

  3. coat 彩色的cat

    # 和cat类似,但每一行一种颜色,方便查看 curl -o coat https://raw.githubusercontent.com/oldratlee/useful-scripts/maste ...

  4. 修改centos7容器的时间和宿主机时间一致

    一.问题 centos7系统容器时间与宿主机系统时间不一致,就进去查看一番,发现时区和宿主机上的时间不一致,下面就来解决一下 二.现象 1.查看centos宿主机的时间 输入如下命令查看 # date ...

  5. android应用开发错误:Your project contains error(s),please fix them before running your

    重新打开ECLIPSE运行android项目,或者一段时间为运行ECLIPSE,打开后,发现新建项目都有红叉,以前的项目重新编译也有这问题,上网搜索按下面操作解决了问题 工程上有红叉,不知道少了什么, ...

  6. Dijkstra算法2

    // 再来一手精髓的Dijkstra // 复杂度O( E*log(V) ) #include <cstdio> #include <iostream> #include &l ...

  7. MySQL数据库root密码忘记丢失重置方法

    对于DBA来说,丢失超管用户root的密码是致命的, 可以通过添加--ship-grant-tables参数来跳过权限表. 1.忘记root密码,进不去数据库: 这个时候需要强制停库,先查看MySQL ...

  8. 【01】HTML_day01_03-HTML常用标签

    typora-copy-images-to: media 第01阶段.前端基础.HTML常用标签 学习目标 理解: 相对路径三种形式 应用 排版标签 文本格式化标签 图像标签 链接 相对路径,绝对路径 ...

  9. mysql必知必会--MySQL简介

    什么是MySQL MySQL已经存在很久了,它在世界范围内得到了广泛的安装和使用. 为什么有那么多的公司和开发人员使用MySQL?以下列出其原因. 成本--MySQL是开放源代码的,一般可以免费使用( ...

  10. Bringing up interface eth0: Device eth0 does not seem to be presen

    在公司的电脑虚拟机上安装了centos 6.5 ,然后我把他克隆下来用在家里电脑的虚拟机上,打开后查看ip,发现只有回环地址lo,没有eth0, 于是重启网络 输入 service network r ...