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

2021/6/15

I got a problem with a Timer

Turbo_Thorsten Turbo_Thorsten

2021/6/15

#
So below are my classes/world. World class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    int wave = 1;
    int d;
    int f;
    int g;
    double h;
    static int highscore = 0;
    int i;
    int j;
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(700, 700, 1); 
        Player player = new Player ();
        addObject(player, 350, 350);
        Virus bacteria1 = new Virus ();
        addObject(bacteria1, 670, 670);
        Virus bacteria2 = new Virus ();
        addObject(bacteria2, 30, 30);
        Bacteria bacteria3 = new Bacteria ();
        addObject(bacteria3, 30, 670);
        Bacteria bacteria4 = new Bacteria ();
        addObject(bacteria4, 670, 30);
        Player.bulletsshot = 0;
        Bullet.enemieskilled = 0;
        Border border1 = new Border ();
        addObject(border1, 175, 100);
        Border border2 = new Border ();
        addObject(border2, 115, 165);
        border2.setRotation(270);
        Border border3 = new Border ();
        addObject(border3, 525, 100);
        Border border4 = new Border ();
        addObject(border4, 584, 165);
        border4.setRotation(90);
        Border border5 = new Border ();
        addObject(border5, 175, 600);
        Border border6 = new Border ();
        addObject(border6, 115, 539);
        border6.setRotation(270);
        Border border7 = new Border ();
        addObject(border7, 525, 600);
        Border border8 = new Border ();
        addObject(border8, 584, 539);
        border8.setRotation(90);
        Virus.freeze1 = false;
        Virus.timerB = 0;
        Bacteria.freeze2 = false;
        Bacteria.timerC = 0;
    }
    public void act ()
    {
       d =getObjects(Bacteria.class).size() + getObjects(Virus.class).size();
       showD();
       g = wave -1;
       h = Math.round((Bullet.enemieskilled / Player.bulletsshot)*100.0)/100.0;
       if (getObjects(Player.class).size() == 0)
       {
           gameOver();
        }
        else
        {
        newWave();
        showWave();
    }
    }
    
    public void showD()
    {
        showText("Enemies left: " + d, 615, 50);
    }
    private void showWave()
    {
        showText("Wave " + wave, 650, 25);
        showText("VirustimerC " + Bacteria.timerC, 600, 75);
        showText("VirustimerB " + Virus.timerB, 600, 125);
    }
    
    public void newWave () 
    {
    if (d == 0)
    {
     wave++;
     Virus.freeze1 = false;
     Virus.timerB = 0;
     Bacteria.freeze2 = false;
     Bacteria.timerC = 0;
     for (int e = wave;e != 0;e--)
      {
      f = wave*2; 
      if (Greenfoot.getRandomNumber(100) > f)
      {
      Bacteria bacteria1 = new Bacteria ();
      addObject(bacteria1, 670, 670);
      }
      else 
      {
      Virus virus1 = new Virus ();
      addObject(virus1, 670, 670);
      }
      if (Greenfoot.getRandomNumber(100) > f)
      {
      Bacteria bacteria2 = new Bacteria ();
      addObject(bacteria2, 30, 670);
      }
      else 
      {
      Virus virus2 = new Virus ();
      addObject(virus2, 30, 670);
      }
      if (Greenfoot.getRandomNumber(100) > f)
      {
     Bacteria bacteria3 = new Bacteria ();
     addObject(bacteria3, 670, 30);
      }
      else 
      {
      Virus virus3 = new Virus ();
      addObject(virus3, 670, 30);
      }
      if (Greenfoot.getRandomNumber(100) > f)
      {
       Bacteria bacteria4 = new Bacteria ();
      addObject(bacteria4, 30, 30);
      }
      else 
      {
      Virus virus4 = new Virus ();
      addObject(virus4, 30, 30);
      }
      }
    }
    }
    public void gameOver()
    {
        if (wave - 1> highscore)
       {
        highscore = wave - 1;
       }
       i = (int) Bullet.enemieskilled;
       j = (int) Player.bulletsshot;
        GameOver gameover = new GameOver ();
        addObject(gameover, 350, 350);
        showText(" ", 650, 25);
        showText(" ", 615, 50);
        showText("Waves survived: " + g, 350, 270);
        showText("Enemies killed: " + i, 350, 310);
        showText("Bullets shot: " + j, 350, 350);
        showText("Accuracy: " + h, 350, 390);
        showText("Highscore: " + highscore, 350, 430);
        showText("(Highscore resets after restarting Greenfoot)", 350, 470);
        Greenfoot.stop();
    }
    
}
My Bullet class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Mover
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public static double enemieskilled;
    public static int b;
    int c;
    public void act() 
    {
        move (10.0);
        if (isTouching(Border.class))
        {
            removeTouching (Bacteria.class);  
         getWorld().removeObject(this);
        }
        else if (isTouching(Bacteria.class))
        {
            //getWorld().addObject(new Explosion(), getX(), getY());
         removeTouching (Bacteria.class);  
         getWorld().removeObject(this);
         enemieskilled++;
        }
        else if (isTouching(Virus.class))
        {
            //getWorld().addObject(new Explosion(), getX(), getY());
         removeTouching (Virus.class); 
         enemieskilled++;
         if(Greenfoot.getRandomNumber(100) < 100)
         {
             c = 2;
             switch(c)
             {
                 case 0:
                 nuke();
                 break;
                 case 1:
                 Player.fasterShooting();
                 break;
                 case 2:
                 Virus.freezeVirus();
                 Bacteria.freezeBacteria();
                }
         } 
         getWorld().removeObject(this);
        }
        else if (isAtEdge()) 
        {
         getWorld().removeObject(this);
    }
    }   
    public void nuke()
    {
    if (getWorld() != null)
        {
     getWorld().removeObjects(getWorld().getObjects(Bacteria.class));
     getWorld().removeObjects(getWorld().getObjects(Virus.class));
     nukeScreen();
    }
    }
    public void nukeScreen()
    {
        
    }
}
My Bacteria class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bacteria extends Actor
{
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int a;
    int b = 2;
    public static boolean freeze2 = false;
    public static int timerC;
    public void act() 
    {
        if (freeze2 == true)
        {
        timerC--;
        if(timerC == 0)
        {
        freeze2 = false;
        }
       }
        else
        {
        if( isAtEdge() ){
            turn(17);
        }
        if( isTouching(Border.class) ){
            turn(17);
        }
        if (Greenfoot.getRandomNumber(50) <3){
            a = Greenfoot.getRandomNumber(91)-45;
            turn(a);
        }
        
        move(b);
       }
        lookForPlayer();
    }  
    public void lookForPlayer() {
        
        if (isTouching(Player.class))
        {
         removeTouching (Player.class);  
        }
        
    }
    public static void freezeBacteria()
    {
        freeze2 = true;
        timerC = timerC + 200;
    }
}
And my Virus class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Virus extends Actor
{
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int a;
    int b = 3;
    public static boolean freeze1 = false;
    public static int timerB;
    public void act() 
    {
        if (freeze1 == true)
        {
        timerB--;
        if(timerB == 0)
        {
        freeze1 = false;
        }
       }
        else
        {
        if( isAtEdge() ){
            turn(17);
        }
        if( isTouching(Border.class) ){
            turn(17);
        }
        if (Greenfoot.getRandomNumber(50) <3){
            a = Greenfoot.getRandomNumber(91)-45;
            turn(a);
        }
       
        move(b);
    }
        lookForPlayer();
    }  
     public void lookForPlayer() {
        
        if (isTouching(Player.class))
        {
         removeTouching (Player.class);  
        }
        
    }
    public static void freezeVirus()
    {
        freeze1 = true;
        timerB = timerB + 200;
    }
}
Why is the Timer different in different scenarios?
danpost danpost

