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

2018/5/9

I'm Trying to Make a game

razornale razornale

2018/5/9

#
Hello, so my group partner and i cannot figure out for the life of us what is going on with our code. We have stared at it over and over so i will attach all the code we have so far. and explain as i go
public class backGround extends World
{

    static Counter counter = new Counter();
    Counter counter2 = new Counter();
    
    SimpleTimer st = new SimpleTimer();
    
    /**
     * Constructor for objects of class backGround.
     * 
     */
    public backGround()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 550, 1);
        
        Jerry j1 = new Jerry();
        addObject(j1,100,300);
        
        counter.setValue(0);
        st.mark();
        prepare();
    }
    
    public void act()
    {
        if(Greenfoot.getRandomNumber(200)<3)
        {
            addObject(new Basketball(),Greenfoot.getRandomNumber(601),0);
            
            counter2.setValue(st.millisElapsed()/1000);
            
            if(counter.getValue()>=40)
            {
                showText("Time is Up", 300,200);
                Greenfoot.stop();
            }
        }
   } 
   private void prepare()
   {
        Jeffy jeffy = new Jeffy();
        addObject(jeffy, 550, 46);
        showText("score", 63, 16);
        
        addObject(counter, 63, 36);
        showText("Timer", 548, 362);
        
        addObject(counter2, 548, 382);
   }
}
This is our backGround where is says counter.setValue(0); st.mark(); and st.millisElapsed is where we are getting our errors at in this class.
public class Counter extends Actor
{
    public static final float FONT_SIZE = 18.0f;
    
    private int value;
    private String text;
    private int stringLength;
    private Font font;

    /**
     * Create a counter without text. 
     */
    public Counter()
    {
        this("");
    }

    /**
     * Create a counter with prefix text, and 0 value.
     */
    public Counter(String prefix)
    {
        this(prefix, 0);
    }
    
    /**
     * Create a counter with prefix text and value.
     */
    public Counter(String prefix, int value)
    {
        this.value = value;
        text = prefix;
        stringLength = (text.length() + 4) * 10;

        GreenfootImage image = new GreenfootImage(stringLength, 22);
        setImage(image);
        font = image.getFont();
        font = font.deriveFont(FONT_SIZE);
        updateImage();
    }

    /**
     * Increment the counter by one.
     */
    public void increment()
    {
        value++;
        updateImage();
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return value;
    }
    
    public int setValue()
    {
        this.value = value;
    }
    
    /**
     * Make the image
     */
    private void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.setFont(font);
        image.setColor(Color.BLACK);
        image.drawString(text + value, 6, (int)FONT_SIZE);
    }   
}
currently this code for counter appears to be working including it for an as a whole purpose
public class Scoreboard extends Actor
{
    public static final float FONT_SIZE = 48.0f;
    public static final int WIDTH = 600;
    public static final int HEIGHT = 400;
    
    public Scoreboard()
    {
        makeImage("Scores", "Score: ", "nobody", 100);
    }

    /**
     * Create a score board for an interim result.
     */
    public Scoreboard(String title, String text, String prefix, int map, int[] scores)
    {
        makeImage(title, text, prefix, scores[map]);
    }
    
    /**
     * Create a score board for the final result.
     */
    public Scoreboard(String title, String text, String prefix, int[] scores)
    {
        int total = 0;
        for (int val : scores) {
            total += val;
        }
        makeImage(title, text, prefix, total);
    }
    
    /**
     * Make the score board image.
     */
    private void makeImage(String title, String text, String prefix, int score)
    {
        GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);

