Python and games with pygame, part 8 – Lotta sprites

In this post we will see hot to put a lot of sprites at the same time in the window with python and pygame.

This is the code

# Pygame first gamet - tutorial 4
import pygame as pg
import sys
from glob import glob
from random import randrange, choice


frame = pg.Surface([80, 30])
frame.fill((0, 0, 0))
count_fps = 0
class Sprite(pg.sprite.Sprite):
    def __init__(self, pw, ph, folder, action):
        "loads all the image that starts with 'action' in position pw, ph"
        super().__init__()

        self.pw = randrange(10, 600)
        self.ph = randrange(10, 400)
        self.current = 0
        self.countframe = 0
        self.folder = folder
        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]
        self.size = self.image.get_size()
        self.cover = pg.Surface(self.size)
        # use the same color of the background to hide the sprite
        self.cover.fill((0, 128, 0))

    def load_images(self):
        "Load images that starts with action"
        lst = glob(f"{self.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"
        global count_fps
        count_fps +=1
        self.countframe += 1
        # the more is the number the more the animation is slow
        if self.countframe == 20:
            #print(clock.get_fps())
            # 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]
            # Use hide sprites for higher performances
            hide_sprites()
            # screen.fill((0, 0, 255))
            # mask.fill()
            g.draw(screen)
            # mask = pg.mask.from_threshold(self.image, (255, 255, 0))
        if count_fps == 200:
            count_fps = 0
            # screen.fill((0, 0, 255))
            screen.blit(frame, (0, 0))
            display_fps()

def window(title, w=400, h=400):
    "Create the screen + title and the clock object for the frame"
    screen = pg.display.set_mode((w, h))
    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():
    pass
    # screen.fill((0, 64, 128))


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


def hide_sprites():
    screen.blits(blit_sequence=(hiders))


def sprites_init():
    global hiders

    # Sprite(pw, ph, folder, action)
    rfolder = lambda: choice(["cat", "dog"])
    raction = lambda: choice(["idle", "walk", "jump", "run", "slide"])
    list_of_sprites = []
    for s in range(50):
        sprite = Sprite(100, 60, rfolder(), raction())
        list_of_sprites.append(sprite)
        g.add(sprite)

    # Blocks to hide sprites - to be used in hidesprites
    hiders = []
    for sprite in list_of_sprites:
        hiders.append([sprite.cover, (sprite.pw, sprite.ph)])
    return list_of_sprites


# ============== 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))
# fonts = create_fonts([32, 16, 14, 8])
# ============================= font functions ====


def game_init():
    "Initialize pygame, sprites, fonts, screen"
    global g, fonts, screen, clock, loop, list_of_sprites
    pg.init()
    g = pg.sprite.Group()
    list_of_sprites = sprites_init()
    fonts = create_fonts([32, 16, 14, 8])
    screen, clock = window("Game", w=700, h=500)
    screen.fill((0, 128, 0))
    loop = 1


def update_screen():
    "The while loop updates"
    quitbutton()
    g.update()
    refresh_screen()


game_init()
while loop:
    update_screen()

pg.quit()
sys.exit()

The images

If you want to test the code, you will probably need these images or use your own.

Images of the cat

images of the dog

Script used to resize images of cat and dog

If you are wondering how I resized all the images, here is the script.

''' Resizer

Resize images with PIL
'''

from PIL import Image
import os

print("Resize images in the folder")
folder = input("folder: ")
w = int(input("width: "))
h = int(input("height: "))
for i in os.listdir(folder):
    file = f"{folder}\\{i}"
    im = Image.open(file)
    im = im.resize((w, h), Image.ANTIALIAS)
    im.save(file)


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.