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

2020/2/26

Collision and removal between two actors when size is larger than the other

tudibk tudibk

2020/2/26

#
Hi. Im making a little game where you eat yourself big against your enemy player (its a 2p game). To win over your enemy, you have to be bigger than the enemy and collide with the enemy (kinda like agario). I just cant seem to remove the enemy from the world when a collision with greater size happens. I'll attach both the player and player2 code, hope one of you geniusses can help a fellow programmer out :D Oh, just realized all my comments is in danish, bear with me ;)
import greenfoot.*;

public class Blob extends Actor
{   
 
    //Koordinat ints
    private int x;
    private int y; 
    
    //Størrelses int, opdateres i koden når blobFood bliver spist
    public int size = 30;
    
    //Ints anvendt til at holde styr på antal blobFoods i spillet
    private int blobIndex = 0;
    private int maxBlobs = 30;
    
    //Billede initalizers 
    private GreenfootImage baseImage;
    private Color randomColor = new Color(Greenfoot.getRandomNumber(255), Greenfoot.getRandomNumber(255), Greenfoot.getRandomNumber(255), 255);
    private GreenfootImage img = new GreenfootImage(size, size);
    
    //BlobFood array, brugt til at lave et bestemt antal blobFoods
    private BlobFood[] blobFood = new BlobFood[maxBlobs];
    
    
    //Blob konstruktor, anvendt til billede indstillinger
    public Blob(){
        
        baseImage = new GreenfootImage(size, size);
        
        //Sæt farven til initial, en variabel der i starten af spillet bliver sat til en tilfældig RGB værdi med en gennemsigtighed på 255
        baseImage.setColor(randomColor);
        
        //Metode brugt til at lave en cirkel med størrelsen 'size'
        baseImage.fillOval(0, 0,size,size);  
    }
    
    //Metode til at finde et tilfældigt tal mellem min og max
    public int getRandomNumberBorder(int min,int max){
       
        int number = Greenfoot.getRandomNumber(max-min+1);
       return number+min;
    }
    
    //Metode anvendt i blobworld, til for loop
    public int getMaxBlobs(){
      
        return maxBlobs;
    }
    
    //Metode til at returnere Blob størrelse
    public int getSizeBlob(){
    
        return size; 
    }
    
    //Metode til at tilføje en blobFood et tilfædigt sted mellem x(200-800) og y(0-700)
    public void addBlob(){
        
        //Hvis antallet af max blobFood ikke er blevet overskrevet, tilføj blobfood
        if(maxBlobs > blobIndex){
         
         //initaliser nyt blobFood fra array med positionen blobIndex 
         blobFood[blobIndex] = new BlobFood(); 
         
         //tilføj blobFood[blobIndex] til den nuværende verden med et billede fra setShape metoden
         getWorld().addObject(blobFood[blobIndex],getRandomNumberBorder(200, 800),Greenfoot.getRandomNumber(700));
         blobFood[blobIndex].setImage(blobFood[blobIndex].setShape());
         
         //Tilføj 1 til blobIndex int'en
         blobIndex += 1;
        }
    }
    
    //metode til at returnere et billede med tilfældig farve
    public GreenfootImage setShape(){
     
        img.setColor(randomColor);
        
        img.fillOval(0, 0,size,size);  
        
        return img;
    }
    
    //Metoden til at se om man har kollideret med en blobFood og derfor 'spiser' den
    public void eat(){
  
            //Hvis afstanden mellem Blob og et object af BlobFood klassen er inden for den variable størrelse size
            if(getObjectsInRange(size, BlobFood.class).size() > 0)
            {   
              
              //Fjern det kolliderede BlobFood objekt fra klassen
              getWorld().removeObjects(getObjectsInRange(size, BlobFood.class));
            
              //Forøg variablen size
              size += 2;
              
              //Skaler cirklen til en ny størrelse
              GreenfootImage image = new GreenfootImage(baseImage);
              image.scale(size, size);
              setImage(image);
            
         
            }
            
                                              
    }
    
    //Bevægelses metode 
    public void movement(){
        
        //Skaf objektets nuværende koordinater
        x = getX();
        y = getY();
        
        //Nedenstående if statements checker om man har trykket på en tast og rykker de nuværende koordinater med 1, hvis knappen er trykket
        if (Greenfoot.isKeyDown("left")){
        
            x -= 1;
            setLocation(x,y);
            
        }
        
        if (Greenfoot.isKeyDown("right")){
        
            x += 1;
            setLocation(x,y);
            
        }
        
         if (Greenfoot.isKeyDown("up")){
         
            y -= 1;
            setLocation(x,y);
            
            
        }
        
        if (Greenfoot.isKeyDown("down")){
        
            y += 1;
            setLocation(x,y);
            
        }
    }
    
    //Act metoden der køres konstant af Greenfoot det her kører movement og eat metoderne og andet kode
    public void act() 
    {
        
        movement();
        eat();
        
        //for loop int
        int i;
   
        //Hvis mængden af blobFood i verden kommer under 10, skal der indsættes en ny mængde af blobFood med addBlob metoden
        if(getWorld().getObjects(BlobFood.class).size() < 10){
                
           blobIndex = 0; 
                
           for(i = 0; i < maxBlobs; i++){
               
               addBlob();
            
           }
        }
        
        i = 0;
        
        if(getWorld().getObjects(Blob2.class).get(0).size < size){
            
           getWorld().removeObjects(getObjectsInRange(size, Blob2.class));
        } 
    }    
}
import greenfoot.*;

public class Blob2 extends Actor
{   
 
    //Koordinat ints
    private int x;
    private int y; 
    
    //Størrelses int, opdateres i koden når blobFood bliver spist
    public int size = 30;
    
    //Billede initalizers 
    private GreenfootImage baseImage;
    private Color randomColor = new Color(Greenfoot.getRandomNumber(255), Greenfoot.getRandomNumber(255), Greenfoot.getRandomNumber(255), 255);
    private GreenfootImage img = new GreenfootImage(size, size);
    
    //Blob2 konstruktor, anvendt til billede indstillinger
    public Blob2(){
        
        baseImage = new GreenfootImage(size, size);
        
        //Sæt farven til initial, en variabel der i starten af spillet bliver sat til en tilfældig RGB værdi med en gennemsigtighed på 255
        baseImage.setColor(randomColor);
        
        //Metode brugt til at lave en cirkel med størrelsen 'size'
        baseImage.fillOval(0, 0,size,size);  
    }
    
    //Metode til at finde et tilfældigt tal mellem min og max
    public int getRandomNumberBorder(int min,int max){
       
       int number = Greenfoot.getRandomNumber(max-min+1);
       return number+min;
    }    
    
    //Metode til at returnere Blob størrelse
    public int getSizeBlob2(){
    
        return size; 
    }
    
    //metode til at returnere et billede med tilfældig farve
    public GreenfootImage setShape(){
     
        img.setColor(randomColor);
        
        img.fillOval(0, 0,size,size);  
        
        return img;
    }
    
    //Metoden til at se om man har kollideret med en blobFood og derfor 'spiser' den
    public void eat(){
  
            //Hvis afstanden mellem Blob og et object af BlobFood klassen er inden for den variable størrelse size
            if(getObjectsInRange(size, BlobFood.class).size() > 0)
            {   
              
              //Fjern det kollidere BlobFood objekt fra klassen
              getWorld().removeObjects(getObjectsInRange(size, BlobFood.class));
            
              //Forøg variablen size
              size += 2;
              
              //Skaler cirklen til en ny størrelse
              GreenfootImage image = new GreenfootImage(baseImage);
              image.scale(size, size);
              setImage(image);
            
         
            }
                                              
    }
    
    //Bevægelses metode 
    public void movement(){
        
        //Skaf objektets nuværende koordinater
        x = getX();
        y = getY();
        
        //Nedenstående if statements checker om man har trykket på en tast og rykker de nuværende koordinater med 1, hvis knappen er trykket
        if (Greenfoot.isKeyDown("a")){
        
            x -= 1;
            setLocation(x,y);
            
        }
        
        if (Greenfoot.isKeyDown("d")){
        
            x += 1;
            setLocation(x,y);
            
        }
        
         if (Greenfoot.isKeyDown("w")){
         
            y -= 1;
            setLocation(x,y);
            
            
        }
        
        if (Greenfoot.isKeyDown("s")){
        
            y += 1;
            setLocation(x,y);
            
        }
    }
    
