Open a text file then write the game level onto it with python

Welcome back, let us continue with our previous pygame project. In this article we will first create a text file which will be used to store the game level. This text file will be called level.txt which will be stored within the project folder. We need to create this text file by hand and type in 1 which is the first level of the game onto it, this level number will be rewritten each and every time the user has pressed the home button on the pause scene to go back to the homepage of the game. The reason why we are not letting the program to create the level.txt file for us is because when we have installed the game on our pc later on our computer’s os might not let the game program to create that text file on it’s won folder due to the security reason, therefore it is always the best practice for the game developer to create our own text file even if it is an empty file so the program will be able to edit that file later on. OK so much for that, lets create the text file first and write the starting level onto it.

pycharm's interface
pycharm’s interface

The next thing we need to do is to edit the level manager class. Now the level variable will directly get the level number from the text file.

import os

class LevelManager(object):

    def __init__(self, gm):

        self.game_manager = gm

            f = open("level.txt", "r")
                if f.mode == 'r':
                    f1 = f.readlines()
                    for x in f1:
                        self.level = int(x)
        except IOError:

        self.MAX_LEVEL = 3

    def increase_level(self):

        self.level += 1
        if(self.level >  self.MAX_LEVEL):
            self.game_manager.state = self.game_manager.WIN
            self.level = 1
            self.game_manager.state = self.game_manager.NEXT

    def get_level(self):

        return self.level

One thing you might have noticed from the top program is that we actually do not need to create loop when reading the content of the text file because it only consists of one line, the reason why we have created a loop is because we will have more than one line of game level in the future so we do need this loop and we will further modify this loop content in the future.

The last part is to edit the game manager class which will rewrite the game level to the text file each time the user has clicked on the home button on the pause scene to get back to the main game page.

from Player import Player
from Background import Background
from EnemyManager import EnemyManager
from Overlap import Overlap
from ExplosionManager import ExplosionManager
from Score import Score
from StartScene import StartScene
from pygame.locals import *
from LevelManager import LevelManager
import pygame
import webbrowser
import os

class GameManager(object):

    def __init__(self, scene):

        self.scene = scene
        self.start_scene = StartScene(scene)
        self.overlap_manager = Overlap()
        self.level_manager = LevelManager(self)


        self.pause = False # flag to pause the game

        #game state
        self.LOAD = 0
        self.GAME = 1
        self.OVER = 2
        self.NEXT = 3
        self.WIN = 4
        self.ABOUT = 5
        self.MANUAL = 6
        self.PAUSE = 7

        self.state = self.LOAD

    def setup(self, game_level):

        self.game_level = game_level
        self.score_manager = Score(self.scene)
        self.background = Background(self.scene)
        self.player = Player(self.scene)
        self.enemy_manager = EnemyManager(self.scene, self.player, game_level)
        self.explosion_manager = ExplosionManager(self.scene)

    def loop(self):

        if(self.state == self.LOAD or self.state == self.OVER or self.state == self.NEXT or self.state == self.WIN or self.state == self.ABOUT or self.state == self.MANUAL):


        elif(self.state == self.GAME):


        elif(self.state == self.PAUSE):


    def isAreaClick(self, pos):
        if (self.state == self.LOAD or self.state == self.OVER or self.state == self.NEXT or self.state == self.WIN or self.state == self.ABOUT or self.state == self.MANUAL or self.state == self.PAUSE):
            self.rect = Rect(177, 274, 306, 112) # the position of the play button on the scene
            self.rect_play = Rect(229, 200, 200, 53)  # the position of the play button on the home scene
            self.rect_about = Rect(229, 263, 200, 53)  # the position of the about button on the home scene
            self.rect_pause_home = Rect(229, 263, 200, 53)  # the position of the home button on pause scene
            self.rect_manual = Rect(229, 393, 200, 53)  # the position of the manual button on the home scene
            self.rect_back = Rect(10, 620, 40, 30)  # the position of the back button on the home scene
            self.rect_sound = Rect(10, 620, 29, 30) # the position of the sound button on the home scene
            x, y = pos
            if(self.rect.collidepoint(x, y) and (self.state == self.OVER or self.state == self.NEXT or self.state == self.WIN)):
                self.state = self.GAME
            elif(self.rect_play.collidepoint(x,y) and self.state == self.LOAD):
                self.state = self.GAME
            elif (self.rect_play.collidepoint(x, y) and self.state == self.PAUSE):
                self.state = self.GAME
            elif (self.rect_about.collidepoint(x, y) and self.state == self.LOAD):
                self.state = self.ABOUT
            elif (self.rect_pause_home.collidepoint(x, y) and self.state == self.PAUSE):

                self.state = self.LOAD

                    f = open("level.txt", "w+")
                except IOError:
                    print ('cannot open the file')

            elif (self.rect_manual.collidepoint(x, y) and self.state == self.LOAD):
            elif (self.rect_back.collidepoint(x, y) and (self.state == self.ABOUT or self.state == self.MANUAL)):
                self.state = self.LOAD
            elif (self.rect_sound.collidepoint(x, y) and self.state == self.LOAD):

                if(self.start_scene.soundon == True):
                    self.start_scene.soundon = False
                    self.start_scene.soundon = True

    def set_pause(self, pause):

        self.pause = pause

        if(self.pause == True):

            self.state = self.PAUSE

    def load_music(self):

    def play_music(self): #play the music infinite time

    def set_player_x(self, _x):
        if (self.state == self.GAME):

    def set_player_y(self, _y):
        if (self.state == self.GAME):

    def set_missile_strike(self, strike):
        if (self.state == self.GAME):

    def update(self):

    # check for player, enemy, missiles overlap
    def isOverlap(self):
        self.overlap_manager.isOverlap(self.player, self.enemy_manager, self.explosion_manager, self.score_manager, self)

    def draw(self):
        if(self.state == self.GAME):

That is basically it, in the next chapter we will work on the score scene where the program will list out 5 highest levels that the player has achieved with the help of the level.text file which we have just created earlier in this chapter.

Leave a Reply

Your email address will not be published. Required fields are marked *