[This is a really long code so im just going to put the traceback part in and where I think the problem is]

[def directions():
enter=0
nwscreen()
Font1 = pygame.font.SysFont('ActionIsShaded', 25)
d1 = basicFont.render('Directions',False,WHITE,BLACK)
d2 = Font1.render('Directions',False,WHITE,BLACK)
d3 = Font1.render('ALL ANSWERS MUST BE LOWERCASE',False,RED,BLACK)
d4 = Font1.render ('Press enter to continue...',False,GREEN,BLUE)
d1Rect = d1.get_rect()
d2Rect = d2.get_rect()
d3Rect = d3.get_rect()
d4Rect = d4.get_rect()
d1Rect.centerx = windowSurface.get_rect().centerx
d1Rect.centery = windowSurface.get_rect().centery-175
d2Rect.centerx = windowSurface.get_rect().centerx
d2Rect.centery = windowSurface.get_rect().centery-140
d3Rect.centerx = windowSurface.get_rect().centerx
d3Rect.centery = windowSurface.get_rect().centery-105
d4Rect.centerx = windowSurface.get_rect().centerx
d4Rect.centery = windowSurface.get_rect().centery-70
windowSurface.blit(d1,d1Rect)
windowSurface.blit(d2,d2Rect)
windowSurface.blit(d3,d3Rect)
windowSurface.blit(d4,d4Rect)
pygame.display.update()
while enter==0:
pygame.event.get()
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_KP_ENTER:
enter=enter+1
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
]


[Traceback (most recent call last):
File "C:\Python31\", line 250, in <module>
directions()
File "C:\Python31\", line 63, in directions
if event.type == KEYDOWN:
KeyboardInterrupt]


[Can anyone help me]

Dani AI

Generated

The traceback shows a KeyboardInterrupt (Ctrl+C) rather than a NameError or pygame exception. That usually means the program was manually stopped while the loop was spinning. The root cause in the posted snippet is calling pygame.event.get() without iterating its return and then testing event — that leaves the loop never processing fresh events and can exhaust the CPU. and @vegaseat pointed toward the right pattern: iterate the event list and break the wait properly.

A compact, safe pattern that avoids busy-waiting, handles both Enter keys, and keeps the loop responsive:

clock = pygame.time.Clock()
waiting = True
while waiting:
    clock.tick(30)                       # limit CPU usage
    for ev in pygame.event.get():        # pull and iterate events
        if ev.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if ev.type == pygame.KEYDOWN:
            if ev.key in (pygame.K_RETURN, pygame.K_KP_ENTER):
                waiting = False
            elif ev.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit()

Notes and troubleshooting:

  • Ensure either import pygame and use pygame.KEYDOWN/pygame.K_RETURN, or have from pygame.locals import * so unqualified names work.
  • If the loop still seems stuck, add a brief print(pygame.event.get()) to confirm events are arriving. An empty list means no input events reached Pygame.
  • Prefer returning from this function (or setting a flag) instead of calling sys.exit() from deep inside helper functions — it makes testing and cleanup easier.
  • Wrapping in a try/except KeyboardInterrupt can make Ctrl+C exit cleaner for debugging, but it does not fix the underlying event-loop logic.

This ties back to ’s original code and the suggestions from /@vegaseat: iterate the event queue, limit frame rate, and explicitly break the waiting loop when Enter is detected.

Recommended Answers

All 2 Replies

code-tags, please.

(This is a really long code so im just going to put the traceback part in and where I think the problem is)

(Can anyone help me)

def directions():
    enter=0
    nwscreen()
    Font1 = pygame.font.SysFont('ActionIsShaded', 25)
    d1 = basicFont.render('Directions',False,WHITE,BLACK)
    d2 = Font1.render('Directions',False,WHITE,BLACK)
    d3 = Font1.render('ALL ANSWERS MUST BE LOWERCASE',False,RED,BLACK)
    d4 = Font1.render ('Press enter to continue...',False,GREEN,BLUE)
    d1Rect = d1.get_rect()
    d2Rect = d2.get_rect()
    d3Rect = d3.get_rect()
    d4Rect = d4.get_rect()
    d1Rect.centerx = windowSurface.get_rect().centerx
    d1Rect.centery = windowSurface.get_rect().centery-175
    d2Rect.centerx = windowSurface.get_rect().centerx
    d2Rect.centery = windowSurface.get_rect().centery-140
    d3Rect.centerx = windowSurface.get_rect().centerx
    d3Rect.centery = windowSurface.get_rect().centery-105
    d4Rect.centerx = windowSurface.get_rect().centerx
    d4Rect.centery = windowSurface.get_rect().centery-70
    windowSurface.blit(d1,d1Rect)
    windowSurface.blit(d2,d2Rect)
    windowSurface.blit(d3,d3Rect)
    windowSurface.blit(d4,d4Rect)
    pygame.display.update()
    while enter==0: 
        pygame.event.get() 
        if event.type == QUIT: 
            pygame.quit() 
            sys.exit() 
        if event.type == KEYDOWN: 
            if event.key == K_KP_ENTER: 
                enter=enter+1
            if event.key == K_ESCAPE: 
                pygame.quit() 
                sys.exit()

[Traceback (most recent call last):
  File "C:\Python31\pytest.py", line 250, in <module>
    directions()
  File "C:\Python31\pytest.py", line 63, in directions
    if event.type == KEYDOWN:
KeyboardInterrupt]

Maybe you need to put code in

try.. except KeyboardInterrupt:

block somehow.

From this post of vegaseat: http://www.daniweb.com/forums/post1275991.html#post1275991
though I see the event taken like this:

keepGoing = True
    while keepGoing:
        #clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.