In this chapter we will edit the main file of the
import pygame from pygame.locals import * from GameManager import GameManager pygame.init() size = width, height = 640, 640 pygame.display.set_caption("Amaze Boy") # set the title of the window screen = pygame.display.set_mode(size) game_manager = GameManager(screen) rect_exit = Rect(229, 456, 200, 53) # the position of the exit button on the home scene running = True pygame.key.set_repeat(100, 40) while running: for event in pygame.event.get(): # when the user clicks on the 'x' if event.type == pygame.QUIT: game_manager.save_level() running = False # detect key press event if event.type == KEYDOWN: if (game_manager.state == 1): if event.key == K_LEFT: game_manager.set_player_x(-2) elif event.key == K_RIGHT: game_manager.set_player_x(2) elif event.key == K_UP: game_manager.set_player_y(-2) elif event.key == K_DOWN: game_manager.set_player_y(2) elif event.type == KEYUP: if (game_manager.state == 1): if event.key == K_LEFT: game_manager.set_player_x(0) elif event.key == K_RIGHT: game_manager.set_player_x(0) elif event.key == K_UP: game_manager.set_player_y(0) elif event.key == K_DOWN: game_manager.set_player_y(0) elif event.key == K_p: game_manager.set_pause(True) # set the pause state to true elif event.key == K_s: # take the game screenshot game_manager.save_scene() elif event.type == pygame.MOUSEBUTTONDOWN: x, y = event.pos if (rect_exit.collidepoint(x, y) and game_manager.state == game_manager.LOAD): game_manager.save_level() running = False # 1 is the left mouse button elif event.button == 1: game_manager.isAreaClick(x, y) game_manager.loop()
We have already included the code of the save_level method of the game manager class in the previous chapter so we will not show that same code again in this chapter. Here is the final outcome.
Working on the exit button for the ongoing @pygame project pic.twitter.com/qquSj7AOE5
— TechLikin (@ChooWhei) March 13, 2019
Like, share or follow me on Twitter.