    //Act metoden der køres konstant af Greenfoot det her kører movement og eat metoderne
    public void act() 
    {
        
        movement();
        eat();       
    }    
}
danpost danpost

2020/2/27

#
Line 171 is written improperly. The "size' field for the Blob2 object is not in the Object claas -- it is in the Blob2 class. So, the compiler needs to know where to look for that field. Cast Blob2 as the type of object gotten from the List object returned via getObjects. Also, you need to ensure that the returned list is not empty before you can try to get an object from the list.
tudibk tudibk

2020/2/27

#
I kinda figured it out (Almost the same code as i posted earlier). now i just get an error message when the action happens: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:458) at Blob.act(Blob.java:177) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) This is my current little strip of code that handles it:
int blobSize = ((Blob2)getWorld().getObjects(Blob2.class).get(0)).size;

        
        if(blobSize < size){
            
           getWorld().removeObjects(getObjectsInRange(size, Blob2.class));
        }
If you know what's wrong, please give me an example as im quite new to Java and Greenfoot. Thanks a lot in advance :D
danpost danpost

2020/2/27

#
tudibk wrote...
Im new to Java and Greenfoot, really gratefull for your posts, but can you give an example?
Let us say you got a non-empty list of Blob2 objects:
java.util.List blobs = getWorld().getObjects(Blob2.class);
You can then get the size of the first Blob2 object in the list with
int blob2size = ((Blob2)blobs.get(0)).size;
Note how I indicated that the object gotten from the list is a Blob2 object.
danpost danpost

2020/2/27

#
danpost wrote...
you need to ensure that the returned list is not empty before you can try to get an object from the list.
tudibk tudibk

2020/2/28

#
So i have implemented your bit of code now. It still says that range 0 is out of bounds for 0, and i know it's because the list is empty. What i wonder is, that your code puts all Blob2 objects in the world into a list. As i start my world with a Blob2 object, why does the list return a null?
tudibk tudibk

2020/2/28

#
Ive tried som different things and i have found the problem, yet to find the soloution. When i remove the Blob2 object, the code that looks for the size: blob2size = ((Blob2)blobs.get(0)).size; terminates the program, because the Blob 2 object is gone. I have tried this, with no luck:
if(getWorld().getObjects(Blob2.class) != null){
          blob2size = ((Blob2)blobs.get(0)).size;
        }
Any ideas?
danpost danpost

2020/2/28

#
tudibk wrote...
Ive tried som different things and i have found the problem, yet to find the soloution. When i remove the Blob2 object, the code that looks for the size: blob2size = ((Blob2)blobs.get(0)).size; terminates the program, because the Blob 2 object is gone. I have tried this, with no luck: << Code Omitted >> Any ideas?
There will always be a list. A null return value is not the problem (it will never be null). However, an empty list is a possibility -- and that is the issue..
danpost danpost

2020/2/28

#
Line 2 looks terrible. If you are trying to check the size of the list, then why would by try to get a specific element from the list? If blobs is the list, then blobs.size() is its size and blobs.isEmpty() is the state of the list being empty (a true/false value).
tudibk tudibk

2020/2/29

#
I dont think we are on the same page here :D the .size is a size variable from the Blob2 object (you can see it in the original posted code) i am comparing the size int of the Blob2 object with the Blob size integer.
danpost danpost

2020/3/1

#
tudibk wrote...
I dont think we are on the same page here :D the .size is a size variable from the Blob2 object (you can see it in the original posted code) i am comparing the size int of the Blob2 object with the Blob size integer.
Still, what I wrote in the previous post holds and what I wrote in the last one should help as far as how to change things.
tudibk tudibk

2020/3/3

#
Got it fixed, thanks a lot danpost :)
You need to login to post a reply.