-3

im new on this, and im using an if statement to load an image in case X thing happens, this is my code:

if  self.puntos > 0:
        self.fondo = load_image('Fondo2.png', IMG_DIR)
        self.primera_vez = True

So when puntos (points) get over 0 the background changes, the problem is that the new image loads constantly causing low framerates, is this because the if statement executes constantly or what? Thanks

MrBean
  • 1
  • 3
  • 2
    There is no possible way of knowing that from these 3 lines of code. But this "if" as shown will only execute once. – Idos Jan 30 '19 at 01:00
  • This code will only execute once, unless it's inside a loop. – John Gordon Jan 30 '19 at 01:02
  • that if is inside a definition that includes a bunch of other statements that work correctly, but of course they are all separated, no idea why the image loads constantly – MrBean Jan 30 '19 at 01:04
  • We need to see more code to know what's going on here. – Jab Jan 30 '19 at 01:04
  • im going to try to put the code on a separate definition – MrBean Jan 30 '19 at 01:05
  • It will load the image every time it executes, if `self.puntos` is greater than 0. It doesn't automatically only execute when `self.puntos` changes from less than to more than zero. – Blorgbeard Jan 30 '19 at 01:06
  • 1
    Generally speaking, you should load *all* your images when your program first starts -- that way they're already decompressed and in memory when you need them. – Charles Duffy Jan 30 '19 at 01:07
  • im working on a simple game with pygame – MrBean Jan 30 '19 at 01:08
  • 1
    I'm assuming this code is in the main loop, so yes, it will be executing every time. Do what @Charles suggests - before the main loop, put `img_fondo2 = load_image('Fondo2.jpg', IMG_DIR)`, then you can change the line in your `if` statement to `self.fondo = img_fondo2`, and it will be much faster. – Blorgbeard Jan 30 '19 at 01:11
  • @Blorgbeard that's what i think is happening, is there any way to make the image change only once? – MrBean Jan 30 '19 at 01:11
  • 1
    Every statement executes once, unless another part of the program tells it to execute again. e.g. a statement inside a function gets called once, *every time that function is called*, a statement inside a loop gets called *every time that loop is rerun*, and a statement in an if runs *only if the logical expression evaluates to true*. – 3D1T0R Jan 30 '19 at 01:15
  • Please [edit] your question to give more context to this code. – 3D1T0R Jan 30 '19 at 01:19
  • Also, what do you use `self.primera_vez = True` for? – 3D1T0R Jan 30 '19 at 01:24
  • @3D1T0R ok thanks for the comment, if i don't put that line the new background appears only where the different character's sprites are, for example i have my character wich i move and as i move it around the screen it starts showing the image where the sprite has been, till every space has been covered by any character – MrBean Jan 30 '19 at 01:40

1 Answers1

-3

Yes, you want to look to put the same code in a while block.

while self.puntos > 0:
        self.fondo = load_image('Fondo2.png', IMG_DIR)
        self.primera_vez = True

then when self.puntos is not > 0 it won't render

Bread
  • 49
  • 3
  • 1
    This is *worse* than the original -- the OP was complaining that they were seeing performance that implied that `load_image` was running more than once; now you're *ensuring* that it does so, even if the rest of their program is corrected! – Charles Duffy Jan 30 '19 at 01:07