2021/6/15

#
Turbo_Thorsten wrote...
Why is the Timer different in different scenarios?
What do you mean? Different? How so? Also, are all bacteria supposed to freeze at the same time? and all viruses ... at the same time?
Turbo_Thorsten Turbo_Thorsten

2021/6/15

#
The virus and the bacterias should freeze at the same time for the same time but that isn't the case. The bacteria timer is sometimes faster than the virus timer and I don't get why.
danpost danpost

2021/6/15

#
Turbo_Thorsten wrote...
The virus and the bacterias should freeze at the same time for the same time but that isn't the case. The bacteria timer is sometimes faster than the virus timer and I don't get why.
For better control, remove timers from both Virus and Bacteria classes Put a timer in MyWorld class and control it from act method there. At beginning of act methods of the enemy classes, use:
if (MyWorld.freezeTimer > 0) return;
The boolean fields, freezeVirus and freezeBacteria, are not needed. There values will always be the result of the conditional expression in the if statement above.
Turbo_Thorsten Turbo_Thorsten

2021/6/15

#
danpost wrote...
Turbo_Thorsten wrote...
The virus and the bacterias should freeze at the same time for the same time but that isn't the case. The bacteria timer is sometimes faster than the virus timer and I don't get why.
For better control, remove timers from both Virus and Bacteria classes Put a timer in MyWorld class and control it from act method there. At beginning of act methods of the enemy classes, use:
if (MyWorld.freezeTimer > 0) return;
The boolean fields, freezeVirus and freezeBacteria, are not needed. There values will always be the result of the conditional expression in the if statement above.
Thank you soo much!! I updated and modified my code and now everything works! It's my first game and I'm learning so much from you
You need to login to post a reply.