简体   繁体   中英

how restarting game on pygame works

how can I restart the game with user input? i searched all over the place and i wasnt able to discover how i can restart my game, i just want to press ESC and my game restart, after knowing how to do that i will implement a button, but how can I restart my game? this is my main loop:

while True:
    
    pygame.time.Clock().tick(fps)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()

    #input
        elif event.type == pygame.KEYDOWN:
            #snake
            if event.key == ord('w'):
                change_to = 'UP'
            if event.key == ord('s'):
                change_to = 'DOWN'
            if event.key == ord('a'):
                change_to = 'LEFT'
            if event.key == ord('d'):
                change_to = 'RIGHT'   
                    
            #snake2
            if event.key == pygame.K_UP:
                change2_to = 'UP'
            if event.key == pygame.K_DOWN:
                change2_to = 'DOWN'
            if event.key == pygame.K_LEFT:
                change2_to = 'LEFT'
            if event.key == pygame.K_RIGHT:
                change2_to = 'RIGHT'

            #quit game
            if event.key == pygame.K_ESCAPE:
                #here is where was supposed to restart the game

i cut the major part to not be so long but i dont know how to restart my game

I think it should be something like this

# imports
restart = False
while running:
    if restart:
        # create instance of all objects used
        # score = 0
        # Default values should be initialised
    # Keyboard events go here
    # code for restarting
    if event.key == pygame.K_ESCAPE:
        restart = True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM