[Pygame] looping through dir of images with time delay

[Pygame] looping through dir of images with time delay

Postby metulburr » Mon Apr 22, 2013 8:59 pm

i know Mekire we touched up on some of this somewhere, but now i see where i NEEd it. Current code displays all images at one frame rate i think? which ends in a blur. So just to get code down as i am thinking it, i just blitted it in a for loop, in update() method of Control. Like you would for any print.

I think i get the concept. blit self.image every frame, and change self.image upon (well if a second) then 1000 mill passing pygame.time.get_ticks(). Which i tried to do this in the method iterate_images() in Control. The thing i am stuck at, is a counter for holding the next index for the next image to reassign to self.image. Currently all i think about is a generator, to handle this, but you say that is not a good idea (@ Mekire)? The only other solution then would be to hold the index.

Code: Select all
import pygame
import os
import time

class ImageClass:
   def __init__(self):
      self.image_dir = '/home/metulburr/Pictures/alchemy_images'
      self.image_dict = {}
      self.get_images()
   def get_images(self):
      for f in os.listdir(self.image_dir):
         if f.endswith('.png'):
            self.image_dict[os.path.splitext(f)[0]] = pygame.image.load(os.path.join(self.image_dir, f))

class Control:
   def __init__(self):
      pygame.init()
      self.screensize = (600,600)
      self.screen = pygame.display.set_mode(self.screensize)
      self.images = ImageClass()
      self.image = None
      self.clock = pygame.time.Clock()
      self.time_delay = 1000
      self.time = 0

      
   def text(self, displaytext, color=(0,0,0), size=15, ul=False, bold=False,
         ital=False, font='timesnewroman'):
      font = pygame.font.SysFont(font, size)
      font.set_underline(ul)
      font.set_bold(bold)
      font.set_italic(ital)
      label = font.render(displaytext, 1, color)
      return label
      
   def iterate_images(self):
      if (self.time + self.time_delay) >= pygame.time.get_ticks():
         #self.image = #assign next image to current image
         self.time = pygame.time.get_ticks()

      
   def update(self):
      self.screen.fill((50,50,50))
      for key, val in self.images.image_dict.items():
         
         self.screen.blit(val, (0,0))
         self.screen.blit(self.text(key, color=(255,255,255)), (0,100))
      
   def main(self):
      run = True
      while run:
         for event in pygame.event.get():
            if event.type == pygame.QUIT:
               run = False
         self.update()
         pygame.display.flip()
         self.clock.tick(60)
         

controller = Control()
controller.main()


EDIT: after some trying, maybe a dictionary is not the way to go as i cannot find a way to iterate through the dictionary upon 1000 milliseconds of a frame. MAybe a list of tuples is better? then i could iterate the index and grab the info from the tuple?

EDIT2:
OK now i got it (i think) to iterate through the images in the dir, and display its filename, but its hauling ass. I cant seem to figure out how to slow it down? I thought i handled this in the iterate_images, to only assing a new image every second.
Code: Select all
import pygame
import os
import time

class ImageClass:
   def __init__(self):
      self.image_dir = '/home/metulburr/Pictures/alchemy_images'
      self.image_dict = []
      self.get_images()
   def get_images(self):
      for f in os.listdir(self.image_dir):
         if f.endswith('.png'):
            #self.image_dict[os.path.splitext(f)[0]] = pygame.image.load(os.path.join(self.image_dir, f))
            self.image_dict.append(
               (os.path.splitext(f)[0], pygame.image.load(os.path.join(self.image_dir, f)))
               )
class Control:
   def __init__(self):
      pygame.init()
      self.screensize = (600,600)
      self.screen = pygame.display.set_mode(self.screensize)
      self.images = ImageClass()
      self.image = None
      self.clock = pygame.time.Clock()
      self.time_delay = 1000
      self.time = 0
      self.image_ind = 0

      
   def text(self, displaytext, color=(0,0,0), size=15, ul=False, bold=False,
         ital=False, font='timesnewroman'):
      font = pygame.font.SysFont(font, size)
      font.set_underline(ul)
      font.set_bold(bold)
      font.set_italic(ital)
      label = font.render(displaytext, 1, color)
      return label
      
   def iterate_images(self):
      if (self.time + self.time_delay) >= pygame.time.get_ticks():
         try:
            self.image = self.images.image_dict[self.image_ind]
            self.image_ind += 1
         except IndexError:
            self.image_ind = 0
         self.time = pygame.time.get_ticks()

   def update(self):
      self.iterate_images()
      self.screen.fill((50,50,50))
      self.screen.blit(self.image[1], (0,0))
      self.screen.blit(self.text(self.image[0], color=(255,255,255)), (0,100))
      
   def main(self):
      run = True
      while run:
         for event in pygame.event.get():
            if event.type == pygame.QUIT:
               run = False
         self.update()
         pygame.display.flip()
         self.clock.tick(60)
         

