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

2018/1/4

How do you show a Game Over?

Potato-sama Potato-sama

2018/1/4

#
I'm new-ish to green foot and I'm wondering where I'm missing a line of code because when the main character dies, nothing shows up. I also am implementing code for a health bar too and it isn't going down. But I want to take care of the game over first. Can anyone assist a n00b? Gameover:
//Gameover:

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class GameOver here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GameOver extends Actor
{
    String msgTxt = "GAME OVER";
    /**
     * Act - do whatever the GameOver wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public GameOver()
    {
        setImage(new GreenfootImage("Game Over", 48, greenfoot.Color.WHITE, greenfoot.Color.BLACK));
    }
}

//Char:

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

/**
 * Write a description of class mainchar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class mainchar extends Actor
{   
    int aniframe = 0;
         GreenfootImage s1 = new GreenfootImage ("maincharleftwalk1.gif");
         GreenfootImage s2 = new GreenfootImage ("maincharleftwalk2.gif");
         GreenfootImage s3 = new GreenfootImage ("maincharleftwalk3.gif");
          
         GreenfootImage maincharrightwalk1 = new GreenfootImage ("maincharrightwalk1.gif");
         GreenfootImage maincharrightwalk2 = new GreenfootImage ("maincharrightwalk2.gif");
         GreenfootImage maincharrightwalk3 = new GreenfootImage ("maincharrightwalk3.gif");
         
         GreenfootImage maincharbackrightwalk = new GreenfootImage ("maincharbackrightwalk.gif");
         GreenfootImage maincharbackleftwalk = new GreenfootImage ("maincharbackleftwalk.gif");
         
         GreenfootImage maincharforwardrightwalk = new GreenfootImage ("maincharforwardrightwalk.gif");
         GreenfootImage maincharforwardleftwalk = new GreenfootImage ("maincharforwardleftwalk.gif");
         
    public int directionstate;
    int coincount = 0;
    
    boolean touchingOthertestenemy = false;
    /**
     * Act - do whatever the mainchar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       int y = getY();
        int x = getX();
    eatCoins();
        if(Greenfoot.isKeyDown("W"))
        {
            y--;
           aniup();
    }
    if(Greenfoot.isKeyDown("S"))
        {y++;
         anidown();
        }
    if(Greenfoot.isKeyDown("A"))
        {x--;
         anileft();
        }
    if(Greenfoot.isKeyDown("D"))
        {x++;
         aniright();
        }
    setLocation(x, y);  
 }
 public void anileft()
 {
    aniframe ++;
    System.out.println(aniframe);
    if (aniframe <= 10)
    {
        System.out.println("changing to aniframe1");
        setImage(s1);
    }
    else if (aniframe <= 20)
    {
      System.out.println("changing to aniframe2");
      setImage(s2);
    }
    else if (aniframe <= 30) {
        	System.out.println("changing to aniframe3");
        	setImage (s3);
   }
   if (aniframe == 30)
   {
       System.out.println("reseting aniframe");
       aniframe = 0;
   }
 }
 public void aniright()
 {
    aniframe ++;
    System.out.println(aniframe);
    if (aniframe <= 10)
    {
       System.out.println("changing to aniframe1");
       setImage(maincharrightwalk1);
    }
    else if (aniframe <= 20)
    {
      System.out.println("changing to aniframe2");
      setImage(maincharrightwalk2);
    }
    else if (aniframe <= 30) {
      System.out.println("changing to aniframe3");
      setImage (maincharrightwalk3);
   }
   if (aniframe == 30)
   {
       System.out.println("reseting aniframe");
       aniframe = 0;
   }
 }
 public void aniup()
 {
    aniframe ++;
    System.out.println(aniframe);
   if (aniframe <= 10)
    {
       System.out.println("changing to aniframe1");
       setImage(maincharbackrightwalk);
    }
    else if (aniframe <= 20)
    {
      System.out.println("changing to aniframe2");
      setImage(maincharbackleftwalk);
    }
   if (aniframe == 30)
   {
       System.out.println("reseting aniframe");
       aniframe = 0;
   }
 }
 public void anidown()
 {
    aniframe ++;
    System.out.println(aniframe);
   if (aniframe <= 10)
    {
       System.out.println("changing to aniframe1");
       setImage(maincharforwardrightwalk);
    }
    else if (aniframe <= 20)
    {
      System.out.println("changing to aniframe2");
      setImage(maincharforwardleftwalk);
    }
   if (aniframe == 30)
   {
       System.out.println("reseting aniframe");
       aniframe = 0;
   }
 }
 
 public void eatCoins()
    {
       Actor coin;
       coin = getOneObjectAtOffset(0, 0,coin.class);
        
       if(coin!=null)
       {
        MyWorld myworld = (MyWorld)getWorld();
        getWorld().removeObject(coin);
        coincount++;
        System.out.println(coincount);
       }
 }
 public void hitOthertestenemy()
 {
   Actor Othertestenemy = getOneIntersectingObject(Othertestenemy.class);
    if(Othertestenemy != null)
  { World myWorld = getWorld();
       MyWorld myworld = (MyWorld)myWorld;
       Healthbar healthbar = myworld.getHealthBar();
    if(touchingOthertestenemy == false)
       {
         healthbar.loseHealth();
         touchingOthertestenemy = true;
         if(healthbar.health <=0)
         {
               GameOver gameover = new GameOver();
               myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
               myWorld.removeObject(this);
         }
        }
  } else {
      touchingOthertestenemy = false; 
  }
 }
}

//World:

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

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    Healthbar healthbar = new Healthbar();
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 900, 1);
        prepare();
    }

    public Healthbar getHealthBar()
    {        
       return healthbar;
    }    
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        addObject(healthbar, 200, 40);
        THEtree tree = new THEtree();
        addObject(tree,877,39);
        tree.setLocation(865,37);
        removeObject(tree);
        THEtree thetree2 = new THEtree();
        addObject(thetree2,881,58);
        THEtree thetree3 = new THEtree();
        addObject(thetree3,863,114);
        THEtree thetree4 = new THEtree();
        addObject(thetree4,884,195);
        THEtree thetree5 = new THEtree();
        addObject(thetree5,853,255);
        THEtree thetree6 = new THEtree();
        addObject(thetree6,845,187);
        THEtree thetree7 = new THEtree();
        addObject(thetree7,887,316);
        THEtree thetree8 = new THEtree();
        addObject(thetree8,853,370);
        THEtree thetree9 = new THEtree();
        addObject(thetree9,891,432);
        THEtree thetree10 = new THEtree();
        addObject(thetree10,862,492);
        THEtree thetree11 = new THEtree();
        addObject(thetree11,889,566);
        THEtree thetree12 = new THEtree();
        addObject(thetree12,856,617);
        THEtree thetree13 = new THEtree();
        addObject(thetree13,884,685);
        THEtree thetree14 = new THEtree();
        addObject(thetree14,852,750);
        THEtree thetree15 = new THEtree();
        addObject(thetree15,882,824);
        THEtree thetree16 = new THEtree();
        addObject(thetree16,842,869);
        THEtree thetree17 = new THEtree();
        addObject(thetree17,793,867);
        THEtree thetree18 = new THEtree();
        addObject(thetree18,739,871);
        THEtree thetree19 = new THEtree();
        addObject(thetree19,684,871);
        THEtree thetree20 = new THEtree();
        addObject(thetree20,633,870);
        THEtree thetree21 = new THEtree();
        addObject(thetree21,584,887);
        THEtree thetree22 = new THEtree();
        addObject(thetree22,537,868);
        THEtree thetree23 = new THEtree();
        addObject(thetree23,499,897);
        THEtree thetree24 = new THEtree();
        addObject(thetree24,450,888);
        THEtree thetree25 = new THEtree();
        addObject(thetree25,404,873);
        THEtree thetree26 = new THEtree();
        addObject(thetree26,355,879);
        THEtree thetree27 = new THEtree();
        addObject(thetree27,309,873);
        THEtree thetree28 = new THEtree();
        addObject(thetree28,263,870);
        THEtree thetree29 = new THEtree();
        addObject(thetree29,222,891);
        THEtree thetree30 = new THEtree();
        addObject(thetree30,178,870);
        THEtree thetree31 = new THEtree();
        addObject(thetree31,134,893);
        THEtree thetree32 = new THEtree();
        addObject(thetree32,96,875);
        THEtree thetree33 = new THEtree();
        addObject(thetree33,49,892);
        THEtree thetree34 = new THEtree();
        addObject(thetree34,12,856);
        THEtree thetree35 = new THEtree();
        addObject(thetree35,26,54);
        thetree34.setLocation(31,83);
        thetree35.setLocation(23,56);
        thetree35.setLocation(25,191);
        thetree34.setLocation(24,56);
        thetree35.setLocation(13,112);
        THEtree thetree36 = new THEtree();
        addObject(thetree36,28,181);
        THEtree thetree37 = new THEtree();
        addObject(thetree37,13,236);
        THEtree thetree38 = new THEtree();
        addObject(thetree38,35,297);
        THEtree thetree39 = new THEtree();
        addObject(thetree39,7,372);
        THEtree thetree40 = new THEtree();
        addObject(thetree40,32,429);
        THEtree thetree41 = new THEtree();
        addObject(thetree41,13,488);
        THEtree thetree42 = new THEtree();
        addObject(thetree42,26,562);
        THEtree thetree43 = new THEtree();
        addObject(thetree43,13,617);
        THEtree thetree44 = new THEtree();
        addObject(thetree44,33,682);
        THEtree thetree45 = new THEtree();
        addObject(thetree45,38,747);
        THEtree thetree46 = new THEtree();
        addObject(thetree46,18,799);
        THEtree thetree47 = new THEtree();
        addObject(thetree47,22,874);
        coin coin = new coin();
        addObject(coin,749,135);
        coin coin2 = new coin();
        addObject(coin2,180,374);
        coin coin3 = new coin();
        addObject(coin3,684,741);
        coin coin4 = new coin();
        addObject(coin4,261,639);
        coin coin5 = new coin();
        addObject(coin5,372,118);
        Othertestenemy othertestenemy = new Othertestenemy();
        addObject(othertestenemy,673,295);
        Othertestenemy othertestenemy2 = new Othertestenemy();
        addObject(othertestenemy2,310,562);
        othertestenemy2.setLocation(139,201);
        othertestenemy.setLocation(739,591);
        Othertestenemy othertestenemy3 = new Othertestenemy();
        addObject(othertestenemy3,231,527);
        mainchar mainchar = new mainchar();
        addObject(mainchar,261,639);
        coin coin6 = new coin();
        addObject(coin6,105,54);
        coin coin7 = new coin();
        addObject(coin7,501,252);
        coin coin8 = new coin();
        addObject(coin8,606,516);
        Othertestenemy othertestenemy4 = new Othertestenemy();
        addObject(othertestenemy4,465,80);
        Othertestenemy othertestenemy5 = new Othertestenemy();
        addObject(othertestenemy5,480,783);
        mainchar.setLocation(258,643);
        mainchar.setLocation(461,494);
        mainchar.setLocation(451,431);
        Healthbar healthbar = new Healthbar();
        addObject(healthbar,175,45);
        healthbar.setLocation(113,26);
        removeObject(healthbar);
    }
}
The other actors have no code regarding the health or game over.
danpost danpost

2018/1/4

#
Where are you calling hitOthertestenemy to execute it from?
xbLank xbLank

2018/1/4

#
danpost wrote...
Where are you calling hitOthertestenemy to execute it from?
He is not. And worst of all: He never realized because his code is a mess. Please work on your style.
danpost danpost

2018/1/4

#
일리아스 wrote...
his code is a mess. Please work on your style.
It really is not bad. Just needs to press Ctrl-I to fix indentation. There may be one or two squiggly brackets that are not consistent with the rest of the code, but otherwise it is quite good.
You need to login to post a reply.