This site requires JavaScript, please enable it in your browser!
Greenfoot back
Sn3aKyGuY
Sn3aKyGuY wrote ...

2014/9/26

Code Cleanup

Sn3aKyGuY Sn3aKyGuY

2014/9/26

#
So, in my scenario Adventures of Crabby (http://www.greenfoot.org/scenarios/12203) I have implemented a wave system. Every 100 points a new lobster is spawned to up the dificulty. The issue is, and it's not really an issue other than my OCD, I have 5 miles of code for this wave system. Because the lobsters can eat the worms and take 5 points from the player score I have to have two sets of instructions for every wave. If score == 100 then ... and if score == 105 then ... What would be the cleanest way to shorten this all up?
        //Wave 1
        if (theScore.getScore() == 0)
        {

            {
                if (getObjects(Lobster.class).size() < 1)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        //Wave 2
        if (theScore.getScore() == 100)
        {
            {
                if (getObjects(Lobster.class).size() < 2)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }

        if (theScore.getScore() == 105)
        {
            {
                if (getObjects(Lobster.class).size() < 2)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        //Wave 3
        if (theScore.getScore() == 200)
        {

            {
                if (getObjects(Lobster.class).size() < 3)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        
        if (theScore.getScore() == 205)
        {
            {
                if (getObjects(Lobster.class).size() < 3)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        //Wave 4
        if (theScore.getScore() == 300)
        {

            {
                if (getObjects(Lobster.class).size() < 4)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        
        if (theScore.getScore() == 305)
        {
            {
                if (getObjects(Lobster.class).size() < 4)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        //Wave 5
        if (theScore.getScore() == 400)
        {

            {
                if (getObjects(Lobster.class).size() < 5)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        
        if (theScore.getScore() == 405)
        {
            {
                if (getObjects(Lobster.class).size() < 5)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        //Wave 6
        if (theScore.getScore() == 500)
        {

            {
                if (getObjects(Lobster.class).size() < 6)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        
        if (theScore.getScore() == 505)
        {
            {
                if (getObjects(Lobster.class).size() < 6)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        //Wave 7
        if (theScore.getScore() == 600)
        {

            {
                if (getObjects(Lobster.class).size() < 7)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        
        if (theScore.getScore() == 605)
        {
            {
                if (getObjects(Lobster.class).size() < 7)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        //Wave 8
        if (theScore.getScore() == 700)
        {

            {
                if (getObjects(Lobster.class).size() < 8)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        
        if (theScore.getScore() == 705)
        {
            {
                if (getObjects(Lobster.class).size() < 8)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        //Wave 9
        if (theScore.getScore() == 800)
        {

            {
                if (getObjects(Lobster.class).size() < 9)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        
        if (theScore.getScore() == 805)
        {
            {
                if (getObjects(Lobster.class).size() < 9)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        //Wave 10
        if (theScore.getScore() == 900)
        {

            {
                if (getObjects(Lobster.class).size() < 10)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        
        if (theScore.getScore() == 905)
        {
            {
                if (getObjects(Lobster.class).size() < 10)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        //Wave 11
        if (theScore.getScore() == 1000)
        {

            {
                if (getObjects(Lobster.class).size() < 11)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        
        if (theScore.getScore() == 1005)
        {
            {
                if (getObjects(Lobster.class).size() < 11)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        //Wave 12
        if (theScore.getScore() == 1100)
        {

            {
                if (getObjects(Lobster.class).size() < 12)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        
        if (theScore.getScore() == 1105)
        {
            {
                if (getObjects(Lobster.class).size() < 12)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        //Wave 13
        if (theScore.getScore() == 1200)
        {

            {
                if (getObjects(Lobster.class).size() < 13)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        
        if (theScore.getScore() == 1205)
        {
            {
                if (getObjects(Lobster.class).size() < 13)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        //Wave 14
        if (theScore.getScore() == 1300)
        {

            {
                if (getObjects(Lobster.class).size() < 14)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        
        if (theScore.getScore() == 1305)
        {
            {
                if (getObjects(Lobster.class).size() < 14)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        //Wave 15
        if (theScore.getScore() == 1400)
        {

            {
                if (getObjects(Lobster.class).size() < 15)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
        
        if (theScore.getScore() == 1405)
        {
            {
                if (getObjects(Lobster.class).size() < 15)
                addObject(new Lobster(), randomXL, randomYL);
            }
        }
erdelf erdelf

2014/9/26

#
not quite sure if I understood what you are asking for, but if I got it right, this code here should work
if(getObjects(Lobster.class).size() < (Math.round(theScore.getScore() /100)+1))
{
   addObject(new Lobster(), randomXL, randomYL);
}
Sn3aKyGuY Sn3aKyGuY

2014/9/26

#
The easiest way to explain it would be, if score == 100 or 105 add second lobster then 200 or 2005 add third lobster then 300 or 305 add fourth lobster ... so on and so forth. It works fine now with the above code, I just feel there is a way to achieve the same thing without so much code. I could be wrong, I'm new to this and this is my first scenario.
erdelf erdelf

2014/9/26

#
oh, did you try mine ?
danpost danpost

2014/9/26

#
I think you did not understand that the code erdelf gave was to replace your entire code above. I think it is pretty short, although it could be a little shorter:
if (getObjects(Lobster.class).size() < 1+theScore.getScore()/100)
{
    addObject(new Lobster(), randomXL, randomYL);
}
or
if (getObjects(Lobster.class).size() < 1+theScore.getScore()/100) addObject(new Lobster(), randomXL, randomYL);
Sn3aKyGuY Sn3aKyGuY

2014/9/26

#
Sorry, posted last comment before trying that. That worked great, thank you so much!
Sn3aKyGuY Sn3aKyGuY

2014/9/26

#
So the other issue I'm having is getting the background .gif to change with the score. The issue is I want it to change at every 5/10 points within each wave. Example: if score == 0 display image1.gif if score == 5 or 10 display image2.gif if score == 15 or 20 display image3.gif so on and so forth till 100 at which point i want it to reset to image1.gif and start redisplaying the same images every 5/10 points. if score == 105 or 110 display image2.gif if score == 115 or 120 display image3.gif Sorry if that's confusing. I was hoping that the answer to my lobster spawning would work for this, but it doesn't.
Sn3aKyGuY Sn3aKyGuY

2014/9/26

#
Well that and I only want my .gif image to play one loop. When I designed it in Photoshop it's only supposed to play one time and not loop, but when put into Greenfoot in continuously loops the image.
Super_Hippo Super_Hippo

2014/9/26

#
Instead of having a .gif, you can use the images and make an array for every image. With this, you can play the animation once.
danpost danpost

2014/9/26

#
@Sn3aKyGuy, your last two posts seem to contradict each other. That is, it appears that you DO want your images to loop continuously in the 'Example' post; but, then in the next post, you say you want the images to play only one time and not loop. Maybe there is a distinction that needs to be make here -- that is, in the case that you are referring to two separate sets of gif images. At the end of the first post you wrote 'Sorry if that's confusing'. Well, it was not -- until you posted the next post.
Super_Hippo Super_Hippo

2014/9/26

#
I think there are 10 different .gif's with a set of images each. The images of each .gif are an animation (or whatever) which should play once when the score changed. The last image should be displayed until the score changed again. Then the next animation is played. And so on.
Sn3aKyGuY Sn3aKyGuY

2014/9/26

#
Super_Hippo is right, I have 10 .gif images. Each image has 6 frames that fill a progress bar in 2% increments until it reaches either 10%, 20%, 30% .... 100% at which point the 10th .gif animates the progress bar emptying back to 0%. I'm using this set of .gif images to animate the progress within each wave, so I want to go through each set of .gif images every 100 points. When the .gif image plays though, it plays over and over so the image looks like it fills to, for example, 10% then empties over and over until the next .gif is called. I want each individual .gif to play just once every time it is called then stop on it's final frame instead of playing all frames over and over. The .gif images are designed from Photoshop to only play once then stop on the last frame, but Greenfoot doesn't play it that way. I'm at work atm, but I can upload what I have so you can see what I mean when I get home tonight if that wasn't clear.
danpost danpost

2014/9/27

#
Sn3aKyGuY wrote...
but Greenfoot doesn't play it that way.
I am not sure what you mean by this. Greenfoot does not 'play' gif images in itself. The correct Java code, such as that which the 'GIFActor' class contains can be used to run gif image animation. If you have downloaded this class and are using it, there are 'pause' and 'resume' methods in the class, as well as a 'setImage(String)' method you can use to change the gif file used. All you then need is an int field to count the frames as one of the animations runs.
You need to login to post a reply.