Problem with code...images, random function etc

edited August 2017 in Python Mode

I am trying to get a bomb to drop and when it hits the ground, it brings up an image of an explosion - the bomb resets and drops again.

NEXT I want a new random location for the bomb. I don't know where I am going wrong. I am trying to make this more efficient by using function for each thing that I am doing. I am sure there is something major wrong here, but I just can't make heads of tails of it right now . I don't know how to attach the two images.

I also had to make up my own TIME DELAY because I can't figure out how to do it.

start_drop=0

def setup():
    global img, img2, random_start_x
    size(400, 400)
    background(255) # white
    img=loadImage("bombsmall.jpg")
    img2=loadImage("explosion.jpg")
    random_start_x=int(random(1,400))    

def draw():
    global img, img2
    random_start()

def random_start():
    random_start_x=int(random(1,400))    
    print("This is the new start on x ", random_start_x)     
    bomb()

def bomb():
    global start_drop, img, img2, random_start_x
    background(255)
    image(img, random_start_x, start_drop)
    start_drop=start_drop+5
    if start_drop>360:
        c=0
        while c<1000:
           explosion()
           print(c)
           c=c+1
        start_drop=0
        random_start()

def explosion():
       global img2
       print("test")
       img2=loadImage("explosion.jpg")
       image(img2, random_start_x, 100)
Tagged:

