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()