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

2018/5/15

Help with counter

TupTup98 TupTup98

2018/5/15

#
Hello i am trying to make my counter work,when my player gets to a specific y koordinate. The player has to Cross the Road wit many cars, when he gets over the Counter has to go up and then the player has to be send back, im new at programming, so i really need some help. right now you can only walk aorund and when you get hit by a car you are dead and a scoreboard shows up
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * A Counter class that allows you to display a numerical value on screen.
 * 
 * The Counter is an actor, so you will need to create it, and then add it to
 * the world in Greenfoot.  If you keep a reference to the Counter then you
 * can adjust its value.  Here's an example of a world class that
 * displays a counter with the number of act cycles that have occurred:
 * 
 * <pre>
 * class CountingWorld
 * {
 *     private Counter actCounter;
 *     
 *     public CountingWorld()
 *     {
 *         super(600, 400, 1);
 *         actCounter = new Counter("Act Cycles: ");
 *         addObject(actCounter, 100, 100);
 *     }
 *     
 *     public void act()
 *     {
 *         actCounter.setValue(actCounter.getValue() + 1);
 *     }
 * }
 * </pre>
 * 
 * @author Neil Brown and Michael Kölling 
 * @version 1.0
 */
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    private String prefix;
    
    public Counter()
    {
        this(new String());
    }

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        this.prefix = prefix;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

    /**
     * Add a new score to the current counter value.  This will animate
     * the counter over consecutive frames until it reaches the new value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return target;
    }

    /**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
    
    /**
     * Sets a text prefix that should be displayed before
     * the counter value (e.g. "Score: ").
     */
    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
        
        if (text.getWidth() > image.getWidth() - 20)
        {
            image.scale(text.getWidth() + 20, image.getHeight());
        }
        
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
import greenfoot.*;

/**
 *Får spilleren til at bevæge sig ved hjælp af piltaster 
 *the plauers moves the actor MoveActorWithArrows method + en hastighed int speed
 * 
 * @author (jean) 
 * @version (1.0)
 */
public class bevælgelsemedpiltaster  
{
   

    /**
     * Constructor for objects of class bevælgelsemedpiltaster
     */
    Actor myActor = null;
    public bevælgelsemedpiltaster(Actor a)
    {
        myActor = a;
    }
    
