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

2018/6/5

How to make my game End?

Kuhakuu1428 Kuhakuu1428

2018/6/5

#
I have been trying to make my game end with my code, but i don't know why it can't end while the Main character is touching the enemy more than 10 times...
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class GameEnd here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GameOver extends Actor
{
    private int HP = 10;
    /**
     * Act - do whatever the GameEnd wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        GameOver();
        if (HP == 0)
        {
            Greenfoot.stop();
        }
    }    
    
    public int GameOver()
    {
        Actor Item1 = getOneIntersectingObject(Item_1.class);
        if (Item1 != null ) 
        {
            HP += 1;   
        }
        Actor Item2 = getOneIntersectingObject(Item_2.class);
        if(Item2 != null)
        {
            HP += 0.5; 
        }
        
        Actor Enemy1 = getOneIntersectingObject(Enemy_1.class);
        Actor Enemy2 = getOneIntersectingObject(Enemy_2.class);
        Actor Enemy3 = getOneIntersectingObject(Enemy_3.class);
        if (Enemy1 != null) {   
            HP -= 1; 
        }
        if (Enemy2 != null)
        {
            HP -= 1; 
        }
        if (Enemy3 != null)
        {
            HP -= 1;     
        }
        return HP;
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MainCharacter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MainCharacter extends Actor
{
    private boolean spaceDown;
    //private int Score = 10;
    /**
     * Act - do whatever the MainCharacter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        int speed = 3;
        checkFire();
        
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY() - speed);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY() + speed);
        }
        if(Greenfoot.isKeyDown("left"))
        {  
            setLocation(getX() - speed, getY());
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+ speed, getY());
        }
        
        Actor enemy_1 = getOneIntersectingObject(Enemy_1.class);
        if(enemy_1 != null) {
            getWorld().removeObject(enemy_1);
        }
        Actor enemy_2 = getOneIntersectingObject(Enemy_2.class);
        if(enemy_2 != null) {
            getWorld().removeObject(enemy_2);
        }
        Actor enemy_3 = getOneIntersectingObject(Enemy_3.class);
        if(enemy_3 != null) {
            getWorld().removeObject(enemy_3);
        }
        Actor Item_1 = getOneIntersectingObject(Item_1.class);
        if(Item_1 != null) {
            getWorld().removeObject(Item_1);
        }
        Actor Item_2 = getOneIntersectingObject(Item_2.class);
        if(Item_2 != null) {
            getWorld().removeObject(Item_2);
        }
        
        /*Actor Item1 = getOneIntersectingObject(Item_1.class);
        if (Item1 != null ) 
        {
            Score += 1;   
        }
        Actor Item2 = getOneIntersectingObject(Item_2.class);
        if(Item2 != null)
        {
            Score += 0.5; 
        }
        
        Actor Enemy1 = getOneIntersectingObject(Enemy_1.class);
        Actor Enemy2 = getOneIntersectingObject(Enemy_2.class);
        Actor Enemy3 = getOneIntersectingObject(Enemy_3.class);
        if (Enemy1 != null||Enemy2 != null||Enemy3 != null) {   
            Score -= 1; 
        }
        if (Score == 0)
        {
            Greenfoot.stop();
        }*/
        
    } 
    
    public void checkFire()
    {
       
        if(!spaceDown && Greenfoot.isKeyDown("space")) 
        {
            spaceDown = true;
            getWorld().addObject(new Bullet(), getX(), getY());
        }
        if (spaceDown && !Greenfoot.isKeyDown("space"))
        {
            spaceDown = false;
        }
    }
}
danpost danpost

2018/6/5

#
Your GameOver object is not what has health (or health points) -- it is your MainCharacter object that does. Also, it is not the GameOver object that will intersect the items and enemies. There should be practically nothing added into your GameOver class (it has no actions as the scenario should be stopped; it only has an image, which is probably a default one). A GameOver object should only be created and added into the world when the HP (or Score) of MainCharacter reaches zero. The commented code (lines 60 through 80 of the MainCharacter class) is on the right track. The only issue I see there is that since you are not removing touching enemies, the health (or score) will plummet quickly (about 1/6th of a second touching an enemy will deplete health to zero). You will need to regulate the drop in health better there.
Kuhakuu1428 Kuhakuu1428

2018/6/5

#
I understand, thank you very much :)
You need to login to post a reply.