我在程序中按下D键时遇到了一些问题。我会展示然后解释。 (省略不相关的事情)
Main.py
while True:
process(Cursor,movie,music)
Effects.List.draw(screen)
pygame.display.flip()
Classes.py
class BaseClass(pygame.sprite.Sprite):
allsprites = pygame.sprite.Group()
def __init__(self,x,y,image_string):
pygame.sprite.Sprite.__init__(self)
BaseClass.allsprites.add(self)
self.image = pygame.image.load(image_string)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def destroy(self, ClassName):
ClassName.List.remove(self)
BaseClass.allsprites.remove(self)
del self
class Effects(BaseClass):
List = pygame.sprite.Group()
def __init__(self,x,y,image_string):
BaseClass.__init__(self,x,y,image_string)
Effects.List.add(self)
Process.py
def process(Cursor,movie,music):
for event in pygame.event.get():
Stage = True
Stage2 = False
keys = pygame.key.get_pressed()
if Stage:
if Cursor.rect.collidepoint(370,340): #Start
if keys[pygame.K_RETURN]:
Stage2 = True
if Stage2:
Stage = False
hitkeys = HitKeys(65,600,"Images/Hit Keys.png") #520
lane = KeyLane(50,0,"Images/4k lane.png")
movie.play()
pygame.mixer.music.play()
if keys[pygame.K_d]:
effect1 = Effects(55,0,"Images/Effect.png")
我没有编程经验,所以我实现我想要的方法非常迂回。正如您所看到的,我希望在按下D时显示Effect.png
,但仅在Stage2
期间显示。它没有发生。如果我将其缩小一部分,那将会有效,但这意味着它会在Stage
期间出现。不是我想要的。
如果您能帮助我确定为什么它不会出现在Stage2
中,我们将不胜感激。
答案 0 :(得分:0)
在for event in pygame.event.get():
中设置
Stage = True
Stage2 = False
因此,每次运行process()
时,您都会转到Stage
(Stage = True
)
你必须设置
Stage = True
Stage2 = False
在游戏开始时(while True:
之前)
BTW:您必须使用Stage
和Stage2
process(Cursor,movie,music, Stage, Stage2)
也许最好使用一个Stage
并指定阶段编号1
,2
BTW:有python规则如何命名函数和变量(PEP8) - 对变量名称_
使用小写(和stage
), stage1
,all_sprites
)并在类名中使用大写字母。事件Stackover
使用该规则,并且它使用浅蓝色作为类名。
修改强>
我制作了测试键盘的脚本。检查你得到了什么。
keys = pygame.key.get_pressed()
只有在屏幕/窗口存在时才有效。
import pygame
import pygame.locals
pygame.init()
screen = pygame.display.set_mode((800,600))
d_pressed = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_d:
d_pressed = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_d:
d_pressed = False
keys = pygame.key.get_pressed()
if d_pressed:
print '> d_pressed <'
if keys[pygame.K_RETURN]:
print '> K_RETURN <'
if keys[pygame.K_d]:
print '> K_d <'
编辑:以列表形式发送阶段 - 不是(分隔)值
stages = [True, False]
while True:
process(Cursor, movie, music, stages)
Effects.List.draw(screen)
pygame.display.flip()
-
while True
def process(Cursor,movie,music, stages):
for event in pygame.event.get():
keys = pygame.key.get_pressed()
if stages[0]:
if Cursor.rect.collidepoint(370,340): #Start
if keys[pygame.K_RETURN]:
stages[0] = False
stages[1] = True
if stage[1]:
hitkeys = HitKeys(65,600,"Images/Hit Keys.png") #520
lane = KeyLane(50,0,"Images/4k lane.png")
movie.play()
pygame.mixer.music.play()
if keys[pygame.K_d]:
effect1 = Effects(55,0,"Images/Effect.png")