    public void bevælgelsemedpiltaster (int speed)
{
    if (Greenfoot.isKeyDown("right"))
    {
        myActor.setRotation(0);
        myActor.move (speed);
    }
     if (Greenfoot.isKeyDown("down"))
    {
        myActor.setRotation(90);
        myActor.move (speed);
    }
     if (Greenfoot.isKeyDown("left"))
    {
        myActor.setRotation(180);
        myActor.move (speed);
    }
     if (Greenfoot.isKeyDown("up"))
    {
        myActor.setRotation(270);
        myActor.move (speed);
    }
}
}
the screen
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Stickman here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Stickman extends Actor
{
    /**
     * Act - do whatever the Stickman wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    bevælgelsemedpiltaster bmp = new bevælgelsemedpiltaster(this);
    public void act() 
    {
        bmp.bevælgelsemedpiltaster(5);
    }    

}
ws up. i have diffuculties figuring out how i make my counter go up when gets to the y koordinate 10. and how i make the player spawn at the beginning. some one who can help
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Greenbil here.
 * 
 * @author Jean P 
 * @version (a version number or a date)
 */
public class Greenbil extends Biler
{
    /**
     * Act - do whatever the Greenbil wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
         public void act()
    {
        //getImage().scale(140, 74);
        move(5);
        turnAtEdge();
        tryToEatPlayer();
        
    }
    
     /**
     * If we reach the edge of the world, turn a little bit.
     */
    public void turnAtEdge()
    {
        if (atWorldEdge())
        {
            turn(180);
            move(750);
            turn(180);
            
        }
    }
    
    /**
     * Act - do whatever the Ambu1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void tryToEatPlayer()
    {
        if (canSee(Stickman.class) )
        {
            eat(Stickman.class);
            Greenfoot.playSound("buzz.wav");
            ((MyWorld) getWorld()).GameOver();[code]import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * An actor class that can display a scoreboard, using Greenfoot's
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;

/**
 * The ScoreBoard is used to display results on the screen. It can display some
 * text and several numbers.
 * 
 * @author Jean P
 * @version 1.0
 */
public class ScoreBoard extends Actor
{
    public static final float FONT_SIZE = 48.0f;
    public static final int WIDTH = 400;
    public static final int HEIGHT = 300;
    
    /**
     * Create a score board with dummy result for testing.
     */
    public ScoreBoard()
    {
        this(100);
    }

    /**
     * Create a score board for the final result.
     */
    public ScoreBoard(int score)
    {
        makeImage("Game Over", "Score: ", score);
    }

    /**
     * Make the score board image.
     */
    private void makeImage(String title, String prefix, int score)
    {
        GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);

        image.setColor(new Color(0, 0, 0, 160));
        image.fillRect(0, 0, WIDTH, HEIGHT);
        image.setColor(new Color(255, 255, 255, 100));
        image.fillRect(5, 5, WIDTH-10, HEIGHT-10);
        Font font = image.getFont();
        font = font.deriveFont(FONT_SIZE);
        image.setFont(font);
        image.setColor(Color.WHITE);
        image.drawString(title, 60, 100);
        image.drawString(prefix + score, 60, 200);
        setImage(image);
    }
}

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

/**
 * Løb over vejen.
 * 
 * @author (Jean P Cassøe) 
 * @version (1.0)
 */
public class MyWorld extends World
{
    public Stickman Stickman;
    public int LIFE = 1;
    public int live = 1;
    public int dead = 0;
    //placering på vejej på vejen
    public int l1 = 80;
    public int l2 = 165;
    public int l3 = 240;
    public int l4 = 310;
    public int worldWidth = 800;
    public int worldHeight = 330;
    public int sector = worldWidth/5;
    static Counter counter = new Counter();
    Counter counter2 = new Counter ();
  
   
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Størrelse på verden
        super(800, 330, 2); 
        //placering at spille figur
        Stickman j1 = new Stickman ();
        addObject (j1, 400, 320); 
        //point tæller
        Counter j2= new Counter();
        addObject (j2, 100, 10);
        
        counter.setValue(0);
        prepare ();
    }
    
   /**
     * Foreberedese til spillets starts
     * tilføj objekter til planet.
     */
    private void prepare()
    {

        Ambu1 ambu = new Ambu1();
        addObject(ambu, 700, 110);
        ambu.setRotation(180);

        Ambu1 ambu2 = new Ambu1();
        addObject(ambu2, 303, 175);
        ambu2.setRotation(0);

        Greenbil gbil = new Greenbil();
        gbil.setRotation(180);

        Greenbil gbil2 = new Greenbil();
        addObject(gbil2, 435, 110);
        gbil2.setRotation(180);

        Bluebil bbil = new Bluebil();
        addObject(bbil, 632, 110);
        bbil.setRotation(180);

        Bluebil bbil2 = new Bluebil();
        addObject(bbil2, 197, 110);
        bbil2.setRotation(180);

        Redbil rbil = new Redbil();
        addObject(rbil, 521, 50);
        rbil.setRotation(180);

        Redbil rbil2 = new Redbil();
        addObject(rbil2, 500, 110);
        rbil2.setRotation(180);

        Ambu1 ambu3 = new Ambu1();
        addObject(ambu3, 700, 175);

        Redbil rbil3 = new Redbil();
        addObject(rbil3, 525, 175);

        Bluebil bbil3 = new Bluebil();
        addObject(bbil3, 375, 175);

        Greenbil gbil3 = new Greenbil();
        addObject(gbil3, 171, 175);
        
        Greenbil gbil4 = new Greenbil();
        addObject(gbil4, 627, 250);

        Redbil rbil4 = new Redbil();
        addObject(rbil4, 350, 250);

        Bluebil bbil4 = new Bluebil();
        addObject(bbil4, 483, 250);
   }  
   
    /**
     * Called when game is up. Stop running and display score.
     */
    public void GameOver() 
    {
        addObject(new ScoreBoard(counter.getValue()), getWidth()/2, getHeight()/2);
        Greenfoot.playSound("buzz.wav");
        Greenfoot.stop();
    }
    }
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

   {
    /**
     * Test if we are close to one of the edges of the world. Return true is we are.
     */
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
    
    
    /**
     * Return true if we can see an object of class 'clss' right where we are. 
     * False if there is no such object here.
     */
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }

    
    /**
     * Try to eat an object of class 'clss'. This is only successful if there
     * is such an object where we currently are. Otherwise this method does
     * nothing.
     */
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }

    
}
danpost danpost

2018/5/15

#
In bevælgelsemedpiltaster class, at end of bevælgelsemedpiltaster method:
if (myActor.getY() < 10)
{
    World w = myActor.getWorld();
    int ww = w.getWidth();
    int wh = w.getHeight();
    myActor.setLocation(ww/2, wh-10); // wherever
    bumpCounter(); // pseudo-code (code to be determined)
}
The reason for the last line is that you create 3 different counters in your MyWorld class code -- (1) line 17 creates a Counter object that you named counter which, as static, belongs to the class itself (not the world); (2) line 18 creates a Counter object that you named counter2 which does belong to the MyWorld world object; and (3) line 39 creates a Counter object which line 40 adds to the world (locally named j2, which is not valid beyond the creation of the world. The first two counters are not used, apparently (although, you do reset the class counter to zero in the constructor). Now that I have established that only one Counter object is in the world, we can replace 'bumpCounter();' with the following:
((Counter) w.getObjects(Counter.class).get(0)).add(1);
You need to login to post a reply.