controller = Control()
controller.main()



EDIT3:
of course i can figure out how to iterate through the images using a key press, probably because i am not sure how to utilize the process of time_delay apparently.
Code: Select all
import pygame
import os
import time

class ImageClass:
   def __init__(self):
      self.image_dir = '/home/metulburr/Pictures/alchemy_images'
      self.image_dict = []
      self.get_images()
   def get_images(self):
      for f in os.listdir(self.image_dir):
         if f.endswith('.png'):
            #self.image_dict[os.path.splitext(f)[0]] = pygame.image.load(os.path.join(self.image_dir, f))
            self.image_dict.append(
               (os.path.splitext(f)[0], pygame.image.load(os.path.join(self.image_dir, f)))
               )
class Control:
   def __init__(self):
      pygame.init()
      self.screensize = (600,600)
      self.screen = pygame.display.set_mode(self.screensize)
      self.images = ImageClass()
      self.image = None
      self.clock = pygame.time.Clock()
      self.time_delay = 1000
      self.time = 0
      self.image_ind = 0

      
   def text(self, displaytext, color=(0,0,0), size=15, ul=False, bold=False,
         ital=False, font='timesnewroman'):
      font = pygame.font.SysFont(font, size)
      font.set_underline(ul)
      font.set_bold(bold)
      font.set_italic(ital)
      label = font.render(displaytext, 1, color)
      return label
      
   def iterate_images(self):
      if (self.time + self.time_delay) >= pygame.time.get_ticks():
         try:
            self.image = self.images.image_dict[self.image_ind]
            self.image_ind += 1
         except IndexError:
            self.image_ind = 0
         self.time = pygame.time.get_ticks()

   def update(self):
      #self.iterate_images()
      self.image = self.images.image_dict[self.image_ind]
      self.screen.fill((50,50,50))
      self.screen.blit(self.image[1], (0,0))
      self.screen.blit(self.text(self.image[0], color=(255,255,255)), (0,100))
      
   def main(self):
      run = True
      while run:
         for event in pygame.event.get():
            if event.type == pygame.QUIT:
               run = False
            elif event.type == pygame.KEYDOWN:
               if event.key == pygame.K_SPACE:
                  self.image_ind += 1
         self.update()
         pygame.display.flip()
         self.clock.tick(60)
         

controller = Control()
controller.main()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] looping through dir of images with time delay

Postby Mekire » Tue Apr 23, 2013 1:23 pm

Sorry, don't have so much time to reply at the moment (and possibly for the next week and a half). What I can tell however is your time inequality is backwards.

My:
Code: Select all
if pygame.time.get_ticks()-self.time >= self.time_delay:
is equal to:
Code: Select all
if (self.time+self.time_delay) <= pygame.time.get_ticks():


Not:
Code: Select all
if (self.time + self.time_delay) >= pygame.time.get_ticks():


Honestly I recommend you keep it in the form I originally had as I feel that is the most readable:

Code: Select all
if current_time - last_updated_time >= delay_between_updates:

I will look over it some more if I get a chance (I might be in China next week, life is crazy at the moment),
-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Pygame] looping through dir of images with time delay

Postby metulburr » Tue Apr 23, 2013 1:45 pm

orry, don't have so much time to reply at the moment (and possibly for the next week and a half).

hey no problem, you helped me out a lot, no need if your busy.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] looping through dir of images with time delay

Postby Mekire » Wed Apr 24, 2013 3:03 pm

Did you get this one figured out?

Code: Select all
def iterate_images(self):
    if (self.time + self.time_delay) <= pygame.time.get_ticks():
        self.image_ind = (self.image_ind+1)%len(self.images.image_dict)
        self.time = pygame.time.get_ticks()

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Pygame] looping through dir of images with time delay

Postby metulburr » Wed Apr 24, 2013 3:42 pm

actually not yet, i think i put that one on hold for some other part of pygame, and figuring it out. Ive got tabs upon tabs up with your code snippets, and my code snippets up in my IDE, trying to figure pygame methods out. I feel like i ma running around with my head cut off, regarding figuring how to do this in pygame and that in pygame. lol.

Ill e back to it though at some point
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to Game Development

Who is online

Users browsing this forum: Baldyr, jonin and 2 guests