Pygame FG 05 – organizing code

In this episode I will try to put some order in the code to make it more readable in order to expand the project and make it usable for effective games.

I remind you that you can use pygame 2 dev 8 with python 3.8. Go to this post to see how.

The “well written” code

Now, in my intentions, you can understand what the code does just reading the functions and methos names.

# Pygame first gamet - tutorial 4
import pygame as pg
import sys
from glob import glob


class Sprite(pg.sprite.Sprite):
    def __init__(self, pw, ph, action):
        "loads all the image that starts with 'action' in position pw, ph"
        super().__init__()

        self.pw = pw
        self.ph = ph
        self.current = 0
        self.countframe = 0
        self.action = action
        self.order_images()
        self.position_sprite()

    def order_images(self):
        "take the list of images (load_images()) and order them"
        lst = self.load_images()
        img = [f for f in lst if len(f) == len(lst[0])]
        img.extend([f for f in lst if len(f) != len(lst[0])])
        self.sprites = [pg.image.load(x) for x in img]
        self.image = self.sprites[self.current]

    def load_images(self):
        "Load images that starts with action"
        folder = "cat\\"
        lst = glob(f"{folder}{self.action}*")
        return lst

    def position_sprite(self):
        self.rect = self.image.get_rect()
        self.rect.topleft = [self.pw, self.ph]

    def update(self):
        "This animate the sprite, it is called in the while loop by th update(sprite) function"
        self.countframe += 1
        # the more is the number the more the animation is slow
        if self.countframe == 16:
            # restart the counter
            self.countframe = 0
            # The new animation image is flipped after 4 frames
            self.current += 1
            # check if the images of the animations are ended
            if self.current >= len(self.sprites):
                # if so it goes back to the initial image
                self.current = 0
            # and now it shows the next image or the initial one
            self.image = self.sprites[self.current]


def update(sprite):
    sprite.draw(screen)
    sprite.update()


def window(title):
    "Create the screen + title and the clock object for the frame"
    screen = pg.display.set_mode((400, 300))
    pg.display.set_caption(title)
    return screen, pg.time.Clock()


def quitbutton():
    "Look for the key events"
    global loop
    for event in pg.event.get():
        if event.type == pg.QUIT:
            loop = 0


def clear_screen():
    screen.fill((0, 64, 128))


def refresh_screen():
    pg.display.flip()
    clock.tick(60)


def show_sprites():
    "Iterate all the sprites in the dsprites"
    for s in dsprites:
        g.add(dsprites[s])
        update(g)


# ============== FRAME RATE FUNCTIONS =====

def create_fonts(font_sizes_list):
    "Creates different fonts with one list"
    fonts = []
    for size in font_sizes_list:
        fonts.append(
            pg.font.SysFont("Arial", size))
    return fonts


def render(fnt, what, color, where):
    "Renders the fonts as passed from display_fps"
    text_to_show = fnt.render(what, 1, pg.Color(color))
    screen.blit(text_to_show, where)


def display_fps():
    "Data that will be rendered and blitted in _display"
    render(
        fonts[0],
        what=str(round(clock.get_fps(), 1)),
        color="white",
        where=(0, 0))
# put this after pygame.init() or pg.init()
# fonts = create_fonts([32, 16, 14, 8])
# ==== END ==== #



def update_screen():
    "The while loop updates"
    quitbutton()
    clear_screen()
    display_fps()
    show_sprites()
    refresh_screen()

# All the sprites that are updated in update_screen => show_sprites
dsprites = {
    "pwalk": Sprite(50, 30, "Walk"),
    "prun": Sprite(200, 70, "Run"),
    "pjump": Sprite(150, 150, "Jump"),
}

pg.init()
g = pg.sprite.Group()
fonts = create_fonts([32, 16, 14, 8])
screen, clock = window("Game")
loop = 1
# ============== Where everything happens
while loop:
    update_screen()
# ======================== engine end ===

pg.quit()
sys.exit()

The video – fast speed coding


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.