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

2021/12/23

lag in wheel of fortune class

Roshan123 Roshan123

2021/12/23

#
Plz tell me why it's lagging on website??? Which all lines am i supposed to remove/modify??
import greenfoot.*;
public class Fortune extends Actor
{
    byte turn1=105;//red enforcer
    byte turn2=96;//cyan health kit
    byte turn3=104;//blue shield
    byte turn4=102;//orange potion
    byte turn5=101;//purple bonus point
    byte turn6=99;//green jack pot
    byte mod=0;
    short s[]={0,1,2,3,4,5};
    int rand;
    Shop chances=new Shop(" COST: 10 \n SPINS LEFT: 1 ", 0,255,255,50);
    Shop chances2=new Shop(" COST: 10 \n SPINS LEFT: 0 ", 0,255,255,50);
    Shop clearBag=new Shop(" CLEAR UR BAG STORAGE \n TO SPIN ", 0,255,255,50);
    Shop gotEnforcer=new Shop(" YOU GOT A ENFORCER ", 0,255,255,50);
    Shop gotHealth=new Shop(" YOU GOT A HEALTH KIT ", 0,255,255,50);
    Shop gotShield=new Shop(" YOU GOT A SHIELD ", 0,255,255,50);
    Shop gotPotion=new Shop(" YOU GOT A POTION ", 0,255,255,50);
    Shop gotScore=new Shop(" YOU GOT BONUS POINT ", 0,255,255,50);
    Shop shopJackPot=new Shop(" CONGRATULATION !!! \n YOU WON 100 COINS ", 0,255,255,50);
    public void Fortune()
    {
        //getImage().scale(50, 50);
    }
    public void act() 
    {
       MyWorld w = (MyWorld) getWorld();
       
       if(getWorld()!=null && getWorld().getObjects(CounterShop.class).size()>0)
       {
         CounterShop counterShop = getWorld().getObjects(CounterShop.class).get(0);
         if(Greenfoot.mouseClicked(chances)  && counterShop.score>=10)
         {
             getImage().setTransparency(90);
             Greenfoot.delay(5);
             getImage().setTransparency(255);
         }
         if(w.click3<=1 || w.click2<=4 || w.click4<=1 
         || w.click5<=1  || w.click1<=4)
         {
             if(Greenfoot.mouseClicked(chances) && counterShop.score>=1 && w.g==0 ) 
             {
               w.g+=1;
               rand=Greenfoot.getRandomNumber(s.length);
               //rand=4;
               UserInfo myData2 = UserInfo.getMyInfo();
               if (myData2 != null)
               {
                   counterShop.score-=10;
                   myData2.setInt(5,counterShop.score);
                   myData2.store();
               }
               getWorld().removeObject(counterShop);
               getWorld().addObject(new CounterShop(), getWorld().getWidth()/2-20, 18);
             }
         }
       }
       
       if(rand==0 && w.click3>1)
       {
           w.removeObject(w.shopEnforcer);
           Shop shopEnforcer=new Shop(" BAG IS FULL ", 255,225,0,50);
           w.addObject(shopEnforcer, 400, 450);//orange
       }else if(rand==1 && w.click2>4)
       {
            w.removeObject(w.shopHealth);
            Shop shopHealth=new Shop(" BAG IS FULL ", 255,0,255,70);
            w.addObject(shopHealth, 245, 450);//purple
       }else if(rand==2 && w.click4>1)
       {
           w.removeObject(w.shopShield);
           Shop shopShield=new Shop(" BAG IS FULL ", 86,12,255,60);
           w.addObject(shopShield, 550, 450);//blue
       }else if(rand==3 && w.click5>1)
       {
            w.removeObject(w.shopPotion);
            Shop shopPotion=new Shop(" BAG IS FULL ", 255,0,0,60);
            w.addObject(shopPotion, 695, 450);//red
       }else if(rand==4 && w.click1>4)
       {
           w.removeObject(w.shopScore);
           Shop shopScore=new Shop(" BAG IS FULL ", 0,255,255,50);
           w.addObject(shopScore, 95, 450);//cyan
       }
       
       if(w.g==1)
       {
           if(rand==0 && w.click3<=1)
           {
               if(++mod%4==0)
               {
                 turn(--turn1);
               }
               if(turn1<=1)
               {
                   turn1=1;
                   w.click3++;
                   w.removeObject(w.shopEnforcer);
                   getWorld().addObject(gotEnforcer, getX(), getY()+100);
                   Shop shopEnforcer=new Shop(" ENFORCER "+"\n COST: 15 \n LEFT:"+w.available3+"\n BAG STORAGE: "+w.click3+"/2 ", 255,225,0,50);
                   w.addObject(shopEnforcer, 400, 450);//orange
                   w.g++;
               }
           } 
           
           if(rand==1 && w.click2<=4)
           {
               if(++mod%4==0)
               {
                 turn(--turn2);
               }
               if(turn2<=1)
               {
                   turn2=1;
                   w.click2++;
                   w.removeObject(w.shopHealth);
                   getWorld().addObject(gotHealth, getX(), getY()+100);
                   Shop shopHealth=new Shop(" HEALTH KIT "+"\n COST: 3 \n LEFT:"+w.available2+"\n BAG STORAGE: "+w.click2+"/5 ", 255,0,255,70);
                   w.addObject(shopHealth, 245, 450);//purple
                   w.g++;
               }
           }
           
           if(rand==2 && w.click4<=1)
           {
               if(++mod%4==0)
               {
                 turn(--turn3);
               }
               if(turn3<=1)
               {
                   turn3=1;
                   w.click4++;
                   w.removeObject(w.shopShield);
                   getWorld().addObject(gotShield, getX(), getY()+100);
                   Shop shopShield=new Shop(" SHILED "+"\n COST: 10 \n LEFT:"+w.available4+"\n BAG STORAGE: "+w.click4+"/2 ", 86,12,255,60);
                   w.addObject(shopShield, 550, 450);//blue
                   w.g++;
               }
           }
           
           if(rand==3 && w.click5<=1)
           {
               if(++mod%4==0)
               {
                 turn(--turn4);
               }
               if(turn4<=1)
               {
                   turn4=1;
                   w.click5++;
                   w.removeObject(w.shopPotion);
                   getWorld().addObject(gotPotion, getX(), getY()+100);//cyan
                   Shop shopPotion=new Shop(" POTION "+"\n COST: 15 \n LEFT:"+w.available5+"\n BAG STORAGE: "+w.click5+"/2 ", 255,0,0,60);
                   w.addObject(shopPotion, 695, 450);//red
                   w.g++;
               }
           }
           
           if(rand==4 && w.click1<=4)
           {
               if(++mod%4==0)
               {
                 turn(--turn5);
               }
               if(turn5<=1)
               {
                   turn5=1;
                   w.click1++;
                   w.removeObject(w.shopScore);
                   getWorld().addObject(gotScore, getX(), getY()+100);//cyan
                   Shop shopScore=new Shop(" BONUS POINT "+"\n COST: 5 \n LEFT:"+w.available1+"\n BAG STORAGE: "+w.click1+"/5 ", 0,255,255,50);
                   w.addObject(shopScore, 95, 450);//red
                   w.g++;
               }
           }
           
           if(rand==5)
           {
               if(++mod%4==0)
               {
                 turn(--turn6);
               }
               if(turn6<=1 )
               {
                   turn6=1;
                   if(getWorld()!=null && getWorld().getObjects(CounterShop.class).size()>0)
                   {
                      CounterShop counterShop2 = getWorld().getObjects(CounterShop.class).get(0);
                      UserInfo myData = UserInfo.getMyInfo();
                      if (myData != null) 
                      {
                         counterShop2.score+=100;
                         myData.setInt(5,counterShop2.score);
                         myData.store();
                      }
                      getWorld().removeObject(counterShop2);
                      getWorld().addObject(shopJackPot, getX(), getY()+105);//cyan
                      w.g++;
                   }
                   getWorld().addObject(new CounterShop(), getWorld().getWidth()/2-20, 18);
               }
           }
           
           //getWorld().showText("red "+turn1+"\ncyan "+turn2+"\nbllue "+turn3+"\norange "+turn4+
           //"\npurp "+turn5+"\ngreen "+turn6+"\n rand"+rand+"\n click"+w.g, 222, 222);
       }
       
       if(w.click3<=1 || w.click2<=4 || w.click4<=1 
       || w.click5<=1  || w.click1<=4)
       {
         if(w.g>0) 
         {
             getWorld().removeObject(chances);
             getWorld().removeObject(clearBag);
             getWorld().addObject(chances2, getWorld().getWidth()/2-20, 70);  
         }else if(w.g==0) 
         {
             getWorld().removeObject(chances2);
             getWorld().removeObject(clearBag);
             getWorld().addObject(chances, getWorld().getWidth()/2-20, 70);
         }
       }
       if(w.click3>1 || w.click2>4 || w.click4>1 
       || w.click5>1  || w.click1>4)
       {
          getWorld().removeObject(chances);
           getWorld().removeObject(chances2);
          getWorld().addObject(clearBag, getWorld().getWidth()/2-20, 70);
       }
    }
}
danpost danpost

