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

2021/1/27

Game Over Screen :(

kingCaspian kingCaspian

2021/1/27

#
I have been trying to make a game over screen but all my attempts keep failing. could you give me a few tips?
1
2
3
4
5
6
7
8
Actor Crab;
Crab = getOneObjectAtOffset(0,0, Crab.class);
if (Crab != null)
    {       
    World world = getWorld();
    world.removeObjects(world.getObjects(null));
    world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2);
    Greenfoot.stop();
danpost danpost

2021/1/27

#
kingCaspian wrote...
I have been trying to make a game over screen but all my attempts keep failing. could you give me a few tips? << Code Omitted >>
Please show GameOver class codes.
kingCaspian kingCaspian

2021/1/27

#
1
2
3
4
5
6
7
public class GameOver extends Actor
{
        public void act()
    {
        // Add your action code here.
    }   
}
danpost danpost

2021/1/27

#
kingCaspian wrote...
<<Code Omitted >>
It all looks okay, so far. Are you getting any errors? If so, what are they, in detail?
kingCaspian kingCaspian

2021/1/27

#
well, when I run it and my character gets eaten, the game over screen does not pop up and the game itself keeps running.
danpost danpost

2021/1/27

#
kingCaspian wrote...
well, when I run it and my character gets eaten, the game over screen does not pop up and the game itself keeps running.
Please show entire class initial code given is in.
kingCaspian kingCaspian

2021/1/27

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
public class Crab extends Actor
{
    public void act()
    {
        MoveandTurn();
        eat();
    }
    public void MoveandTurn()
{
move(4);
if (Greenfoot.isKeyDown("left"))
        {
            turn(-3);
    }   
if (Greenfoot.isKeyDown("right"))
        {
            turn(3);
        }
    }
    public void eat()
    {
Actor food;
food = getOneObjectAtOffset(0, 0, food.class);
if (food != null)
{
    World world;
    world = getWorld();
    world.removeObject(food);
    }
Actor Crab;
Crab = getOneObjectAtOffset(0,0, Crab.class);
if (Crab != null)
    {       
    World world = getWorld();
    world.removeObjects(world.getObjects(null));
    world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2);
    Greenfoot.stop();
 
        }
}
}
danpost danpost

2021/1/27

#
Herein lies the problem. You have this (starting line 33) -- "if crab touches middle of crab, then end game." Your enemy is not involved as coded.
kingCaspian kingCaspian

2021/1/27

#
What should I replace it with to make it work?
danpost danpost

2021/1/27

#
kingCaspian wrote...
What should I replace it with to make it work?
What is your enemy called?
kingCaspian kingCaspian

2021/1/27

#
It is called "poison" and here is its code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class poison extends Actor
{
    public void act()
    {
        move(4);
        if (Greenfoot.getRandomNumber(100) < 10)
        {
            turn(Greenfoot.getRandomNumber(90) - 45);
        }
            if (getX() <5 || getX() >= getWorld().getWidth() - 5)
            {
                turn(180);
            }
              if (getY() <= 5 || getY() >= getWorld().getHeight() -5)
              {
                  turn(180);
}
Actor Crab;
Crab = getOneObjectAtOffset(0, 0, Crab.class);
if (Crab != null)
{
    World world;
    world = getWorld();
    world.removeObject(Crab);
    Greenfoot.playSound("eating.wav");
}
}
}
danpost danpost

2021/1/27

#
Remove lines 18 thru 26 in poison class. Replace lines 32 thru 34 in Crab class with:
1
2
3
Actor poison;
poison = getOneObjectAtOffset(0, 0, poison.class);
if (poison != null)
kingCaspian kingCaspian

2021/1/27

#
It worked! Thank you for the help.
You need to login to post a reply.