Pygame is a free-to-use and open-source set of Python Modules. And as the name suggests, it can be used to build games, and to save game data or the last position of the player you need to store the position of the player in a file so when a user resumes the game its game resumes when he left. In this case, the text file with JSON format stores the data in form of a dictionary with keys, and values. Let’s see the implementation of the game in a stepwise manner.
Stop & Resume Your Game with Pause in Python
Step 1: Import libraries Pygame, and JSON.
import pygame from pygame.locals import * import json
Step 2: Take colors as RGB.
YELLOW = (255, 255, 0) BLUE = (0, 0, 255)
Step 3: Initialize Data Values in form of a dictionary.
data = { 'pos_x':320, 'pos_y':175 }
Step 4: Try to read the existing file and load file content to variable data using json.load(file_reference) method.
In this case, prev_pos.txt is the file. Except the file doesn’t exist, Create the file with initial data values using json.dump(data, file_reference).
try: with open('prev_pos.txt') as load_file: data = json.load(load_file) except: with open('prev_pos.txt','w') as store_file: json.dump(data,store_file)
Step 5: Construct the GUI
pygame.init()
Step 6: Set the dimensions of your GUI game.
w, h = 1200, 500 screen = pygame.display.set_mode((w, h))
Step 7: Take the image as input that you want to move with the mouse.
img = pygame.image.load('gfg.png') img.convert() rect = img.get_rect()
Step 8: Initialize the image to the given coordinates retrieved from the file.
rect.center = data["pos_x"], data["pos_y"]
Step 9: Set the running value for running the game and the moving value for moving the image.
running = True moving = False
Step 10: Set the things which you want your app to do when in a running state and give functionality with events.
while running: for event in pygame.event.get():
Step 11: If the user QUIT the game. The positions will be stored in the prev_pos.txt file.
if event.type == QUIT: with open('prev_pos.txt', 'w') as store_data: json.dump(data, store_data) running = False
Step 12: Continuously change pos_x and pos_y with mouse movements.
elif event.type == MOUSEMOTION and moving: rect.move_ip(event.rel) data["pos_x"] = event.pos[0] data["pos_y"] = event.pos[1]
Step 13: Set screen color, and image and construct a border to the image.
screen.fill(YELLOW) screen.blit(img, rect) pygame.draw.rect(screen, BLUE, rect, 2)
Step 14: Update the game
pygame.display.update()
Step 15: Quit the GUI game.
pygame.quit()
Below is the implementation.
Python3
import pygame from pygame. locals import * import json # Take colors input// YELLOW = ( 255 , 255 , 0 ) BLUE = ( 0 , 0 , 255 ) # initial data values data = { 'pos_x' : 320 , 'pos_y' : 175 } try : # the file already exists with open ( 'prev_pos.txt' ) as load_file: data = json.load(load_file) except : # create the file and store initial values with open ( 'prev_pos.txt' , 'w' ) as store_file: json.dump(data, store_file) # Construct the GUI game// pygame.init() # Set dimensions of game GUI// w, h = 1200 , 600 screen = pygame.display.set_mode((w, h)) # Take image as input// img = pygame.image.load( 'gfg.png' ) img.convert() # Draw rectangle around the image// rect = img.get_rect() rect.center = data[ "pos_x" ], data[ "pos_y" ] # Set running and moving values// running = True moving = False # Setting what happens when game # is in running state while running: for event in pygame.event.get(): # Close if the user quits the # game if event. type = = QUIT: with open ( 'prev_pos.txt' , 'w' ) as store_data: json.dump(data, store_data) running = False # Making the image move elif event. type = = MOUSEBUTTONDOWN: if rect.collidepoint(event.pos): moving = True # Set moving as False if you want # to move the image only with the # mouse click # Set moving as True if you want # to move the image without the # mouse click elif event. type = = MOUSEBUTTONUP: moving = False # Make your image move continuously elif event. type = = MOUSEMOTION and moving: rect.move_ip(event.rel) data[ "pos_x" ] = event.pos[ 0 ] data[ "pos_y" ] = event.pos[ 1 ] # Set screen color and image on screen screen.fill(YELLOW) screen.blit(img, rect) # Construct the border to the image pygame.draw.rect(screen, BLUE, rect, 2 ) # Update the GUI pygame pygame.display.update() # Quit the GUI game pygame.quit() |
Output: