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);
}
}
