keyPress och keyHold vägrar uppdateras. Varför?
Jag vet att det finns inbyggda funktioner för att hämta keypresses i pygame men det skiter jag i.
Kod:
#! /usr/bin/env python
import pygame, sys
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
keyHold = {'q':0, 'w':0, 'e':0, 'r':0,
't':0, 'y':0, 'u':0, 'i':0,
'o':0, 'p':0, 'a':0, 's':0,
'd':0, 'f':0, 'g':0, 'h':0,
'j':0, 'k':0, 'l':0, 'z':0,
'x':0, 'c':0, 'v':0, 'b':0,
'n':0, 'm':0, }
keyPress = {'q':0, 'w':0, 'e':0, 'r':0,
't':0, 'y':0, 'u':0, 'i':0,
'o':0, 'p':0, 'a':0, 's':0,
'd':0, 'f':0, 'g':0, 'h':0,
'j':0, 'k':0, 'l':0, 'z':0,
'x':0, 'c':0, 'v':0, 'b':0,
'n':0, 'm':0, }
def keyboardinput():
global keyPress
global keyHold
keyPress = { 'q':0, 'w':0, 'e':0, 'r':0,
't':0, 'y':0, 'u':0, 'i':0,
'o':0, 'p':0, 'a':0, 's':0,
'd':0, 'f':0, 'g':0, 'h':0,
'j':0, 'k':0, 'l':0, 'z':0,
'x':0, 'c':0, 'v':0, 'b':0,
'n':0, 'm':0, }
keylist = [ 'q', 'w', 'e', 'r',
't', 'y', 'u', 'i',
'o', 'p', 'a', 's',
'd', 'f', 'g', 'h',
'j', 'k', 'l', 'z',
'x', 'c', 'v', 'b',
'n', 'm', ]
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
sys.exit()
else:
for key in keylist:
if event.type == KEYDOWN and event.key == ('K_' + key):
keyPress[key] = 1
keyHold[key] = 1
elif event.type == KEYUP and event.key == ('K_' + key):
keyHold[key] = 0
print keyPress
print keyHold
print event
# Main loop
while 1:
clock.tick(60)
keyboardinput()