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

2016/5/17

Cars scenario

Trunks Trunks

2016/5/17

#
Hi, I don't understand why GreenFoot does not recognize the " Direction " class, maybe someone can help me to sort this out :) This game is quiet simple, you have a car that you can control and you just have to avoid the other cars ( if a car touch yours, you explode and you loose the game ) Some parts of the codes may be in french, so if you need some explanation just hook me up. Thank's for reading this. I have got 3 actors, a Wombat ( which is my car ), Ennemi ( enemies' cars ) and Explosion. Wombat :

import greenfoot.*;  
import java.util.List;

public class Wombat extends Actor
{
    protected static final Direction DROITE = Direction.DROITE;
    protected static final Direction GAUCHE = Direction.GAUCHE;
    protected static final Direction HAUT = Direction.HAUT;
    protected static final Direction BAS = Direction.BAS;
    protected int maxY;
    protected int maxX;
    
    protected void addedToWorld(World world)
    {
        maxY = getWorld().getHeight();
        maxX = getWorld().getWidth();  
    }
    public void seDeplacer(Direction direction)
    {   
        if(direction == HAUT){
            setLocation(getX(), (maxY + getY()-1) % maxY);
        }
        if(direction == BAS){
            setLocation(getX(), (maxY + getY()+1) % maxY);
        }
        if(direction == DROITE){
            setLocation((maxX + getX()+1) % maxX, getY());
        }
        if(direction == GAUCHE){
            setLocation((maxX + getX()-1) % maxX, getY());
        }         
    }
        public void exploser()
        {
        Explosion explosion = new Explosion();
        getWorld().addObject(new Explosion(), getX(), getY());
        getWorld().removeObject(this);
        explosion.boom();
    }
    public void act() 
    {
        seDeplacer(HAUT);
        seDeplacer(HAUT);
        if(Greenfoot.isKeyDown("up")){
            seDeplacer(HAUT);
            seDeplacer(HAUT);
        }
        if(Greenfoot.isKeyDown("down")){            
            seDeplacer(BAS);
            seDeplacer(BAS);        
        }
        if(Greenfoot.isKeyDown("right")){            
            seDeplacer(DROITE);
            seDeplacer(DROITE);           
        }
        if(Greenfoot.isKeyDown("left")){            
            seDeplacer(GAUCHE);
            seDeplacer(GAUCHE);            
        }       
    }
    
}

Enemies' car

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class Voiture2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ennemi extends Actor
{
    protected static final Direction DROITE = Direction.DROITE;
    protected static final Direction GAUCHE = Direction.GAUCHE;
    protected static final Direction HAUT = Direction.HAUT;
    protected static final Direction BAS = Direction.BAS;
    
    protected int maxY;
    protected int maxX; 
    
    protected void addedToWorld(World world){
        maxY = getWorld().getHeight();
        maxX = getWorld().getWidth();  
    }
    

    public void seDeplacer(Direction direction){   
        if(direction == HAUT){
            setLocation(getX(), (maxY + getY()-1) % maxY);
        }
        if(direction == BAS){
            setLocation(getX(), (maxY + getY()+1) % maxY);
        }
        if(direction == DROITE){
            setLocation((maxX + getX()+1) % maxX, getY());
        }
        if(direction == GAUCHE){
            setLocation((maxX + getX()-1) % maxX, getY());
        }         
    }
        public void exploser(){
        Explosion explosion = new Explosion();
        getWorld().addObject(new Explosion(), getX(), getY());
        getWorld().removeObject(this);
        explosion.boom();
    }
    /**
     * Act - do whatever the Wombat wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        List cibles = getObjectsInRange(30, Wombat.class);
        for(Object wombat : cibles){
          ((Wombat)wombat).exploser();
      }
    }
}    


Explosion

import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

import java.util.*;

/**
 * An explosion. It starts by expanding and then collapsing. 
 * The explosion will explode other obejcts that the explosion intersects.
 * 
 * @author Poul Henriksen
 * @version 1.0.1
 */
public class Explosion extends Actor
{
    /** How many images should be used in the animation of the explostion */
    private final static int IMAGE_COUNT= 8;
    
    /** 
     * The images in the explosion. This is static so the images are not
     * recreated for every object (improves performance significantly).
     */
    private static GreenfootImage[] images;
    
    /** Current size of the explosion */
    private int size=0;
    
    /** How much do we increment the index in the explosion animation. */
    private int increment=1;    
    
    public Explosion()
    {
        initialiseImages();
        setImage(images[0]);        
        Greenfoot.playSound("Explosion.wav");
    }    
    
    /** 
     * Create the images for explosion.
     */
    public synchronized static void initialiseImages()
    {
        if(images == null) {
            GreenfootImage baseImage = new GreenfootImage("explosion.png");
            int maxSize = baseImage.getWidth()*4;
            int delta = maxSize / (IMAGE_COUNT+1);
            int size = 0;
            images = new GreenfootImage[IMAGE_COUNT];
            for(int i=0; i < IMAGE_COUNT; i++) {
                size = size + delta;
                images[i] = new GreenfootImage(baseImage);
                images[i].scale(size, size);
            }
        }
    }
    
    /**
     * EXPLODE!
     */
    public void act()
    { 
        setImage(images[size]);

        size += increment;
        if(size>=IMAGE_COUNT) {
            increment = -increment;
            size += increment;
        }
        
        //explodeOthers();
        if(size <= 0) {
            getWorld().removeObject(this);
        }
    }
    
    /**
     * Explodes all intersecting objects.
     */
    
    private void explodeOthers() 
    {        
        List explodeEm = getIntersectingObjects(null);
        Iterator i = explodeEm.iterator();
        while(i.hasNext()) {
            Actor a = (Actor) i.next();
            if( (a instanceof Wombat)) { //Don't explode other explosions
               /* 
                int x = a.getX();
                int y = a.getY();
                //Replace other object with an explosion
                getWorld().removeObject(a);
                getWorld().addObject(new Explosion(), x, y);
               */ 
               ((Wombat)a).exploser();
            }
        }
    }
    
    public void boom(){
        act();
    }
}

Super_Hippo Super_Hippo

2016/5/17

#
I don't see you created a 'Direction' class, so you can't use it as a variable type.
davmac davmac

2016/5/17

#
Your code refers to a Direction class, but I don't see it defined. Maybe that's the problem?
Trunks Trunks

2016/5/18

#
Hi everyone, I just tried to create a " class direction", but it doesn't work...
Super_Hippo Super_Hippo

2016/5/18

#
I think what you want to do is:
public void seDeplacer(String direction)
{   
    if("HAUT".equals(direction)) setLocation(getX(), (maxY + getY()-1) % maxY);
    if("BAS".equals(direction)) setLocation(getX(), (maxY + getY()+1) % maxY);
    if("DROITE".direction)) setLocation((maxX + getX()+1) % maxX, getY());
    if("GAUCHE".direction)) setLocation((maxX + getX()-1) % maxX, getY());
}
Then you call the method for example like this:
seDeplacer("HAUT");
And you can remove all direction variables.
davmac davmac

2016/5/18

#
Hi everyone, I just tried to create a " class direction", but it doesn't work...
You would need to provide much more information before we could help you, including showing the code you tried and saying what error message you received. If you copied your Wombat class from another scenario, you should be able to copy the Direction class from the same scenario.
Trunks Trunks

2016/5/18

#
Awesome !! Thank you Super_Hippo, but now the car you can control ( wombat ) does not explode anymore ...
Trunks Trunks

2016/5/18

#
The code i'm showing to you is completely working on another scenario, I just tried to take it and to remaster it but it does not work as good as expected
Trunks Trunks

2016/5/20

#
Up !
davmac davmac

2016/5/20

#
I can't see any obvious reason why it wouldn't work. Maybe upload your scenario so we can take a proper look at it.
Trunks Trunks

2016/5/20

#
Thank you for all your replies, I have made one of the stupidest mistake of my life, it was just a problem with a name, GreenFoot didn't recognize "Explosion.png" because the file's name was " Explode.png" hahaha !
You need to login to post a reply.