Answers

  • edited August 2017
    1. It has something to do with the background(255) covering up the explosion I believe.
    2. Still dont get why the random isnt working and picking a new random place for the bomb to drop each time
  • Ok, I am breaking it down into easier to read code - just the basics now, with no images and no random functions.

    start_drop=0
    
    def setup():
        size(400, 400)
        strokeWeight(5)
    
    def draw():
        background(255)
        bomb()
    
    def bomb():
        global start_drop
        rect(200,start_drop,20,20)
    
        start_drop=start_drop+5
        if start_drop>350:
            c=0
            explosion()
            while c<2000:
               print("test")
    
               ellipse(200, 400, 50, 50)
               print(c)
               c=c+1
            start_drop=0
    
    def explosion():
           print("test")
           ellipse(200, 400, 50, 50)
    
  • ok, I am getting it. It was the while loop(which was my time delay attempt) that is causing the image problem, now onto the random placement.

  • edited August 2017

    Ok I figured it out, but without the time delay it doesnt look very good. The other issue is that when attempting to use functions (I commented out the functions I wanted to use), I cant seem to get it to work. Here is the working code. I wish I understood the original issue with the while loop, time delay and why when creating functions for each individual thing I want to happen (see explosion function below), things seem to go awry.

    def setup():
        global img, img2, rand_start
        size(400, 400)
        strokeWeight(5)
        img=loadImage("explosion.jpg")
        img2=loadImage("bombsmall.jpg")
        rand_start=int(random(1,400))
    
    def draw():
        background(255)
        bomb()
    
    def bomb():
        global start_drop, c, rand_start
        image(img2, rand_start, start_drop)
    
        start_drop=start_drop+5
        if start_drop>350:
            start_drop=0
            image(img, rand_start, 350)
            rand_start=int(random(1,400))
    #       explosion()
    
    # def explosion():
    #        print("test")
    #        image(img, rand_start, 350)
    
  • The image you see only updates at the end of draw. Having a delay inside draw just means is longer until you see that image. And any intermediate startups will be lost or will all appear at the same time.

    Read the FAQ about setup and draw.

  • Thank you. I appreciate your patience in helping me understand this. I just finished reading the FAQ on setup and draw, but I am still having a hard time understanding this. I am looking at my latest post that is the stripped down version.

    -Setup is just to load everything once - I load the images, size of window, strokeweight and random integer -Draw runs continually, like a movie. - this is going to run the code over and over.

    So in my code, it goes through the setup first and then jumps to the DRAW function. It then draws the background (white) and then jumps to the BOMB() function.

    In the BOMB() function, it runs it loads the bomb image and adds 5 on the y axis , it then jumps back to the draw function and add 5 again. This will run until it reaches the conditional statement of > 350. Once it reaches the bottom (350), it creates a new image (which is the explosion) and it starts all over again. In the conditional statement, right after the EXPLOSION image is where I need to put in a time delay of some sort.
    Do I have this right so far?

  • edited August 2017

    I show how to manage a time delay properly in draw, or at least, one way to do it. Check it below.

    Kf

    start_drop=0
    target_time=-1;
    setExplosion=0
    
    def setup():
        global img, img2, rand_start
        size(400, 400)
        strokeWeight(5)
        img=loadImage("img1.png")  #explosion
        img2=loadImage("img2.png") #smallbomb
        rand_start=int(random(1,400))
    
    def draw():
        background(255)
        bomb()
    
    def bomb():
        global start_drop, c, rand_start,target_time,setExplosion
    
    
        start_drop=start_drop+5
    
        if start_drop<350:
            image(img2, rand_start, start_drop)
            target_time=millis()+2000;    ## 2000ms or 2 sec       
        else:
            setExplosion=1;       
    
        if(target_time-millis()>0 and start_drop>=350):
            explosion()  
        else:
            setExplosion=0
    
        if(target_time-millis()<0 and start_drop>=350):
            start_drop=0 
            rand_start=int(random(1,400))        
    
    def explosion():
        print("test")
        image(img, rand_start, 350)
    
  • edited August 2017

    ... right after the EXPLOSION image is where I need to put in a time delay of some sort.

    You need to have a means to say that bomb has entered the EXPLODING state from its previous MOVING state.

    It's a very good idea to define some class for your Bomb, so its attributes and states will be encapsulated in 1 place only.

    Here's an online example in Java for a fireworks animation.
    It's complicated, but just as you can have some idea on how to deal w/ animation & state:

    http://Studio.ProcessingTogether.com/sp/pad/export/ro.9Q6oRai8-41WJ/latest

    Pay special attention to Firework::display() method:

    void display() {
      if (isInactive)   return;
    
      if (hasExploded)  exploding();
      else              rising();
    }
    

    It checks whether field hasExploded is true or false.
    In this case, there are 2 states only: Either the Firework is in a rising() or in an exploding() animation.

    There's also a 3rd state here too, which is isInactive.
    It becomes true after the exploding() animation has finished.

    void explodeMaths() {
      if (++explodeTimer > explosionDuration)  disable();
    }
    
    void disable() {  
      isInactive  = true;
    }
    
  • edited August 2017

    kfrajer ,

    I copied/pasted your code and it works great! BUT I am still trying to figure this out.

        if start_drop<350:
            image(img2, rand_start, start_drop)
            target_time=millis()+2000    ## 2000ms or 2 sec       
        else:
            setExplosion=1    
    

    If the bomb is in the air, and hasn't reached 350 units (the ground), then you set the target_time to whatever the milliseconds are + 2000. So from the very top, you are starting a timer by taking the computer time of milliseconds and adding 2000?

    Else, the explosion is set to 1 (or similar to a boolean, TRUE FALSE, I believe)

    if(target_time-millis()>0 and start_drop>=350):
            explosion()  
        else:
            setExplosion=0
    

    If the target_time subtract milliseconds is greater than 0 (I am not understanding this part) and the bomb has HIT the ground (350), go to the EXPLOSION function. Else, set the explosion to ZERO

        if(target_time-millis()<0 and start_drop>=350):
            start_drop=0
            rand_start=int(random(1,400))        
    

    If the target_time subtract the milliseconds is LESS than 0 this time (again, I am not getting this part) and it has hit the ground (<350), repeat the animation, no explosion.

    def explosion():
        print("test")
        image(img, rand_start, 350)
    

    The explosion function, bringing up the image of fire.

    So, when does the boolean of setExplosion = 0 or 1 come in? I am glad this works, but trying to understand HOW it is accomplishing the time delay is throwing me off. Where does the delay actually occur?

Sign In or Register to comment.