Python Forum

Full Version: Resource or zip file?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Does anyone know of any solutions that have already been built to create something like a resource file, or zip files?

I've been writting a game with pygame, and it uses a lot of image files. The animation software i've been using creates a file for every frame, and that's the way i've been working on my projects till now. It wasn't an issue when I first started the project but nearing the end of the project I've got hundreds of files Dodgy . It would be neat if I could just put all my images into one file and get pygame to load them when needed Idea .

What do you think Think ?
Usually people put them in spritesheets so there are numerous frames in one image. You can merge them into a spritsheet and load and parse them out in the code.
I've seen a couple of examples of spritesheets in the past but I've got images of a spaceship that rotates 360 degrees with the use of around 72 frames. Would have been nice to be able to import a module into my project that reads an image from a resource file.

I guess its not huge leap from spritesheets to resource file Think , I just didn't want to reinvent the wheel.

Its good to get second opinion, thanks Smile .
(Apr-22-2019, 07:47 PM)microphone_head Wrote: [ -> ]I've got images of a spaceship that rotates 360 degrees with the use of around 72 frames
If something is rotating and is not changing in the image itself other than angle then there should only be one image. Pygame can rotate the image by itself. You shouldnt need 72 frames to rotate.
(Apr-23-2019, 02:32 AM)metulburr Wrote: [ -> ]If something is rotating and is not changing in the image itself other than angle then there should only be one image. Pygame can rotate the image by itself. You shouldnt need 72 frames to rotate.

I have been plesantly surprised at just how fast pygame is at rendering graphics Big Grin , but I've never bothered put it to the test Undecided . I've even been tempted to look into the 3D aspect, but I'm still having so much fun with 2D.

My game is more a proof of concept. I was creating this game Rocks In Space as a stepping stone to bigger projects, learning about some of the things I can and cannot get away with, learning about the pros and cons of doing things a certain way, like loading large amounts of graphics into memory at once and not suffering any performance penalties. Using seperate files for image frames instead of sprite sheets means my game takes longer to load.

Anyway thanks for help, I'm feeling confident enough to make something more adventurous Dance .
You can merge all image to a new biger image! I think that.
Thanks for the help Smile metulburr and friv2019. I'm starting to think along the same lines, using sprite sheets.
Your reinventing the wheel by using spritesheets to mimic rotation. Try this on your first ship image to rotate it 360 degrees. Change the master image to your ship image. One ship image at any angle, it doesnt matter.

import pygame as pg
 
screen = pg.display.set_mode((800,600))
screen_rect = screen.get_rect()
clock = pg.time.Clock()
done = False
 
class Rotator:
    def __init__(self, screen_rect):
        self.screen_rect = screen_rect
        self.master_image = pg.Surface([100,100]).convert_alpha()
        self.master_image.fill((255,0,0))
        self.image = self.master_image.copy()
        self.rect = self.image.get_rect(center=self.screen_rect.center)
        self.delay = 10
        self.timer = 0.0
        self.angle = 0
 
    def new_angle(self):
        self.angle += 1
        self.angle %= 360
 
    def rotate(self):
        self.new_angle()
        self.image = pg.transform.rotate(self.master_image, self.angle)
        self.rect = self.image.get_rect(center=self.screen_rect.center)
 
    def update(self):
        if pg.time.get_ticks()- self.timer > self.delay:
            self.timer = pg.time.get_ticks()
            self.rotate()
 
    def draw(self, surf):
        surf.blit(self.image, self.rect)
 
rotator = Rotator(screen_rect)
 
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
    screen.fill((0,0,0))
    rotator.update()
    rotator.draw(screen)
    pg.display.update()