Hello. I need help with score. Now i have counter in my game, it's working but i din't know how to make when i get like 20 score game set next World.
I use this lesson from youtube: https://www.youtube.com/watch?v=KVQU3sTSR7k
Ball:
World:
Counter:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* The ball of the game. It moves and bounces off the walls and the paddle.
*
* @author mik
* @version 1.0
*/
public class Ball extends Actor
{
private int deltaX; // x movement speed
private int deltaY; // y movement speed
private boolean stuck = true; // stuck to paddle
public Ball()
{
}
/**
* Act. Move if we're not stuck.
*/
public void act()
{
if (!stuck)
{
move();
checkOut();
if (getWorld() == null) return;
checkBlock();
}
}
/**
* Move the ball. Then check what we've hit.
*/
public void move()
{
setLocation (getX() + deltaX, getY() + deltaY);
checkPaddle();
checkWalls();
}
/**
* Check whether we've hit one of the three walls. Reverse direction if necessary.
*/
private void checkWalls()
{
if (getX() == 0 || getX() == getWorld().getWidth()-1) {
deltaX = -deltaX;
}
if (getY() == 0) {
deltaY = -deltaY;
}
}
private void checkBlock()
{
Actor block = getOneIntersectingObject(Block.class);
if(block != null)
{
getWorld().removeObject(block);
deltaY=-deltaY;
Board myBoard = (Board) getWorld();
Counter counter = myBoard.getCounter();
counter.bumpCount(2);
}
}
/**
* Check whether we're out (bottom of screen).
*/
private void checkOut()
{
if (getY() == getWorld().getHeight()-1) {
((Board) getWorld()).ballIsOut();
getWorld().removeObject(this);
}
}
private void checkPaddle()
{
Actor paddle = getOneIntersectingObject(Paddle.class);
if (paddle != null) {
deltaY = -deltaY;
int offset = getX() - paddle.getX();
deltaX = deltaX + (offset/10);
if (deltaX > 7) {
deltaX = 7;
}
if (deltaX < -7) {
deltaX = -7;
}
}
}
/**
* Move the ball a given distance sideways.
*/
public void move(int dist)
{
setLocation (getX() + dist, getY());
}
/**
* Release the ball from the paddle.
*/
public void release()
{
deltaX = Greenfoot.getRandomNumber(11) - 5;
deltaY = -5;
stuck = false;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* The game board. The board had a paddle that can move.
*
* @author mik
* @version 1.0
*/
public class Board extends World
{
private static final int GAP = 12;
private Paddle paddle;
private Counter theCounter;
/**
* Constructor for objects of class Board.
*
*/
public Board()
{
super(460, 460, 1);
setPaintOrder (Ball.class);
theCounter = new Counter();
addObject(theCounter , 34, 449);
paddle = new Paddle();
addObject ( paddle, getWidth() / 2, getHeight() - 40);
createBlocks();
}
public Counter getCounter()
{
return theCounter;
}
/**
* Create the blocks at the top of the world.
*/
private void createBlocks()
{
int y = 30;
while ( y <= 94 ) {
createRow(y);
y = y + 20 + GAP;
}
}
private void createRow(int y)
{
int x = 50;
while ( x < 460 )
{
addObject( new Block(), x, y);
x = x + 60 + GAP;
}
}
public void ballIsOut()
{
paddle.newBall();
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Counter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Counter extends Actor
{
private int totalCount = 0;
public Counter()
{
setImage(new GreenfootImage("Taskai:", 20, Color.WHITE, Color.BLACK));
}
public void bumpCount(int amount)
{
totalCount += amount;
setImage(new GreenfootImage("Taskai: " + totalCount, 20, Color.WHITE, Color.BLACK));
}
}
