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

2019/6/11

Counter for a pong game?

awan16 awan16

2019/6/11

#
Im trying to create a counter for my simple Pong game and I imported the basic counter class from greenfoot. In all honesty I pretty much got stuck from there. I assume that I will only need my Ball and MyWorld class to make a working counter, so here they are. Thanks for the help! Here is the actually game
import greenfoot.*;  

public class Ball extends Actor
{
    private int moveHor = 5;
    private int moveVert = 0;
    private Counter leftCounter;

    public void act() 
    {
        checkWall1();
        checkWall2();
        checkHitRight();
        checkHitLeft();
        checkWinRight();
        checkWinLeft();
        move();
        leftCounter.setValue(leftCounter.getValue() + 1);
    }   

    public boolean rightWin()
    {
        Actor theBall = getOneObjectAtOffset (0, 0, WinRight.class);
        return theBall != null;
    }

    public boolean leftWin()
    {
        Actor theBall = getOneObjectAtOffset (0, 0, WinLeft.class);
        return theBall != null;
    }

    public boolean hitBottom()
    {
        Actor theBall = getOneObjectAtOffset (0, 0, BotWall.class);
        return theBall != null;
    }

    public boolean hitTop()
    {
        Actor theBall = getOneObjectAtOffset (0, 0, TopWall.class);
        return theBall != null;
    }

    public boolean hitLeft()
    {
        Actor theBall = getOneObjectAtOffset (0, 0, LeftBump.class);
        return theBall != null;
    }

    public boolean hitRight()
    {
        Actor theBall = getOneObjectAtOffset (0, 0, RightBump.class);
        return theBall != null;
    }

    public void move()
    {
        setLocation (getX() + moveHor,getY() + moveVert);
    }

    public void checkWinRight()
    {
        if (rightWin())
        {
            Greenfoot.playSound("lose.wav");
            setLocation (400,250);
            moveHor = 5;
            moveVert = 0;

            {
                Greenfoot.stop();
            }
        }

    }

    public void checkWinLeft()
    {
        if (leftWin())
        {
            Greenfoot.playSound("lose.wav");
            setLocation (400,250);
            moveHor = -5;
            moveVert = 0;

            {
                Greenfoot.stop();
            }
        }
    }

    public void checkWall1()
    {
        if (hitBottom())
        {
            moveVert = moveVert - moveVert - moveVert;
            Greenfoot.playSound("beep.wav");
        }
    }

    public void checkWall2()
    {
        if (hitTop())
        {
            moveVert = moveVert - moveVert - moveVert;
            Greenfoot.playSound("beep.wav");
        }
    }

    public void checkHitLeft()
    {
        int y = Greenfoot.getRandomNumber(5);
        if(hitLeft())
        {
            moveHor = moveHor - 1;
            moveHor = moveHor - moveHor - moveHor;
            Greenfoot.playSound("hit.wav");
            if (moveVert < 0)
            {
                moveVert = moveVert - y;
            }
            else
            {
                moveVert = moveVert - y;
            }
        }
    }

    public void checkHitRight()
    {
        int y = Greenfoot.getRandomNumber(5);
        if(hitRight())
        {
            moveHor = moveHor + 1;
            moveHor = moveHor - moveHor - moveHor;
            Greenfoot.playSound("hit.wav");
            if (moveVert < 0)
            {
                moveVert = moveVert + y;
            }
            else
            {
                moveVert = moveVert + y;
            }
        }
    }
}
import greenfoot.*;  

public class background extends World
{
    public Ball ball;
    public LeftBump lbump;
    public RightBump rbump;
    public BotWall bwall;
    public TopWall twall;
    public WinRight rwin;
    public WinLeft lwin;
    public Counter leftCounter;

    public void act()
    {
     
    }

    public background()
    {    
        super(800, 500, 1); 
        run();
    } 

    public void run()
    {
        ball = new Ball();
        addObject(ball,400,250);
        lbump = new LeftBump();
        addObject(lbump,50,250);
        rbump = new RightBump();
        addObject(rbump,750,250);
        bwall = new BotWall();
        addObject(bwall,400,500);
        twall = new TopWall();
        addObject(twall,400,0);
        
        rwin = new WinRight();
        addObject(rwin,0,250);
        lwin = new WinLeft();
        addObject(lwin,800,250);

        leftCounter = new Counter("Left Goals: ");
        addObject(leftCounter, 100, 50);
    }
}

danpost danpost

2019/6/11

#
The leftCounter field in your Ball class has issues. First, it is never assigned a Counter object -- namely, the one created and added into the world at lines 43 and 44 and referenced at line 12 in the background class. Since it is already referenced there, it might be best to remove line 7 in the Ball class and replace line 18 with the following:
Counter counter = ((background)getWorld()).leftCounter;
counter.setValue(counter.getValue()+1);
awan16 awan16

2019/6/11

#
danpost wrote...
The leftCounter field in your Ball class has issues. First, it is never assigned a Counter object -- namely, the one created and added into the world at lines 43 and 44 and referenced at line 12 in the background class. Since it is already referenced there, it might be best to remove line 7 in the Ball class and replace line 18 with the following:
Counter counter = ((background)getWorld()).leftCounter;
counter.setValue(counter.getValue()+1);
Wow that works perfectly! I just stuck it in my WinLeft method and everything works great. Thank you very much for the help!
You need to login to post a reply.