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
danpost danpost

2020/7/9

#
If "void" is what you named a class, rename it. Remove lines 28 thru 30 in ball1 class.
Taeyo Taeyo

2020/7/9

#
now the ball moves only on top and bottom and it gets stuck at the corner of edge
danpost danpost

2020/7/9

#
Taeyo wrote...
now the ball moves only on top and bottom and it gets stuck at the corner of edge
Does your scenario actually run with this code? I had unintentionally made an error of omission in the wrapEdges method that has yet to be corrected. As such, your scenario should not be able to run. I believe an earlier discussion thread had shown similar behavior. Maybe you need to re-install greenfoot (or Java?) as a lot of errors in your coding (if not all) seem to be going unchecked. The omission was at line 37, which should be:
// this
if (getX() == 0 && r > 90 && r < 270) setLocation(getWorld().getWidth()-1, getY());
The ", getY()" part was missed.
Taeyo Taeyo

2020/7/9

#
public void(){
int x,y;
if(ball.ycoordinate>y)
    {
        setLocation(x , y + 5);
}
    if(ball.Ycoordinate<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(getY(),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

#
i did added the value but it didn't work the ball only moves at bottom and top it doesn't bounce randomly
danpost danpost

2020/7/9

#
You put the ", getY()" in at the wrong place. I think you need to be more specific on what you want. Give what part of the bounce you want randomized, where the bounces can occur (if this is not the random part) and any other details you can think of.
Taeyo Taeyo

2020/7/9

#
the player moves vertically towards the ball and rebounds when it hits the players .the ball is in random direction when it hits the edge and player it bounce back and when it hits the edge i want the ball to appear from opposite direction moving randomly and where should I place getY() I placed here
if (getX() == 0 && r > 90 && r < 270) setLocation(getY(),getWorld().getWidth()-1);
beacause it showed error at setLocation
danpost danpost

2020/7/9

#
Taeyo wrote...
where should I place getY() I placed here
if (getX() == 0 && r > 90 && r < 270) setLocation(getY(),getWorld().getWidth()-1);
beacause it showed error at setLocation
I gave the proper code for that line a couple posts back.
the player moves vertically towards the ball and rebounds when it hits the players .the ball is in random direction when it hits the edge and player it bounce back and when it hits the edge i want the ball to appear from opposite direction moving randomly
The ball is to always appear on opposite edge and modify its direction by a random amount when hitting edge. Is that what you want?
Taeyo Taeyo

2020/7/9

#
yeahh
danpost danpost

2020/7/9

#
Taeyo wrote...
yeahh
It is going to take quite a bit of changing of your code. Currently trying to formulate the easiest way to proceed. Have patience with me.
danpost danpost

2020/7/9

#
Try this:
public class ball1 extends Actor
{
    public void act()
    {
        move(10);
        wrapEdges();
        bounce();
    }
    
    private void wrapEdges()
    {
        boolean hitEdge = false;
        int  r = getRotation();
        if (getX() == getWorld().getWidth()-1 && (r < 90 || r > 270))
        {
            setLocation(0, getY();
            hitEdge = true;
        }
        if (getX() == 0 && r > 90 && r < 270)
        {
            setLocation(getWorld().getWidth()-1, getY());
            hitEdge = true;
        }
        if (getY() == getWorld().getHeight()-1 && r > 0 && r < 180)
        {
            setLocation(getX(), 0);
            hitEdge = true;
        }
        if (getY() == 0 && r > 180)
        {
            setLocation(getX(), getWorld().getHeight()-1);
            hitEdge = true;
        }
        if (hitEdge) turn(Greenfoot.getRandomNumber(90)-45);
    }
    
    private void bounce()
    {
        if (isTouching(player1.class) || isTouching(player2.class))
        {
            turn(Greenfoot.getRandomNumber(180)+90);
        }
    }
}
Taeyo Taeyo

2020/7/9

#
Thankyou! it worked but sometimes it moves smoothly and again it gets stuck with a player for couple of secs and bounces back how can I slove that?
danpost danpost

2020/7/9

#
Taeyo wrote...
Thankyou! it worked but sometimes it moves smoothly and again it gets stuck with a player for couple of secs and bounces back how can I slove that?
Try adding:
move(-10);
before the turn on line 41.
Taeyo Taeyo

2020/7/9

#
Thanks! :)
dharasubham dharasubham

2020/7/16

#
To print game over screen, try this in the world: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import greenfoot.*;  
import java.util.*;
public void act(){
         printYouLose();
    }



public void printYouLose(){
        UserInfo myInfo = UserInfo.getMyInfo();
        Class c = Bear.class;
        List<Bear> lst=getObjects(c);
       
        if(lst.size()==0){
            //World myWorld = getWorld();
            showText(myInfo.getUserImage()+""+myInfo.getUserName() +" you have been died by a rock",getWidth()/2,getHeight()/2);
            Greenfoot.stop();
        }
        
    }
Its my code you can show the text another way. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ And to bounce the ball from a player type this: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private void bounce()
    {
        if (isTouching(player1.class) || isTouching(player2.class))
        {
            turn(Greenfoot.getRandomNumber(180));
            move(-5);// I typed -5 you can type any number of speed.
            //you can also type 5.
        }
    }
}
There are more replies on the next page.
1
2
3