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

2021/1/20

Heads or Tails game

Tycr Tycr

2021/1/20

#
I have a game idea but i am unsure what to type for any code. you have to choose between heads or tails. you have three lives or have to get 5 wins for the game to end. there is also a png of both head side and tails side of a coin in the middle of the screen depending on what the generator lands on
danpost danpost

2021/1/20

#
How do you want the player to choose between heads and tails?
Tycr Tycr

2021/1/20

#
danpost wrote...
How do you want the player to choose between heads and tails?
They click head.png or tails.png. i mainly need help setting up the getRandomNumber method with if statements
danpost danpost

2021/1/20

#
Tycr wrote...
They click head.png or tails.png. i mainly need help setting up the getRandomNumber method with if statements
You only have two choices, so use getRandomNumber(2) It will return either 0 or 1.
Tycr Tycr

2021/1/21

#
danpost wrote...
Tycr wrote...
They click head.png or tails.png. i mainly need help setting up the getRandomNumber method with if statements
You only have two choices, so use getRandomNumber(2) It will return either 0 or 1.
I so i got it to add score and lose health but, it doesnt stop, when it goes to the game screen it doesnt allow me to choose either heads or tails as the coin just pops up and seems that it doesnt change when i press either one
GreenfootImage Tails1 = new GreenfootImage("Tails1.png");
    GreenfootImage Tails2 = new GreenfootImage("Tails2.png");
    private int flipCoin = Greenfoot.getRandomNumber(2);
    /**
     * Act - do whatever the Tails wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        mouseHover();
        generate();
    }    
 
    private void generate()
    {
            if (flipCoin == 0)
            {
               getWorld().addObject(new HeadsCoin(),500,250);
               World world = getWorld();
               gameWorld myWorld = (gameWorld)world;
               HealthBar healthbar = myWorld.getHealthBar();
               healthbar.loseHealth();
            }
            if (flipCoin == 1)
            {
               getWorld().addObject(new TailsCoin(),500,250);
               World world = getWorld();
               gameWorld myWorld = (gameWorld)world;
               Counter counter = myWorld.getCounter();
               counter.addScore();
            }
    }
danpost danpost

2021/1/22

#
Line 3 does not execute but once (because of where it is). However, checking for clicks and executing codes for the button clicks would be better placed in your world. Framework of the world might be something like:
public class GameWorld extends World
{
    Actor btnFlip = new Button("Flip");
    Actor hiLiter = new HiLiter();
    Actor btnHeads = new Button("Heads");
    Actor btnTails = new Button("Tails");
    Counter counter = new Counter();
    HealthBar healthbar = new HealthBar();
    int choice;
    
    public GameWorld()
    {
        super(600, 400, 1);
        addObject(counter, 80, 100);
        addObject(healthbar, 80, 500);
        addObject(btnFlip, 300, 300);
        addObject(hiLiter, 120, 200);
        addObject(btnHeads, 120, 200);
        addObject(btnTails, 480, 200);
    }
    
    public void act()
    {
        if (Greenfoot.mouseClicked(btnFlip))
        {
            removeObjects(getObjects(HeadsCoin.class));
            removeObjects(getObjects(TailsCoin.class));
            int flipped = Greenfoot.getRandomNumber(2);
            Actor coin = flipped == 0 ? new HeadsCoin() : new TailsCoin();
            addObject(coin, 300, 100);
            if (flipped == choice) counter.addScore(); else healthbar.loseHealth();
        }
        if (Greeenfoot.mouseClicked(btnHeads))
        {
            hiLiter.setLocation(120, 200);
            choice = 0;
        }
        if (Greenfoot.mouseClickeΦ(btnTails))
        {
            hiLiter.setLocation(480, 200);
            choice = 1;
        }
    }
}
Tycr Tycr

2021/1/22

#
danpost wrote...
Tycr wrote...
They click head.png or tails.png. i mainly need help setting up the getRandomNumber method with if statements
You only have two choices, so use getRandomNumber(2) It will return either 0 or 1.
Dan, when you anwer back do you think i can send the game file to you? there are a lot more problems i have and im stressing out because this game is due tomorrow my email t.glass726@outlook.com message me on there and i will send the file to you ASAP
danpost danpost

2021/1/22

#
Tycr wrote...
Dan, when you anwer back do you think i can send the game file to you? there are a lot more problems i have and im stressing out because this game is due tomorrow
If your scenario compiles, you can upload it on this site with source included. Otherwise, you will need to post your codes within this discussion.
Tycr Tycr

2021/1/22

#
danpost wrote...
Tycr wrote...
Dan, when you anwer back do you think i can send the game file to you? there are a lot more problems i have and im stressing out because this game is due tomorrow
If your scenario compiles, you can upload it on this site with source included. Otherwise, you will need to post your codes within this discussion.
https://www.greenfoot.org/scenarios/27252 ^The game as you can see, it runs smoothly up until you need to choose between heads or tails... the screen shouldn't even have a coin yet and allow you to choose. Instead the coin pops up and the score goes up fast and the healthBar goes down
danpost danpost

2021/1/22

#
Tycr wrote...
https://www.greenfoot.org/scenarios/27252
You need to upload it again (update it) -- this time, just make sure to check the "Publish source code" box.
You need to login to post a reply.