1

I have a program based on pygame that creates a display and every time you hit Space or Enter it prints one of the strings on the list. ALso it has an ingame menu which you can open by pressing 'Esc' but for some reason, which I cannot figure out when I press 'Esc' x sums up and prints the strings when it is not supposed to. Could someone help me solving this problem?

This is my code:

import pygame
import sys

pygame.init()

screenSize = (1000, 600)
fullscreen = 0

gameDisplay = pygame.display.set_mode(screenSize, fullscreen)
clock = pygame.time.Clock()

black = (0, 0, 0)
purple = (174, 55, 174)

strings = ['string1', 'string2', 'string3', 'string4', 'string5']


def scenemanager(x=0):
    gameDisplay.fill(black)
    while x <= len(strings):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit(), sys.exit(), quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    ingame_menu()
                if event.key == pygame.K_SPACE or pygame.K_KP_ENTER:
                    if x <= len(strings)-1:
                        print(strings[x])
                        gameDisplay.fill(black)
                    x += 1
        pygame.display.update()
        clock.tick(60)


def ingame_menu():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit(), sys.exit(), quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    return False

        gameDisplay.fill(purple)
        pygame.display.update()
        clock.tick(60)

scenemanager()
  • Please describe the desired behavior precisely. It's not really clear what you want to achieve. – skrx Jun 29 '18 at 13:55
  • I wnat that every time I hit space or enter it prints one of the items on the list each time. I also want to make so if a hit escape it opens another menu. – ProgramMeALife Jun 29 '18 at 14:22
  • 2
    The problem is a [classic beginner's mistake](https://stackoverflow.com/a/17284605/6220679). Change `if event.key == pygame.K_SPACE or pygame.K_KP_ENTER:` to `if event.key == pygame.K_SPACE or event.key == pygame.K_KP_ENTER:` or `if event.key in (pygame.K_SPACE, pygame.K_KP_ENTER):`. – skrx Jun 29 '18 at 14:51

0 Answers0