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

2015/12/30

How to fixt the error in greenfoot ((slodor) look like flappy bird but not)

walidiah walidiah

2015/12/30

#
i got task from my lecturer. i want to make when the actor(orang) pass the line, he got score +1, whe he touch line or eaten by snake, so game over. can anyone here tell me what shoul i do? where i have to fix?
Background(latar)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

    int score=0;
    
    /**
     * Constructor for objects of class latar.
     * 
     */
    public latar()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
       super(100, 120, 5); 
        
        addObject(new Orang(),60,110);
        addObject(new Line(),1,105);
        addObject(new Line(),51,105);
        addObject(new Line(),99,105);
        
        addObject(new Line(),11,90);
        addObject(new Line(),61,90);
        addObject(new Line(),99,90);
        
        addObject(new Line(),21,75);
        addObject(new Line(),71,75);
        addObject(new Line(),99,75);
        
        addObject(new Line(),31,60);
        addObject(new Line(),81,60);
        addObject(new Line(),1,60);
        
        addObject(new Line(),41,45);
        addObject(new Line(),91,45);
        
        addObject(new Line(),51,30);
        addObject(new Line(),99,30);
        addObject(new Line(),1,30);
        
        addObject(new Line(),61,15);
        addObject(new Line(),99,15);
        addObject(new Line(),11,15);
        
   
        
       addObject(new Musuh(),10,110);
        addObject(new Musuh(),90,90);
        addObject(new Musuh(),20,70);
        addObject(new Musuh(),50,50);
        addObject(new Musuh(),60,30);
        addObject(new Musuh(),40,10);
        addObject(new Diamond(),50,5);
        
        showscore();
  
 
    
    }
     public void showscore (){
        showText("score :" +score, 70,1);

    }

    public void addscore(int nilai){
        score=score+nilai;
        showscore();

    }
    
    
    
    public void addScore(){
        score++;
    } 
    
    
}
SmoothMover

import greenfoot.*;  

public abstract class SmoothMover extends Actor
{
    private Vector movement;
    private double exactX;
    private double exactY;
    
    public SmoothMover()
    {
        this(new Vector());
    }
    
    /**
     * Create new thing initialised with given speed.
     */
    public SmoothMover(Vector movement)
    {
        this.movement = movement;
    }
    
    /**
     * Move in the current movement direction. Wrap around to the opposite edge of the
     * screen if moving out of the world.
     */
    public void move() 
    {
        exactX = exactX + movement.getX();
        exactY = exactY + movement.getY();
        if(exactX >= getWorld().getWidth()) {
          exactX = 0;
       }
       if(exactX < 0) {
         exactX = getWorld().getWidth() - 1;
       }
        if(exactY >= getWorld().getHeight()) {
            exactY = 0;
        }
        if(exactY < 0) {
            exactY = getWorld().getHeight();
        }
       super.setLocation( (int) exactX, (int) exactY);       
        //
       
    }
    
    /**
     * Set the location from exact coordinates.
     */
    public void setLocation(double x, double y) 
    {
        exactX = x;
        exactY = y;
        super.setLocation((int) x, (int) y);
    }
    
    /**
     * Set the location from int coordinates.
     */
    public void setLocation(int x, int y) 
    {
        exactX = x;
        exactY = y;
        super.setLocation(x, y);
    }
    
     public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
    
     public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
    
    public void pass(Class clss){
    if(getY() == 0)  {   
        latar background = (latar) getWorld();
        background.addscore(+1);
        }
    }    
   

    /**
     * Return the exact x-coordinate (as a double).
     */
    public double getExactX() 
    {
      return exactX;
    }

    /**
     * Return the exact y-coordinate (as a double).
     */
    public double getExactY() 
    {
        return exactY;
    }

    /**
     * Increase the speed with the given vector.
     */
    public void addForce(Vector force) 
    {
        movement.add(force);
    }
    
    /**
     * Accelerate the speed of this mover by the given factor. (Factors < 1 will
     * decelerate.)
     */
    public void accelerate(double factor)
    {
        movement.scale(factor);
        if (movement.getLength() < 0.15) {
            movement.setNeutral();
        }
    }
    
    /**
     * Return the speed of this actor.
     */
    public double getSpeed()
    {
        return movement.getLength();
    }
    
    /**
     * Stop movement of this actor.
     */
    //public void stop()
    //{
      //  movement.setNeutral();
    //}
    
    /**
     * Return the current speed.
     */
    public Vector getMovement() 
    {
        return movement;
    }
}
Line

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


public class Line extends SmoothMover
{
   
    public void act() 
    {
       setLocation(getX() + 1, getY()); //jalan kanan
        if(getX()==99)
        {
            setLocation(0,getY()); //untuk menembus bg
        }
        
        lookForOrang();
    }    
    