        image.setColor(new Color(0, 0, 0, 128));
        image.fillRect(0, 0, WIDTH, HEIGHT);
        image.setColor(new Color(0, 0, 0, 128));
        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(text, 60, 220);
        image.drawString(prefix + score, 60, 320);
        setImage(image);
    }
    
       
      
}
same thing with our scoreboard
public class Basketball extends Actor
{
    /**
     * Act - do whatever the Basketball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(), getY()+2);
        
        if(getWorld().getHeight()-getY()<20)
        {
            getWorld().removeObject(this);
        }   
    }
}
again same is working
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import static greenfoot.Greenfoot.*;

/**
 * Write a description of class Jeffy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Jeffy extends Actor
{
    /**
     * Act - do whatever the Jeffy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(getRandomNumber(10));
        
        if(atWorldEdge())
        {
            turn(getRandomNumber(10));
        }
        
        if(isTouching(Jerry.class))
        {
            removeTouching(Jerry.class);
            
            GreenfootImage g1 = new GreenfootImage("You lose", 50, Color.WHITE, Color.BLACK);
            
            setImage(g1);
            setLocation(300, 200);
            setRotation(0);
            stop();
        }
    }    
    
    public boolean atWorldEdge()
    {
        if(getY()<20 || getWorld().getHeight()-getY()<20)
        {
            return true;
        }
        if(getX()<20 || getWorld().getWidth() - getX()<20)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
also working
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.Greenfoot.*;

/**
 * Write a description of class Jerry here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Jerry extends Actor
{
    /**
     * Act - do whatever the Jerry 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.
        
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+2, getY());
        }
        
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-2, getY());
        }
        
        if(Greenfoot.isKeyDown("down"));
        {
            setLocation(getX(), getY()+2);
        }
        
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-2);
        }
        
        Actor a = getIntersectingObjects(Basketball.class);
        
        if(a!=null)
        {
            backGround.counter.add(1);
            getWorld().removeObject(a);
        }
        
        if(backGround.counter.getValue()>=5)
        {
            GreenfootImage g1 = new GreenfootImage("You Win", 50, Color.RED, Color.BLACK);
            
            setImage(g1);
            setLocation(300, 200);
            setRotation(0);
            Greenfoot.stop();
        }
    }
}
the errors in the Jerry Class appear where is says (Basketball.class); and backGround.counter.add(1); any help that you may be able to give would be greatly appreciated and thank you in advance
danpost danpost

2018/5/9

#
razornale wrote...
Hello, so my group partner and i cannot figure out for the life of us what is going on with our code.
The setValue method of the Counter class shows a return value and no parameters, yet you call it with a parameter of '0'.
razornale razornale

2018/5/9

#
Okay so i went and changed the parameter in Counter class to match what we had in background, and that cause backGround and Jerry to appear to be in working order, however counter started showing an error at public int setValue(0) that it was an illegal start. so i changed it to 1 and same thing, then i took the parameter out and now the st.mark on line 22 and st.MillisElapsed on line 32 are showing error of cannot find symbol - method mark() and cannot find symbol- method do you have any idea?
danpost danpost

2018/5/9

#
razornale wrote...
Okay so i went and changed the parameter in Counter class to match what we had in background, and that cause backGround and Jerry to appear to be in working order, however counter started showing an error at public int setValue(0) that it was an illegal start. so i changed it to 1 and same thing, then i took the parameter out and now the st.mark on line 22 and st.MillisElapsed on line 32 are showing error of cannot find symbol - method mark() and cannot find symbol- method do you have any idea?
I do not see any reasons for errors on those lines (unless you created your own SimpleTimer class and did not import the one supplied by greenfoot or changed the code within that class).
razornale razornale

2018/5/10

#
Hello again, so we were able to fix all the other issues that we were having now this is the last issue that we have, in out Jerry class we get an error of illegal start of expression
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.Greenfoot.*;

/**
 * Write a description of class Jerry here.
 * 
 * @author (Caitlin Premont and Glen Mathew)  
 * @version (05/08/2018)
 */
public class Jerry extends Actor
{
    /**
     * Act - do whatever the Jerry wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    // Greenfootsound jerrySound = new greenfootsound("Jerrysound.mp3");
       public void act() 
       {
        // Add your action code here.
        
        private int life = 1;  //this is where the error appears
        
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+2, getY());
        }
        
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-2, getY());
        }
        
        if(Greenfoot.isKeyDown("down"));
        {
            setLocation(getX(), getY()+2);
        }
        
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-2);
        }
        
        if(life <=0)
        {
            getWorld().removeObject(this);
        }
        else
        {
            life--;
            move();
            checkBasketballColl();
        }
    }
    
        private void checkBasketballColl()
        {
            Basketball a = (Basketball) getOneIntersectingObject(Basketball.class);
            if (a !=null)
            {
                backGround background = (backGround) getWorld();
                backGround.removeObject(this);
                space.gameOver();
            }
       
        if(backGround.counter.getValue()>=5)
        {
            GreenfootImage g1 = new GreenfootImage("You Win", 50, Color.RED, Color.BLACK);
            
            setImage(g1);
            setLocation(300, 200);
            setRotation(0);
            Greenfoot.stop();
        }
    }
danpost danpost

2018/5/10

#
Move line 22 to be line 16.
razornale razornale

2018/5/10

#
Okay, now we are getting an error on lines 62 and 63 that reads non-static method removeObject(greenfoot_Actor) cannot be referenced from a static context We are at a loss, and again thanks for the help
danpost danpost

2018/5/10

#
Line 62 is just a spelling error (see line 61). For line 63, I presume space is another class on which you are trying to execute the gameOver method; but you need an object of the class to run the method (not the class itself).
You need to login to post a reply.