Итак, я делаю игру в Pygame, и мне нужно меню, позволяющее игроку выбирать между различными типами игр. Я попытался сделать так, чтобы при указании инструкций игрок мог использовать клавиатуру для выбора типа; однако, игра появляется, когда я это делаю. Инструкции будут оставлены на экране (иногда выполняется рисование спрайта в плоскости). Я добавил строку кода, чтобы напечатать что-то в оболочке, когда было нажато 'e'
, но ничего не печатается.Проблемы с игрой Pygame
def instructions(score):
pygame.display.set_caption("Mail Pilot!")
plane = Plane()
ocean = Ocean()
allSprites = pygame.sprite.Group(ocean, plane)
insFont = pygame.font.SysFont(None, 50)
insLabels = []
instructions = (
"Mail Pilot. Last score: %d" % score ,
"Instructions: You are a mail pilot,",
"delivering mail to the islands.",
"",
"Fly over an island to drop the mail,",
"but be careful not to fly too close",
"to the clouds. Press 'e' for",
"endless",
"",
"good luck!",
"",
"click to start, escape to quit..."
)
for line in instructions:
tempLabel = insFont.render(line, 1, (255, 255, 0))
insLabels.append(tempLabel)
keepGoing = True
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
while keepGoing:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
donePlaying = True
pygame.display.quit()
pygame.quit()
sys.exit()
## if event.type == pygame.MOUSEBUTTONDOWN:
## keepGoing = False
## donePlaying = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_e:
gameType = "endless"
keepGoing = False
donePlaying = False
elif event.key == pygame.K_ESCAPE:
keepGoing = False
pygame.display.quit()
pygame.quit()
sys.exit()
donePlaying = True
allSprites.update()
allSprites.draw(screen)
for i in range(len(insLabels)):
screen.blit(insLabels[i], (50, 30*i))
pygame.display.flip()
plane.sndEngine.stop()
pygame.mouse.set_visible(True)
return donePlaying
def main():
gameType = ""
donePlaying = False
score = 0
while not donePlaying:
donePlaying = instructions(score)
if not donePlaying:
if gameType == "endless":
score = gameEndless()
if __name__ == "__main__":
main()
Прокомментированная часть - это оригинальный код, который начнет игру.
Вы должны очистить экран в каждом цикле - т.е. 'screen.fill (BLACK)' или 'blit (background)'. – furas
у вас есть две локальные переменные с именем 'gameType' - сначала в' main() ', второй в' instructions() '. Вы должны использовать 'return donePlaying, gameType', чтобы вернуть это значение из' commands() 'в' main() ' – furas
, или вы можете использовать словарь для config -' config = {'gameType': "", "score": 0} ', а затем' commands (config) ',' gameEndless (config) 'и вам не нужно' return'. – furas