Save/load game Function in Pygame
Last Updated :
23 Jan, 2023
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
YELLOW = ( 255 , 255 , 0 )
BLUE = ( 0 , 0 , 255 )
data = {
'pos_x' : 320 ,
'pos_y' : 175
}
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)
pygame.init()
w, h = 1200 , 600
screen = pygame.display.set_mode((w, h))
img = pygame.image.load( 'gfg.png' )
img.convert()
rect = img.get_rect()
rect.center = data[ "pos_x" ], data[ "pos_y" ]
running = True
moving = False
while running:
for event in pygame.event.get():
if event. type = = QUIT:
with open ( 'prev_pos.txt' , 'w' ) as store_data:
json.dump(data, store_data)
running = False
elif event. type = = MOUSEBUTTONDOWN:
if rect.collidepoint(event.pos):
moving = True
elif event. type = = MOUSEBUTTONUP:
moving = False
elif event. type = = MOUSEMOTION and moving:
rect.move_ip(event.rel)
data[ "pos_x" ] = event.pos[ 0 ]
data[ "pos_y" ] = event.pos[ 1 ]
screen.fill(YELLOW)
screen.blit(img, rect)
pygame.draw.rect(screen, BLUE, rect, 2 )
pygame.display.update()
pygame.quit()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...