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

2017/3/24

Green foot ping pong game.

avisek avisek

2017/3/24

#
There is a wall in between 6 babies. 3 on one side and 3 on other side. there is a ball moving in random direction which bounces back when hit on edge and babies need to restrict ball to come their way and hit on edge of their side. i need a score counter on both side and declare a winner at last. when opposition misses ball and hits on edge and other team gets a point and this continues till total points becomes 5 and team scoring more score out of 5 will be declared as winner. As babies are allowed to move only on vertical direction and each baby will have different up and down controlling buttons. :)
Super_Hippo Super_Hippo

2017/3/24

#
Do not expect anyone writing you a whole game. Do it step by step, I am sure you can do some steps on your own. And then, when you are struggling with one step, ask for help. It does not improve your programming knowledge if someone is writing your whole game.
avisek avisek

2017/3/24

#
ok bro :) , So could you teach me how to bring 2 score boards on screen and declare one of team as winner ? My assignment deadline in very close.
Super_Hippo Super_Hippo

2017/3/24

#
You really shouldn't start with the scoreboards if you don't have anything else done (or do you have?).
avisek avisek

2017/3/25

#
yeah i have . my work till now is ::: ball bounces back when hit on edge of world and 2 babies crossing the wall between them. now i want code to count score for both players when they miss the ball and hit on edge and declare one of them as winner.
Super_Hippo Super_Hippo

2017/3/25

#
In your ball class, check if it reaches the left or right edge. If so, increase the counter of the corresponding player. You can import the Counter class. Then you can create a counter for each player:
//in your world

//outside methods
private Counter[] counter = new Counter[2];

//when adding objects
addObject(counter[0], 100, 50);
addObject(counter[1], 400, 50);

//method to get the counter
public Counter getCounter(int player)
{
    return counter[player];
}
Then in your ball:
public void act()
{
    //moving around
    if (getX() == 0)
    {
        ((MyWorld) getWorld()).getCounter(1).add(1);
        //set ball back to middle
    }
    else if (getX() == getWorld().getWidth()-1)
    {
        ((MyWorld) getWorld()).getCounter(0).add(1);
        //set ball back to middle
    }
}
You need to login to post a reply.