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

2014/12/5

How to Display Image for GameOver/YouWin

1
2
3
Anne2403 Anne2403

2014/12/5

#
Hello. Uhmm.. I edited an image for when the player killed all the enemies and an image for when the timer is up or the cat was shooted by the enemy.. But I don't know what code am I gonna use to work that out. could someone help me please. This image is for the you win...[Disallowed URL]
danpost danpost

2014/12/5

#
You can create a simple GameOver class that extends Actor and have it sets its image at the time of creation to whichever one was given it.
1
2
3
4
5
6
7
8
9
import greenfoot.*;
 
public class GameOver extends Actor
{
    public GameOver(String imageFilename)
    {
        setImage(new GreenfootImage(imageFilename));
    }
}
If the player kills all enemies, create a 'new GameOver("winner.png")' object and add it to the world. If the player is shot or time expires, create a 'new GameOver("loser.png")' object and add it to the world. Of course, you will have to adjust the filenames to match what you have (or you could rename the files to match the code).
danpost danpost

2014/12/5

#
As an alternative, you can create a new subclass of World for each ( 'WinnerWorld' and 'LoserWorld' ), set the default images of both and then 'Greenfoot.setWorld(new WinnerWorld())' (or 'LoserWorld') to display the appropriate images.
vexqa vexqa

2014/12/5

#
You could make both images an actor, and add the actor to the world when the event happens. Example:
1
2
3
World w;
w = getWorld();
w.addObject(new ACTORNAME, x, y);
Anne2403 Anne2403

2014/12/5

#
@danpost and @vexqa Thank u so much. :)
Anne2403 Anne2403

2014/12/6

#
ahmm.. there is a problem... almost done but... the 'isEmpty()'--cannot fin symbol. why? COuld u please correct me so that I can make it right. It is the last thing to finish.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * Write a description of class YouWin here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class YouWin extends Actor
{
    /**
     * Act - do whatever the YouWin wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
public void act()
{
       hitEnemies();  
      isEmpty();
}  
public void hitEnemies()
    {
        if(!Snake.isEmpty()){
        Actor Snake = (Actor) Lightning.get(0);
    }
}
private void isEmpty()
{
    if(Snake instanceof Ligthning){
        Greenfoot.setWorld(Winner());
    }
}
}
danpost danpost

2014/12/6

#
Here is what I see in the code: * If you have a class called Snake, then line 22 will have the compiler look in the Snake class for the 'isEmpty' method; if it cannot find it there, it should create an error message 'cannot find method 'isEmpty'. *line 23 uses the List object 'get' method on the Lightning class; but a class name is not a List object. *line 23 defines and assigns (or tries to) a value to a variable called Snake which is immediately lost as the method ends. *line 28 should create an error message 'cannot find variable Snake' as the variable is not defined in this method. *line 29 should create an error message 'cannot find method Winner' as there is no method in this class with that name. * It appears this is possibly some sort of check for winning the game, but that fact that it is an Actor subclass called 'YouWin' is puzzling as you would not have a 'YouWin' actor in the world before the game was decidedly won (I would think). You should explain when this actor is placed into the world and what each method is supposed to be doing (in detail). Actually, I am not sure it should be an actor at all and possibly the code within it, after a complete overhaul, should be elsewhere.
Anne2403 Anne2403

2014/12/8

#
uhm.. I change the code from above code @danpost but it didn't work, I've been tryng and tryng different codes but it didn't work.. This is the code that I have now..
1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.List;
import greenfoot.*;
 
public class Snake extends Actor
{
//code for shoot() method
public void act()
{
if(!getNeighbours(45,true,Snake.class).isEmpty())
{
Snake snake =(Snake) getNeighbours(45,true,Snake.class).get(0);
World world = new Space();
setImage(new GreenfootImage("AWESOME.JPG));
You see, the last line is what I want to show when I hit/shooted all the animals.. The image "AWESOME.JPG" Serves as my 'YouWin'. Is there's something that I to change or do to do what I want to happen in my game??
Anne2403 Anne2403

2014/12/8

#
and ow I forgot, the code above has no syntax error but it didn't do anything to make what I want possible.
danpost danpost

2014/12/8

#
You should really explain what class represents the player, the enemy, etc. Confused about these: (1) why a snake would look for another snake (lines 9 and 11) (2) why a new world is created on line 12 followed by changing the image of the snake to the AWESOME image (not the neighbor, but the one that looked for the neighbor) (3) why line 6 calls this 'code for shoot() method'
Anne2403 Anne2403

2014/12/8

#
okay. uhmm. For the player, it is the Cat. For enemy, is the Snake and the other enemy is the subclass of Snake. and about the things u are confused about... 1) That's what it is, I just got that code here in greenfoot when I search in google. hehe.. 2)It is my idea, I just thought that if i put that code the 'AWESOME' IMAGE WILL ADD TO THE WORLD ONCE i killed all the enemy. Am I wrong about that? 3)About that, what I mean by that is, there's a code be4 the act()method that I used to make the. shoot()method possible for the enemy class.
Anne2403 Anne2403

2014/12/8

#
and for more information about my other class well, I have Fire(int rotation)---->Enemies'sWeapon, Lightning()---->Cat's/PlayerWeapon,Timer,GameOver() and yeah the Snake class and the subclass is the other enemies. Likp, public class Snake extends Owl and so on... the only missing to complete and finish my game is the sounds and yeah the 'youwin' image and the write place or the class for removing the fire and lightning from the world after it was shoot by the enemy or the cat.
danpost danpost

2014/12/8

#
(1) I still do not know why you would want a snake to look for another snake. (2) 'setImage' is not a World class method and will not set the image of the world. (3)Ok. (more) an owl is not a snake -- why would you have the Owl class extend the Snake class? if they are both enemies, then maybe they should both extend an Enemy class.
Anne2403 Anne2403

2014/12/8

#
okay I will just change my code. Thanks.
Anne2403 Anne2403

2014/12/9

#
uhmm @danpost sorry If I am always asking for help.. I already change the previous code.. I made a world class called 'Winner' and this is my code for it...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import greenfoot.*;
 
public class Winner extends World
 
public Winner()
{
super(990,790,1)
}
public void act()
{
if(getObjects(Enemies.class).isEmpty())
{
Greenfoot.setWorld(new Winner());
}
but the thing that I am confusing about is.. how am I gonna able to make that code excute?? I am attempting to put that code in my lightning actor class but everytime I tried different code I got different errors. Things that i tried but got errors? I tried.. putting an 'else' in the kill method in the lightning class.(The kill method excuted to kill the enemies once they got hit by the player).
1
2
3
else {
 getWorld( new Winner()):
}
(But it didn't do what I want.) . I tried too, in the lightning class aagain..
1
2
3
4
ublic java.util.List getObjects(java.lang.Class Winner)
{
return;
}
(go t an error) and not really sure what am I gonna put in the 'return' statement.
There are more replies on the next page.
1
2
3