     public void lookForOrang()
    {
        if ( canSee(Orang.class) ) // ketka line menabrak line skor min 1
        {
           eat(Orang.class);
            //Greenfoot.playSound("au.wav");
            latar background = (latar) getWorld();
            background.addscore(-1);
            //Greenfoot.stop();
        }else if(getY() == 0)  {   // jika tidak +1
        //eat(Line.class);
        latar background = (latar) getWorld();
        background.addscore(+1);
        }
        
        
    }
}
Orang

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

public class Orang extends SmoothMover
{
    private static final int gunReloadTime = 5;         

    private int reloadDelayCount;         
    int score;
    
   // private GreenfootImage orang = new GreenfootImage("Orang.png");    
   // private GreenfootImage orangWithThrust = new GreenfootImage("OrangWithThrust.png");

    //public Orang()
    //{
      //  reloadDelayCount = 5;
    //}

     public void act()
     
    {
        lookForDiamond();
        
         if(Greenfoot.isKeyDown("right"))// pergerakan menggunakan tanda panah atas, bawah, kiri, kanan
        {
        setLocation(getX()+1,getY());
        }
        
        if(Greenfoot.isKeyDown("left"))
        {
        setLocation(getX()-1,getY());
        }
        
        if(Greenfoot.isKeyDown("up"))
        {
        setLocation(getX(),getY()-1);
        }
        
        if(Greenfoot.isKeyDown("down"))
        {
        setLocation(getX(),getY()+1);
        }
    
        if (getY() == 0)  {   // jika tidak +1
        //eat(Line.class);
        latar background = (latar) getWorld();
        background.addscore(+1);
        }
        
        checkKeys();
        reloadDelayCount++;
    }
    
    private void checkKeys() // space untuk menembak
    {
        if (Greenfoot.isKeyDown("space")) 
        {
            fire();
        }
    }
    
    private void fire() // pergerakan tembakan
    {
        if (reloadDelayCount >= gunReloadTime) 
        {
            Batu batu = new Batu (getMovement().copy(), getRotation());
            getWorld().addObject (batu, getX(), getY());
            batu.move ();
            reloadDelayCount = 0;
        }
    }
    
   
    
    public void lookForDiamond()
    {
        if ( canSee(Diamond.class) ) 
        {
            eat(Orang.class);// jika menyentuh line -1
            latar background = (latar) getWorld();
            background.addscore(-1);
            
            //Greenfoot.playSound("au.wav");
            //Greenfoot.stop();
        }else if (getY() == 0)  {   // jika tidak +1
        //eat(Line.class);
        latar background = (latar) getWorld();
        background.addscore(+1);
        }
    }
    
    public void addScore(){
        score++;
    } 
    //if(getX() == 0)  
      //  {  
        //    ((Score)(Score)getWorld().getObjects(Score.class).get(0)).upScore();  
          //  getWorld().removeObject(this);  
       // }
   
    
}
Diamond

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

/**
 * Write a description of class Diamond here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Diamond extends SmoothMover
{
    /**
     * Act - do whatever the Diamond wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
}
Musuh / snake

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

public class Musuh extends SmoothMover
{
    /** Size of this musuh */
    private int size;

    /** When the stability reaches 0 the amusuh will explode */
    private int stability;

    
    public Musuh()
    {
        this(50);
    }
    
    public Musuh(int size)
    {
        super(new Vector(Greenfoot.getRandomNumber(180), 1));
        setSize(size);
    }
    
    public Musuh(int size, Vector speed)
    {
        super(speed);
        setSize(size);
    }
    
    public void act()
    {         
        move();
        lookForOrang();
    }

    /**
     * Set the size of this amusuh. Note that stability is directly
     * related to size. Smaller amusuhs are less stable.
     */
    public void setSize(int size) 
    {
        stability = size;
        this.size = size;
        GreenfootImage image = getImage();
        image.scale(size, size);
    }

    /**
     * Return the current stability of this amusuh. (If it goes down to 
     * zero, it breaks up.)
     */
    public int getStability() 
    {
        return stability;
    }
    
    /**
     * Hit this amusuh dealing the given amount of damage.
     */
    public void hit(int damage) {
        stability = stability - damage;
        if(stability <= 0) 
            breakUp ();         
    }
    
    /**
     * Break up this amusuh. If we are still big enough, this will create two
     * smaller amusuhs. If we are small already, just disappear.
     */
    private void breakUp() 
    {
        //Greenfoot.playSound("Explosion.wav");
        
        if(size <= 16) 
        {
            getWorld().removeObject(this);
        }
        else 
        {
            int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45);
            double l = getMovement().getLength();
            Vector speed1 = new Vector(r + 60, l * 0.5);
            Vector speed2 = new Vector(r - 60, l * 0.5);        
            Musuh a1 = new Musuh(size/2, speed1);
            Musuh a2 = new Musuh(size/2, speed2);
            getWorld().addObject(a1, getX(), getY());
            getWorld().addObject(a2, getX(), getY());        
            a1.move();
            a2.move();
        
            getWorld().removeObject(this);
        }
    }
    
     public void lookForOrang()
    {
        if ( canSee(Orang.class) ) 
        {
            eat(Orang.class);
            //Greenfoot.playSound("au.wav");
            //Greenfoot.stop();
        }
    }
}
Batu

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

