0

I have this function:

def draw_image(image, xy ,draw_img=True,camera=False):
    all_images.append(Image(image, xy, draw_img, camera))
    #draw all images
    for image in all_images:
        image.run()
    pass

and in the class I have this:

class Image:
    def __init__(self, image, xy, draw_img, camera):
        self.image = image
        self.x = list(xy)[0]
        self.y = list(xy)[1]
        self.draw_img = draw_img
        self.camera = camera
        pass
    def run(self):
        global 
col_up,col_down,col_left,col_right,right_speed,left_speed,up_speed,down_speed,left_key,up_key,down_key,jump_velocity, left_key,right_key,up_key,down_key,run_once6,run_once3,jumped, time, last_time,change_x,player_speed,change_y
        if self.draw_img:
            if not camera:
                screen.blit(self.image, (self.x, self.y))
        if camera:
            if change_x == 'x-':    
                self.x += player_speed
            if change_x == 'x+':
                self.x -= player_speed
            if change_y == 'y-':
                self.y += player_speed
            if change_y == 'y+':
                self.y -= player_speed
        if self.draw_img:
            if camera:
                screen.blit(self.image, (self.x, self.y))
                
        pass
    pass

I need to call draw_image() in a while loop.
So, two things occur:
The first thing that occurs is that the program keeps adding to the list. I can fix this by placing the variable in the while loop.
The second thing is that the init file runs every frame. As such, everything in init() is ran multiple times, which is not supposed to happen. When I say self.x += 1, it is instantly reset to 0. As such, I'm not able to change the position of the image.

The_spider
  • 1,202
  • 1
  • 8
  • 18
  • So that it adds WHAT to WHICH list? Instantly reset WHAT to 0? Do you realize you can delete all of those `pass` statements and the `global` as well? – Tim Roberts Nov 03 '22 at 19:39
  • if i remove all the globals it gives me errors, and the pass is just because i feel more comfortable to use it. – Morris El Helou Nov 03 '22 at 19:40
  • The problem is, globals are evil. The code you are showing us does not CHANGE any of those values, so the statement is unnecessary. Removing it CANNOT cause errors, and if it does, you need to understand why. And I'm sorry, but your `pass` statement is silly. That's just not good software engineering. – Tim Roberts Nov 03 '22 at 20:24
  • @Tim Roberts, let's be nice to a new contributer. Morris El Helou, you need to be more clear and provide a [mre]. Is the list you mentioned ```all_images```? You said you don't want to create Image instances and add them to the list for every frame. Then, why not change the code so that it reuses instances created outside the loop? – relent95 Nov 06 '22 at 02:27

1 Answers1

0

If you want to control something over time in Pygame you have two options:

  1. Use pygame.time.get_ticks() to measure time and and implement logic that controls the object depending on the time.

  2. Use the timer event. Use pygame.time.set_timer() to repeatedly create a USEREVENT in the event queue. Change object states when the event occurs.

e.g.: Spawning multiple instances of the same object concurrently in python

Rabbid76
  • 202,892
  • 27
  • 131
  • 174