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

2020/7/8

how to make gameover on the screen

1
2
3
Taeyo Taeyo

2020/7/8

#
does anyone know how to make gameover on the screen when the player 1 score 10 and player2 score 20 and how to disappear and appear objects from opposite side
danpost danpost

2020/7/8

#
Taeyo wrote...
does anyone know how to make gameover on the screen when the player 1 score 10 and player2 score 20
Show what you have tried (informing where).
and how to disappear and appear objects from opposite side
Show class code of actor you want to do this to.
Taeyo Taeyo

2020/7/8

#
public class ball1 extends Actor { /** * Act - do whatever the ball1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public int direction = 10; public void act() { move(direction); GameOver(); Actor player_1 = getOneIntersectingObject(player1.class); if (player_1!=null) { if(isTouching(player1.class)) { turn(90+Greenfoot.getRandomNumber(180)); World myWorld = getWorld(); MyWorld myworld = (MyWorld)myWorld; Counter1 counter1 = myworld.getCounter1(); counter1.addScore(1); } } Actor player_2= getOneIntersectingObject(player2.class); if (player_2!=null) { if(isTouching(player2.class)) { turn(90+Greenfoot.getRandomNumber(180)); World myWorld = getWorld(); MyWorld myworld = (MyWorld)myWorld; Counter2 counter2 = myworld.getCounter2(); Counter2.addScore(1); } } if(isAtEdge()){ turn(90+Greenfoot.getRandomNumber(180)); } } public void GameOver() { if(getY() >= 599){ getWorld().addObject(new GameOver(),400,300); } } }
danpost danpost

2020/7/8

#
Remove "if (isAtEdge)" block at end of act method. Add the following method to code:
private void wrapEdges()
{
    int r = getRotation();
    if (getX() == getWorld().getWidth()-1 && (r > 270 || r < 90)) setLocation(0, getY());
    if (getX() == 0 && r > 90 && r < 270) setLocation(getWorld().getWidth()-1);
    if (getY() == getWorld().getHeight()-1 && r < 180) setLocation(getX(), 0);
    if (getY() == 0 && r > 180) setLocation(getX(), getWorld().getHeight()-1);
}
After "move(direction);" in act, add the following line:
wrapEdges();
Taeyo Taeyo

2020/7/8

#
thanks it worked but now my ball doesn't move randomly and can you explain how to add gameover on the screen
Taeyo Taeyo

2020/7/8

#
the ball didn't move randomly so I added isAtEdge but the ball disappear and appear at the same location for multiple time
danpost danpost

2020/7/9

#
Taeyo wrote...
the ball didn't move randomly so I added isAtEdge but the ball disappear and appear at the same location for multiple time
For some randomness, you can add something like:
if (Greenfoot.getRandomNumber(180) == 0) turn (Greenfoot.getRandomNumber(180)-90);
Taeyo wrote...
can you explain how to add gameover on the screen
One thing at a time.
Taeyo Taeyo

2020/7/9

#
it worked but appears at the same loacation for multiple times
danpost danpost

2020/7/9

#
Taeyo wrote...
it worked but appears at the same loacation for multiple times
Show current class code.
Taeyo Taeyo

2020/7/9

#
Actor player_1 = getOneIntersectingObject(player1.class);
        if (player_1!=null)
        {
        if(isTouching(player1.class))
        {
            turn(90+Greenfoot.getRandomNumber(180));\
    }
}
        Actor player_2= getOneIntersectingObject(player2.class);
        if (player_2!=null)
        {
        if(isTouching(player2.class)) 
        {
            turn(90+Greenfoot.getRandomNumber(180));
        }
    }
        if(isAtEdge()){
            turn(90+Greenfoot.getRandomNumber(180));
        }
    }
private void wrapEdges()
{
    int r = getRotation();
if (Greenfoot.getRandomNumber(180) == 0) turn (Greenfoot.getRandomNumber(180)-90);
    if (getX() == getWorld().getWidth()-1 && (r > 270 || r < 90)) setLocation(0, getY());
    if (getX() == 0 && r > 90 && r < 270) setLocation(getWorld().getWidth()-1);
    if (getY() == getWorld().getHeight()-1 && r < 180) setLocation(getX(), 0);
    if (getY() == 0 && r > 180) setLocation(getX(), getWorld().getHeight()-1);
}

 
Taeyo Taeyo

2020/7/9

#
actor class \
    if(ball.ycoordinate>y)
    {
        setLocation(x , y + 5);
}
    if(ball.xcoordinate<y)[quote]actor class[/quote]

    {
        setLocation(x, y - 5);
    }
}
danpost danpost

2020/7/9

#
The codes given is a bit confusing. Please show entire class at once.
Taeyo Taeyo

2020/7/9

#
the class codeof player1 and player 2 which moves vertically with an object
public void(){
int x,y;
if(ball.ycoordinate>y)
    {
        setLocation(x , y + 5);
}
    if(ball.xcoordinate<y)[quote]actor class[/quote]
 
    {
        setLocation(x, y - 5);
    }
}
the class code of ball
Actor player_1 = getOneIntersectingObject(player1.class);
        if (player_1!=null)
        {
        if(isTouching(player1.class))
        {
            turn(90+Greenfoot.getRandomNumber(180));\
    }
}
        Actor player_2= getOneIntersectingObject(player2.class);
        if (player_2!=null)
        {
        if(isTouching(player2.class)) 
        {
            turn(90+Greenfoot.getRandomNumber(180));
        }
    }
        if(isAtEdge()){
            turn(90+Greenfoot.getRandomNumber(180));
        }
    }
private void wrapEdges()
{
    int r = getRotation();
if (Greenfoot.getRandomNumber(180) == 0) turn (Greenfoot.getRandomNumber(180)-90);
    if (getX() == getWorld().getWidth()-1 && (r > 270 || r < 90)) setLocation(0, getY());
    if (getX() == 0 && r > 90 && r < 270) setLocation(getWorld().getWidth()-1);
    if (getY() == getWorld().getHeight()-1 && r < 180) setLocation(getX(), 0);
    if (getY() == 0 && r > 180) setLocation(getX(), getWorld().getHeight()-1);
}
danpost danpost

2020/7/9

#
No. Show entire class. Show from the first import statement at top, down to the last character, '}', at the bottom.
Taeyo Taeyo

2020/7/9

#
public void(){
int x,y;
if(ball.ycoordinate>y)
    {
        setLocation(x , y + 5);
}
    if(ball.xcoordinate<y)[quote]actor class[/quote]
  
    {
        setLocation(x, y - 5);
    }
}
public class ball1 extends Actor
{
    /**
     * Act - do whatever the ball1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int direction = 10;
    public void act() 
    {
        move(direction);
        wrapEdges();
Actor player_1 = getOneIntersectingObject(player1.class);
        if (player_1!=null)
        {
        if(isTouching(player1.class))
        {
            turn(90+Greenfoot.getRandomNumber(180));\
    }
}
        Actor player_2= getOneIntersectingObject(player2.class);
        if (player_2!=null)
        {
        if(isTouching(player2.class)) 
        {
            turn(90+Greenfoot.getRandomNumber(180));
        }
    }
        if(isAtEdge()){
            turn(90+Greenfoot.getRandomNumber(180));
        }
    }
private void wrapEdges()
{
    int r = getRotation();
if (Greenfoot.getRandomNumber(180) == 0) turn (Greenfoot.getRandomNumber(180)-90);
    if (getX() == getWorld().getWidth()-1 && (r > 270 || r < 90)) setLocation(0, getY());
    if (getX() == 0 && r > 90 && r < 270) setLocation(getWorld().getWidth()-1);
    if (getY() == getWorld().getHeight()-1 && r < 180) setLocation(getX(), 0);
    if (getY() == 0 && r > 180) setLocation(getX(), getWorld().getHeight()-1);
}
There are more replies on the next page.
1
2
3