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

2021/11/30

Hi, I am having trouble setting the image to rotate (be vertical) on the side walls (the vertical ones) to make a border with my obstacle class all the way around my world? I can't get it to work, please help.

SportingLife7 SportingLife7

2021/11/30

#
THIS IS MY SPACE CLASS:
public class Space extends World
{

    private String[] soundFiles =
        {"3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c", "4d","4e", "4f", "4g"};
    /**
     * Create space.
     */
    public Space()
    {    
        super(960, 620, 1);
        makeObstacles();
        randomBodies(5);
        // Uncomment one of the following method calls if you want the objects created automatically:
        //sunAndPlanet();
    }

    /**
     * Set up the universe with a sun and a planet.
     */
    public void sunAndPlanet()
    {
        removeAllObjects();
        addObject (new Body (50, 240.0, new Vector(270, 0.03), new Color(255, 216, 0)), 460, 270);
        addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 695, 260);
    }

    /**
     * Set up the universe with a sun and two planets.
     */
    public void sunAndTwoPlanets()
    {
        removeAllObjects();
        addObject (new Body (50, 240.0, new Vector(270, 0.0), new Color(255, 216, 0)), 460, 310);
        addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 695, 300);
        addObject (new Body (24, 4.6, new Vector(270, 1.8), new Color(248, 160, 86)), 180, 290);
    }

    /**
     * Set up the universe with a sun, a planet, and a moon.
     */
    public void sunPlanetMoon()
    {
        removeAllObjects();
        addObject (new Body (50, 240.0, new Vector(270, 0.0), new Color(255, 216, 0)), 460, 270);
        addObject (new Body (20, 4.2, new Vector(90, 2.2), new Color(0, 124, 196)), 720, 260);
        addObject (new Body (5, 0.8, new Vector(90, 3.25), new Color(240, 220, 96)), 748, 260);
    }

    /**
     * Remove all objects currently in the world.
     */
    private void removeAllObjects()
    {
        removeObjects (getObjects(Actor.class));
    }

    /**
     * It will create the obtacles in the world.
     */
    public void makeObstacles()
    {
        int i = 0;
        for (i = 0; i < soundFiles.length; i++) 
        {
            //addObject (new Obstacle (soundFiles [i] + ".wav"), 150 + i*60, 310);
            addObject (new Obstacle (soundFiles [i] + ".wav"), 100 + i*70, 5);
            addObject (new Obstacle (soundFiles [i] + ".wav"), 100 + i*70, 615);
            addObject (new Obstacle (soundFiles [i] + ".wav"), 15 + 10, i*55);//vertical walls
            addObject (new Obstacle (soundFiles [i] + ".wav"), 925 + 10, i*55);//vertical walls
        }
    }
    
    /**
     * 
     */
    public void randomBodies (int n)
    {
         removeObjects(getObjects(Body.class));
          for (int i = 0; i < n; i++)
        {
           int s = 20 + Greenfoot.getRandomNumber(30);
           double m = s * 7.0;
           double speed = Greenfoot.getRandomNumber(150) / 100.0;
           int d = Greenfoot.getRandomNumber(360);
           int x = Greenfoot.getRandomNumber(getWidth());
           int y = Greenfoot.getRandomNumber(getHeight());
           int r =  Greenfoot.getRandomNumber(255);
           int g =  Greenfoot.getRandomNumber(255);
           int b =  Greenfoot.getRandomNumber(255);
           addObject (new Body (s, m, new Vector(d, speed), new Color(r, g, b)), x, y);

        }
    }
}
THIS IS MY OBSTACLE CLASS:
public class Obstacle extends Actor
{
    private String sound;
    private boolean touched;
    
    /**
     * Create an obstacle with a sound for each, sounds are from our piano project
     */
    public Obstacle (String soundFile)
    {
        sound = soundFile;
    }