/**
 * A batu that can hit ausuhs.
 * 
 * @author Poul Henriksen
 */
public class Batu extends SmoothMover
{
    /** The damage this batu will deal */
    private static final int damage = 30;
    
    /** A batu looses one life each act, and will disappear when life = 0 */
    private int life = 30;
    
    public Batu()
    {
    }
    
    public Batu(Vector speed, int rotation)// kecepatan dan arah batu
    {
        super(speed);
        setRotation(rotation);
        addForce(new Vector(rotation, 15));
        Greenfoot.playSound("Shoot.mp3");
    }
    
    /**
     * The batu will damage ausuhs if it hits them.
     */
    public void act()// life 
    {
        if(life <= 0) {
           life++;
           latar background = (latar) getWorld();
           background.addscore(+1);
            //move();
            //checkMusuhHit();
           // getWorld().removeObject(this);
        } 
        else {
            life--;
            move();
            checkMusuhHit();
        }
    }
    
    /**
     * Check whether we have hit an ausuh.
     */
    private void checkMusuhHit()
    {
        Musuh musuh = (Musuh) getOneIntersectingObject(Musuh.class);
        if (musuh != null){
            getWorld().removeObject(this);
            musuh.hit(damage);
        }
    }
}
Game over

import greenfoot.*;

public class gameover extends SmoothMover
{
    
    
    {
        // Add your action code here.
    }    
}
Other class vector

import greenfoot.Greenfoot;


public final class Vector
{
    double dx;
    double dy;
    int direction;
    double length;
    
    /**
     * Create a new, neutral vector.
     */
    public Vector()
    {
    }

    /**
     * Create a vector with given direction and length. The direction should be in
     * the range [0..359], where 0 is EAST, and degrees increase clockwise.
     */
    public Vector(int direction, double length)
    {
        this.length = length;
        this.direction = direction;
        updateCartesian();
    }

    /**
     * Create a vector by specifying the x and y offsets from start to end points.
     */
    public Vector(double dx, double dy)
    {
        this.dx = dx;
        this.dy = dy;
        updatePolar();
    }

    /**
     * Set the direction of this vector, leaving the length intact.
     */
    public void setDirection(int direction) 
    {
        this.direction = direction;
        updateCartesian();
    }
   
    /**
     * Add another vector to this vector.
     */
    public void add(Vector other) 
    {
        dx += other.dx;
        dy += other.dy;
        updatePolar();
    }
    
    /**
     * Set the length of this vector, leaving the direction intact.
     */
    public void setLength(double length) 
    {
        this.length = length;
        updateCartesian();
    }
    
    /**
     * Scale this vector up (factor > 1) or down (factor < 1). The direction
     * remains unchanged.
     */
    public void scale(double factor) 
    {
        length = length * factor;
        updateCartesian();
    }
    
    /**
     * Set this vector to the neutral vector (length 0).
     */
    public void setNeutral() {
        dx = 0.0;
        dy = 0.0;
        length = 0.0;
        direction = 0;
    }
    
    /**
     * Revert to horizontal component of this movement vector.
     */
    public void revertHorizontal() {
        dx = -dx;
        updatePolar();
    }
    
    /**
     * Revert to vertical component of this movement vector.
     */
    public void revertVertical() {
        dy = -dy;
        updatePolar();
    }
    
    /**
     * Return the x offset of this vector (start to end point).
     */
    public double getX() {
        return dx;
    }
     
    /**
     * Return the y offset of this vector (start to end point).
     */
    public double getY() {
        return  dy;
    }
    
    /**
     * Return the direction of this vector (in degrees). 0 is EAST.
     */
    public int getDirection() {
        return direction;
    }
    
    /**
     * Return the length of this vector.
     */
    public double getLength() {
        return length;
    }

    /**
     * Update the direction and length fom the current dx, dy.
     */
    private void updatePolar() 
    {
        this.direction = (int) Math.toDegrees(Math.atan2(dy, dx));
        this.length = Math.sqrt(dx*dx+dy*dy);
    }   
    
    /**
     * Update dx and dy from the current direction and length.
     */
    private void updateCartesian() 
    {
        dx = length * Math.cos(Math.toRadians(direction));
        dy = length * Math.sin(Math.toRadians(direction));   
    }
    
    /**
     * Return a copy of this vector.
     */
    public Vector copy() 
    {
        Vector copy = new Vector();
        copy.dx = dx;
        copy.dy = dy;
        copy.direction = direction;
        copy.length = length;
        return copy;
    }    
}
danpost danpost

2015/12/31

#
In the Line class, uncommenting line 28 will have the project stop when a line is touched. Also, line 30 should be:
getWorld().removeObject(this);
(using 'eat' removes an intersecting object -- but, an object is not considered intersecting itself). Uncommenting lines 101 and 102 of the Musuh class should be fine to end the game when a snake touches the orang.
walidiah walidiah

2016/1/2

#
okay. thank's 4 ur help :D
You need to login to post a reply.