I'm trying to create a way in pygame for the player to skip the tutorial phase simply by pressing "escape". However, I can't seem to get it to work when using the following:
for event in pygame.event.get():
if event.type == pygame.K_ESCAPE:
brek2 = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
run2 = False
brek = True
if brek or brek2:
break
pygame.time.delay(1000)
if brek:
break
Here's the full loop of the tutorial phase if you need it:
while run2:
# intro sequence
pygame.event.pump()
fade(screen_width, screen_height, BLACK)
fade(screen_width, screen_height, WHITE)
techi = pygame.transform.scale(pygame.image.load("Techi-Joe.gif"), (75, 75))
bg = pygame.Surface((screen_width, screen_height))
bg.fill(WHITE)
win.blit(pygame.transform.scale(bg, (screen_width, screen_height)), (0, 0))
win.blit(techi, (100, 600))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
run2 = False
pygame.time.delay(1000)
brek2 = False
presss = 1
brek = False
techi_bubble = pygame.transform.scale(pygame.image.load("techi_speech_bubble.png"), (700, 350))
techi_font = pygame.font.Font('Consolas.ttf', 25)
for a in range(len(techi_says)):
win.blit(pygame.transform.scale(bg, (screen_width, screen_height)), (0, 0))
win.blit(techi_bubble, (175, 300))
win.blit(techi, (100, 600))
writeLikeCode(techi_says[a], 235, 350, techi_font, 20)
for event in pygame.event.get():
if event.type == pygame.K_ESCAPE:
brek2 = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
run2 = False
brek = True
if brek or brek2:
break
pygame.time.delay(1000)
if brek:
break
Here's some addition code referenced as function writeLikeCode:
techi_says = ["Hello, User!", "My name is Techi-Joe.", "I'm a programmer,", "and this is my first game:",
"Dunk the Chief!", "in Dunk the Chief,", "your main objective", "is to dunk any US president", "you choose",
"in a cold tub of water!", "yay!"]
# writeLikeCode function
def writeLikeCode(string, xpos, ypos, font, fontsize):
for x in range(len(string)):
lstring = split(string)
text = font.render(lstring[x], 1, GREEN)
win.blit(text, (xpos + (x * fontsize), ypos))
pygame.time.delay(50)
pygame.display.update()
and here's the split function:
def split(string):
return [char for char in string]
pygame.event.get()
does not only get the events, It also removes the events from the event queue. If you call pygame.event.get()
twice in a row, then the first call will retrieve the events, but the 2nd call returns an empty list.
for event in pygame.event.get(): # returns the list of events # [...] for event in pygame.event.get(): # returns empty list # [...]
If the event occurs after between the 2 calls of pygame.event.get()
, then the event will be received be the 2nd call. But that will be a rare case.
pygame.K_ESCAPE
is not an event type (see pygame.event
), it is a key see (pygame.key
).
If you want to detect if the ESC was pressed, then you have to detect the KEYDOWN
event and to evaluate if the .key
attribute is K_ESCAPE
. e.g:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
run2 = False
brek = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
brek2 = True