2021/12/23

#
Roshan123 wrote...
Plz tell me why it's lagging on website??? Which all lines am i supposed to remove/modify? << Code Omitted >>
Which scenario is this from?
Roshan123 Roshan123

2021/12/23

#
danpost danpost

2021/12/23

#
Roshan123 wrote...
<< Link Omitted >>
Oh :( that one.
Roshan123 Roshan123

2021/12/24

#
danpost wrote...
Oh :( that one.
Since it's too bulky, let it be. Just tell me how do manage to make ur every program reusable? And also tell me what are the things u hate in my programming skills?
danpost danpost

2021/12/24

#
Roshan123 wrote...
tell me how do manage to make ur every program reusable?
By keeping all behaviors and states of an object type in their respective classes. Let the world control the game/scenario without getting involved with any actors not specifically created for the world to control. On the other hand, do not have actors try to control the overall running of the game. Interactions between two type actors should be coded only in one of the actor's classes -- preferably, the more "major" one of the two types.
what are the things u hate in my programming skills?
(1) Over refactoring. One line methods should be avoided if they are only called from within the class and only called once or twice. Longer methods that are only called once should be avoided unless it makes the method they are called from too bulky. Another consideration in refactoring here is when local field values are needed in the called method, as well as what value(s) might be needed returned from the method. (2) Organization. Oftentimes, the way you organize things and what fields you declare can drastically reduce the amount of code used to do things. Arrays can often be used to help reduce code. Not acting on specifics, but collecting all related data prior to acting on the collection can reduce code. Performing actions in a timely manner -- do so at the time they are determined they are needed to be done can reduce and simplify code. (3) Lack of documentation. By adding comments and method headers, anyone looking at your code would find it a lot easier to follow what may be going on. (4) Multiple class with similar (not quite the same, though) actors. There are multiple sets of classes that could be combined, if set up to do so. There are also cases where you use two actors where one would do the job.
You need to login to post a reply.