    /**
     * Act - do whatever the Obstacle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Object body = getOneIntersectingObject(Body.class);
        
        if (body == null && touched) //we did this in our piano project but not with null, null means..
        {
            
            setImage ("block.png");
            touched = false;
        }
         if (body !=null && !touched)
        {
            
            setImage ("block-light.png");
            Greenfoot.playSound(sound);
            touched = true;
        }
        
   
    }
}
SportingLife7 SportingLife7

2021/11/30

#
also how can I make a code for depending on key pressed, keystroke will create that number of planets while 0 clears all of the planets? can't figure it out and i've been trying to figure it out for hours. Here is my code for my body class also:
public class Body extends SmoothMover
{
    // constants
    private static final double GRAVITY = 5.8;
    private static final Color defaultColor = new Color(255, 216, 0);
    
    // fields
   private double mass;
   private int size;
   //private boolean create;
    /**
     * Construct a Body with default size, mass, movement and color.
     */
    public Body()
    {
        this (20, 300, new Vector(0, 1.0), defaultColor);
    }
    
    /**
     * Construct a Body with a specified size, mass, movement and color.
     */
    public Body(int size, double mass, Vector movement, Color color)
    {
        this.size = size;
        this.mass = mass;
        addForce(movement);
        GreenfootImage image = new GreenfootImage (size, size);
        image.setColor (color);
        image.fillOval (0, 0, size-1, size-1);
        setImage (image);
    }
    
    /**
     * Act. That is: apply  the gravitation forces from
     * all other bodies around, and then move.
     */
    public void act() 
    {
        move();
        applyForces();
        bounceAtEdge();
        //checkKeyPress();?
        //changeColor(); //i edited this out and the bodies stopped changing color at the initialization of our world, now they only change color when called fro within our bounceAtEdge method
    }
    /**
     * Return the mass of this body.
     */
    public double getMass()
    {
        return mass;
    }
    /**
     * 
     */
    private void applyForces()
    {
       List<Body> bodies = getWorld().getObjects(Body.class);
       
       for (Body body : bodies)
       {
           if (body!= this)
           {
               applyGravity (body);
            }
        }
    }
    /**
     * apply the gravity force of a given body to this one
     */
    private void applyGravity(Body other)
    {
        double dx = other.getExactX() - this.getExactX();
        double dy = other.getExactY() - this.getExactY();
        Vector force = new Vector (dx,dy);
        double distance = Math.sqrt (dx * dx + dy * dy);
        double strength = GRAVITY * this.mass * other.mass / (distance * distance);
        double acceleration = strength / this.mass;
        force.setLength (acceleration);
        addForce (force);
    }
    /**
     * Check whether we have hit the edge of the universe, If so, bounce of it (our border)
     */
    private void bounceAtEdge()
    {
       if (getX() == 0 || getX () == getWorld().getWidth()-1)
       {
         setLocation((double)getX(), (double)getY());
         getMovement().revertHorizontal();
         accelerate(0.9);
         changeColor();
       }
       else if (getY() == 0 || getY () == getWorld().getHeight()-1)
       {
           setLocation((double)getX(), (double)getY());
           getMovement().revertVertical();
           accelerate(0.9);
           changeColor();
       }
    }
    /**
     * changes color of bodies in world //when it hits wall
     */
    private void changeColor()
    {
       
        int x = Greenfoot.getRandomNumber(255);
        int y = Greenfoot.getRandomNumber(255);
        int z = Greenfoot.getRandomNumber(255);
        GreenfootImage image = new GreenfootImage (size-1, size-1);
        image.setColor (new Color(x,y,z));
        image.fillOval (0, 0, size-1, size-1);
        setImage (image);
       }
    }
danpost danpost

2021/11/30

#
For vertical image issue, you could just pass a boolean (true/false) value to the constructor indicating the image is to be rotated or not. Another way is the create a reference to those vertical actors before adding them into the world and turn then right there in the loop. For keystrokes, add an act method into your Space class to detect the possible keys. It would essentially be something like this (which is not complete in itself, but shows the easiest way to break down and detect the different key options):
public void act()
{
    String key = Greenfoot.getKey();
    if (key == null) return; // no key detected
    
    // key detected
    int n = "0123456789".indexOf(key);
    if (n == -1) return; // not a number key
    
    // number key detected
    if (n == 0)  clearPlanets(); else addPlanets(n);
}
The clearPlanets and addPlanets(int) methods will need to be created in the Space class to perform those tasks.
You need to login to post a reply.