所以基本上我正在制作一个测验游戏,用户必须“锁定”他们的答案。为此,用户必须按下某个键,同时按下另一个键。例如:在按下并放开空格键的同时按住“w”键。但是当我尝试实现此目的时,没有任何反应。以下是我到目前为止的情况:
if (event.type == pygame.KEYDOWN) and (event.key == pygame.K_w) and (event.type == pygame.KEYUP) and (event.key == pygame.K_SPACE):
print(1)
为什么这不起作用?
答案 0 :(得分:0)
一种方法是使用pygame.key.get_pressed()
。示例 -
keys = pygame.keys.get_pressed()
if keys[pygame.K_w] and keys[pygame.K_SPACE]:
print(1)
编辑:这就是我实现它的方式 -
#!/usr/bin/python3
import pygame
from pygame.locals import *
from sys import exit
#initializing variables
pygame.init()
screen=pygame.display.set_mode((640,480),0,24)
pygame.display.set_caption("Key Press Test")
f1=pygame.font.SysFont("comicsansms",24)
#main loop which displays the pressed keys on the screen
while True:
for i in pygame.event.get():
a=100
screen.fill((255,255,255))
if pygame.key.get_focused():
press=pygame.key.get_pressed()
# checking if they are pressed at the same time or not.
if press[pygame.K_w] and press[pygame.K_SPACE]:
print (1)
for i in xrange(0,len(press)):
if press[i]==1:
name=pygame.key.name(i)
text=f1.render(name,True,(0,0,0))
screen.blit(text,(100,a))
a=a+100
pygame.display.update()
答案 1 :(得分:0)
question_num = general_knowlege_questions[0]
keys = pygame.key.get_pressed()
if keys[pygame.K_w] and (General_knowledge[question_num][5] == "a"):
test = 1
if keys[pygame.K_d] and (General_knowledge[question_num][5] == "b"):
test = 1
if keys[pygame.K_s] and (General_knowledge[question_num][5] == "c"):
test = 1
if keys[pygame.K_a] and (General_knowledge[question_num][5] == "d"):
test = 1
所以基本上这是我的代码,以识别我的测验答案,但它最终没有认识到正确的答案。顺便说一下,General_knowledge变量是一个列表,问题编号是自解释,5是包含答案的索引。