有我的代碼:python 3.2:pygame.KEYDOWN只能工作1次?
import pygame, sys, os
from pygame.locals import *
from time import sleep
import os
path = os.getcwd()
os.chdir(path + '\Assets')
pygame.init()
FPS = 30
fpsClock = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((800, 500))
pygame.display.set_caption('fightR')
WHITE = (225, 225, 225)
SILVER = (192, 192, 192)
BGCOLOR = SILVER
fkx = 70
fky = 252
SPEED = 5
jump = False
#loading images
fighter_kungfu_1 = pygame.image.load('fighter_kungfu_1.png')
fighter_kungfu_jump = pygame.image.load('fighter_kungfu_jump.png')
def CheckForQuit() :
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#MGL
while True:
DISPLAYSURF.fill(BGCOLOR)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_a:
if (fkx > 2):
global fkx
fkx = fkx - SPEED
elif event.type == pygame.KEYDOWN and event.key == pygame.K_d:
if (fkx < 300):
global fkx
fkx = fkx + SPEED
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
jump = True
if (jump):
DISPLAYSURF.blit(fighter_kungfu_jump, (fkx - 25, fky - 10))
#sleep(0.5)
else:
DISPLAYSURF.blit(fighter_kungfu_1, (fkx, fky))
jump = False
CheckForQuit()
pygame.display.update()
fpsClock.tick(FPS)
pygame.display.flip()
到圖像的鏈接,如果你想:
https://drive.google.com/open?id=0B4Aq1fJx1P4AQkE1ZTZLR1ZCRW8
https://drive.google.com/open?id=0B4Aq1fJx1P4AeXB5UjYtZ29XU1k
(把它們放在一個 '資產' 文件夾)
我的問題與遊戲:當我按下'a'或'd'按鈕並按住它時,我的角色只移動一次而不是連續移動。
我試圖把,而不是「如果」和「elif的」一「而」循環:
while event.type == pygame.KEYDOWN and event.key == pygame.K_a:
if (fkx > 2):
global fkx
fkx = fkx - SPEED
while event.type == pygame.KEYDOWN and event.key == pygame.K_d:
if (fkx < 300):
global fkx
fkx = fkx + SPEED
,但它是不工作的。
有人可以幫我嗎?
好主意!它正在工作,謝謝! –