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

2017/5/2

Need help with count

skitlesas skitlesas

2017/5/2

#
Hello I need help to make count for my game. I want when "Kamuoliukas" this Ball hit "Plyta" this is Brick I get like +10 points. WORD:
import greenfoot.World;
import greenfoot.Actor;
import greenfoot.GreenfootImage;
import greenfoot.Color;
import greenfoot.*;


public class Zaidimas extends World
{
    public static int SIZEX = 700;
    public static int SIZEY = 600;
    
    public static int BRICKSPACING = 10;
    public static int BRICKLEFTBORDER = 30;
    public static int BRICKRIGHTBORDER = 30;
    public static int BRICKWIDTH = 30;
    public static int BRICKHEIGHT = 15;
    public static int BRICKTOPBORDER = 10;
    private boolean gameStarted = false; 
    
    public void act()
    {
      while (!gameStarted)
      {
        if (Greenfoot.isKeyDown("space"))
        {
            gameStarted = true;
        }
      }
    }
    
    public Zaidimas() {
        super(SIZEX, SIZEY, 1);
        scenario1();
    } 
        
    public void scenario1()
    {
        //kamuoliukas
        addObject(new Kamuoliukas(), 350, 559);
        //pirma eile
        addObject(new Plyta(), 46, 22);
        addObject(new Plyta(), 96, 22);
        addObject(new Plyta(), 146, 22);
        addObject(new Plyta(), 196, 22);
        addObject(new Plyta(), 246, 22);
        addObject(new Plyta(), 296, 22);
        addObject(new Plyta(), 346, 22);
        addObject(new Plyta(), 396, 22);
        addObject(new Plyta(), 446, 22);
        addObject(new Plyta(), 496, 22);
        addObject(new Plyta(), 546, 22);
        addObject(new Plyta(), 596, 22);
        addObject(new Plyta(), 646, 22);
        //antra eile
        addObject(new Plyta(), 46, 53);
        addObject(new Plyta(), 96, 53);
        addObject(new Plyta(), 146, 53);
        addObject(new Plyta(), 196, 53);
        addObject(new Plyta(), 246, 53);
        addObject(new Plyta(), 296, 53);
        addObject(new Plyta(), 346, 53);
        addObject(new Plyta(), 396, 53);
        addObject(new Plyta(), 446, 53);
        addObject(new Plyta(), 496, 53);
        addObject(new Plyta(), 546, 53);
        addObject(new Plyta(), 596, 53);
        addObject(new Plyta(), 646, 53);
        //trecia eile
        addObject(new Plyta(), 46, 84);
        addObject(new Plyta(), 96, 84);
        addObject(new Plyta(), 146, 84);
        addObject(new Plyta(), 196, 84);
        addObject(new Plyta(), 246, 84);
        addObject(new Plyta(), 296, 84);
        addObject(new Plyta(), 346, 84);
        addObject(new Plyta(), 396, 84);
        addObject(new Plyta(), 446, 84);
        addObject(new Plyta(), 496, 84);
        addObject(new Plyta(), 546, 84);
        addObject(new Plyta(), 596, 84);
        addObject(new Plyta(), 646, 84);
        //ketvirta eile
        addObject(new Plyta(), 46, 114);
        addObject(new Plyta(), 96, 114);
        addObject(new Plyta(), 146, 114);
        addObject(new Plyta(), 196, 114);
        addObject(new Plyta(), 246, 114);
        addObject(new Plyta(), 296, 114);
        addObject(new Plyta(), 346, 114);
        addObject(new Plyta(), 396, 114);
        addObject(new Plyta(), 446, 114);
        addObject(new Plyta(), 496, 114);
        addObject(new Plyta(), 546, 114);
        addObject(new Plyta(), 596, 114);
        addObject(new Plyta(), 646, 114);
        //Spygliai
        addObject(new spygliai(), 17, 666);
        addObject(new spygliai(), 52, 666);
        addObject(new spygliai(), 88, 666);
        addObject(new spygliai(), 124, 666);
        addObject(new spygliai(), 160, 666);
        addObject(new spygliai(), 197, 666);
        addObject(new spygliai(), 233, 666);
        addObject(new spygliai(), 269, 666);
        addObject(new spygliai(), 305, 666);
        addObject(new spygliai(), 340, 666);
        addObject(new spygliai(), 376, 666);
        addObject(new spygliai(), 412, 666);
        addObject(new spygliai(), 448, 666);
        addObject(new spygliai(), 483, 666);
        addObject(new spygliai(), 518, 666);
        addObject(new spygliai(), 553, 666);
        addObject(new spygliai(), 589, 666);
        addObject(new spygliai(), 624, 666);
        addObject(new spygliai(), 659, 666);
        addObject(new spygliai(), 695, 666);
        //Lenta
        addObject(new Lenta(), 350, SIZEY - 20); 
    }
}
Kamuoliukas this is BALL
import greenfoot.World;
import greenfoot.Actor;
import java.util.Collection;
import java.util.Iterator;
import greenfoot.Color;
import greenfoot.*;


public class Kamuoliukas extends Actor
{
    public static int BALLSIZEX = 20;
    public static int BALLSIZEY = 20;

    private int motionX;
    private int motionXdenom;
    private int motionXaccum;
    
    private int score;
    
    private int motionY;
    private int motionYdenom;
    private int motionYaccum;
    
    private int ballSpeed;

    public Kamuoliukas()
    {
        motionX = -20;
        motionXdenom = 10;
        motionY = -20;
        motionYdenom = 10;
        ballSpeed = (int) Math.sqrt(motionX * motionX + motionY * motionY);
    }
       
    public static Bounds getBounds(Actor go)
    {
        Bounds b = new Bounds();
        int width = go.getImage().getWidth();
        int height = go.getImage().getHeight();
        b.lx = go.getX() - width / 2;
        b.ty = go.getY() - height / 2;
        b.rx = b.lx + width;
        b.by = b.ty + height;
        return b;
    }
    
    public void act()
    {
        int newX = getX() + motionX / motionXdenom;
        int newY = getY() + motionY / motionYdenom;
        motionXaccum += motionX % motionXdenom;
        motionYaccum += motionY % motionYdenom;
        
        if (motionXaccum >= motionXdenom) {
            newX++;
            motionXaccum -= motionXdenom;
        }
        else if (motionXaccum < 0) {
            newX--;
            motionXaccum += motionXdenom;
        }
        
        if (motionYaccum >= motionYdenom) {
            newY++;
            motionYaccum -= motionYdenom;
        }
        else if (motionYaccum < 0) {
            newY--;
            motionYaccum += motionYdenom;
        }

        if (newX < BALLSIZEX / 2) {
            newX = BALLSIZEX / 2;
            motionX = -motionX;
        }
        else if (newX + BALLSIZEX / 2 >= Zaidimas.SIZEX) {
            newX = Zaidimas.SIZEX - BALLSIZEX / 2;
            motionX = -motionX;
        }
        
        if (newY < BALLSIZEY / 2) {
            newY = BALLSIZEY / 2;
            motionY = -motionY;
        }
        else if (newY + BALLSIZEY / 2 >= Zaidimas.SIZEY) {
            newY = Zaidimas.SIZEY - BALLSIZEY / 2;
            motionY = -motionY;
        }
        
        if (isTouching(spygliai.class))
        {
            Greenfoot.setWorld(new GameOver());
        }
        
        setLocation(newX, newY);
        int centerX = getX();
        int centerY = getY();
        
        Collection objs = getIntersectingObjects(Actor.class);
        Iterator i = objs.iterator();
        while (i.hasNext()) {
            Actor go = (Actor) i.next();
            
            //Kliutis
            if (go instanceof Kliutis) {
                Kliutis brick = (Kliutis) go;
                boolean collision = true; 
               
                Bounds brickBounds = getBounds(brick);
                if (Math.abs(brickBounds.ty - centerY) > 9 &&
                    Math.abs(brickBounds.by - centerY) > 9)
                  collision = false;
                else {
                    if (centerX >= brickBounds.lx && centerX <= brickBounds.rx) {
                        motionY = -motionY;
                    }
                    else if (centerY >= brickBounds.ty && centerY <= brickBounds.by) {
                        motionX = -motionX;
                    }
                    else {

                        int cornerX;
                        int cornerY;
                        float forceX;
                        float forceY;

                        if (centerX < brickBounds.lx)
                            cornerX = brickBounds.lx;
                        else
                            cornerX = brickBounds.rx;
                        
                        if (centerY < brickBounds.ty)
                            cornerY = brickBounds.ty;
                        else
                            cornerY = brickBounds.by;
                        
                        int collisionX = centerX - cornerX;
                        int collisionY = centerY - cornerY;
                        float collisionG = (float) collisionY / collisionX;

                        float dist = (float) Math.sqrt(collisionX * collisionX + collisionY * collisionY);
                        if (dist <= 10) {

                            int tangentX = -collisionY;
                            int tangentY = collisionX;
                            
                            float d = (motionY - collisionG * motionX) /
                                        (collisionG * tangentX - tangentY);
                            
                            int mPointX = (int) (motionX + tangentX * d);
                            int mPointY = (int) (motionY + tangentY * d);
                            
                            int newMotionX = (int) (- motionX - 2 * tangentX * d);
                            int newMotionY = (int) (- motionY - 2 * tangentY * d);
        
                            motionX = newMotionX;
                            motionY = newMotionY;
                        }
                        else
                            collision = false;
                    }
                }
                if (collision)
                    brick.collide();
            }
            
            else if (go instanceof Lenta && motionY > 0) {
                motionY = -motionY;
                motionX += centerX - go.getX();
                if (newY > go.getY() - 10)
                    newY = go.getY() - 10;
                correctMotion();
            }
            
            //Beton
            if (go instanceof Beton) {
                Beton brick = (Beton) go;
                boolean collision = true; 
                
                Bounds brickBounds = getBounds(brick);
                if (Math.abs(brickBounds.ty - centerY) > 9 &&
                    Math.abs(brickBounds.by - centerY) > 9)
                  collision = false;
                else {
                    if (centerX >= brickBounds.lx && centerX <= brickBounds.rx) {
                        motionY = -motionY;
                    }
                    else if (centerY >= brickBounds.ty && centerY <= brickBounds.by) {
                        motionX = -motionX;
                    }
                    else {

                        int cornerX;
                        int cornerY;
                        float forceX;
                        float forceY;
                        
                        if (centerX < brickBounds.lx)
                            cornerX = brickBounds.lx;
                        else
                            cornerX = brickBounds.rx;
                        
                        if (centerY < brickBounds.ty)
                            cornerY = brickBounds.ty;
                        else
                            cornerY = brickBounds.by;
                        
                        int collisionX = centerX - cornerX;
                        int collisionY = centerY - cornerY;
                        float collisionG = (float) collisionY / collisionX;
                        
                        float dist = (float) Math.sqrt(collisionX * collisionX + collisionY * collisionY);
                        if (dist <= 10) {

                            int tangentX = -collisionY;
                            int tangentY = collisionX;
                            
                            float d = (motionY - collisionG * motionX) /
                                        (collisionG * tangentX - tangentY);
                            
                            int mPointX = (int) (motionX + tangentX * d);
                            int mPointY = (int) (motionY + tangentY * d);
                            
                            int newMotionX = (int) (- motionX - 2 * tangentX * d);
                            int newMotionY = (int) (- motionY - 2 * tangentY * d);
        
                            motionX = newMotionX;
                            motionY = newMotionY;
                        }
                        else
                            collision = false;
                    }
                }
                if (collision)
                    brick.collide();
            }
            
            else if (go instanceof Lenta && motionY > 0) {
                motionY = -motionY;
                motionX += centerX - go.getX();
                if (newY > go.getY() - 10)
                    newY = go.getY() - 10;
                correctMotion();
            }
            
            //Plyta
            if (go instanceof Plyta) {
                Plyta brick = (Plyta) go;
                boolean collision = true;
                
                Bounds brickBounds = getBounds(brick);
                if (Math.abs(brickBounds.ty - centerY) > 9 &&
                    Math.abs(brickBounds.by - centerY) > 9)
                  collision = false;
                else {
                    if (centerX >= brickBounds.lx && centerX <= brickBounds.rx) {
                        motionY = -motionY;
                    }
                    else if (centerY >= brickBounds.ty && centerY <= brickBounds.by) {
                        motionX = -motionX;
                    }
                    else {
                        
                        int cornerX;
                        int cornerY;
                        float forceX;
                        float forceY;
                        
                        if (centerX < brickBounds.lx)
                            cornerX = brickBounds.lx;
                        else
                            cornerX = brickBounds.rx;
                        
                        if (centerY < brickBounds.ty)
                            cornerY = brickBounds.ty;
                        else
                            cornerY = brickBounds.by;
                        
                        int collisionX = centerX - cornerX;
                        int collisionY = centerY - cornerY;
                        float collisionG = (float) collisionY / collisionX;

                        float dist = (float) Math.sqrt(collisionX * collisionX + collisionY * collisionY);
                        if (dist <= 10) {

                            int tangentX = -collisionY;
                            int tangentY = collisionX;
                            
                            float d = (motionY - collisionG * motionX) /
                                        (collisionG * tangentX - tangentY);
                            
                            int mPointX = (int) (motionX + tangentX * d);
                            int mPointY = (int) (motionY + tangentY * d);
                            
                            int newMotionX = (int) (- motionX - 2 * tangentX * d);
                            int newMotionY = (int) (- motionY - 2 * tangentY * d);
        
                            motionX = newMotionX;
                            motionY = newMotionY;
                        }
                        else
                            collision = false;
                    }
                }
                if (collision)
                    brick.collide();
            }
            
            else if (go instanceof Lenta && motionY > 0) {
                motionY = -motionY;
                motionX += centerX - go.getX();
                if (newY > go.getY() - 10)
                    newY = go.getY() - 10;
                correctMotion();
            }
        }
    }
    
    /**
     * Adjust motion vectors (X,Y) to correct speed.
     */
    public void correctMotion()
    {
        float gotSpeed = (float) Math.sqrt(motionX * motionX + motionY * motionY);
        motionX *= ballSpeed / gotSpeed;
        motionY *= ballSpeed / gotSpeed;
    }
    
    public static class Bounds
    {
        public int lx; // left edge
        public int rx; // right edge
        public int ty; // top edge
        public int by; // bottom edge
    }
}
Plyta This is BRICK
import greenfoot.World;
import greenfoot.Actor;
import greenfoot.*;

public class Plyta extends Actor
{
  static GreenfootSound song;
  static{
        song = new GreenfootSound("click.wav");
    }
    
    public Plyta()
    {
        
    }
    
    public void act()
    {
        
    }

    public void collide()
    {
        song.play();
        getWorld().removeObject(this);
    }
}

danpost danpost

2017/5/2

#
You may want to refer to my Value Display Tutorial scenario.
skitlesas skitlesas

2017/5/2

#
danpost wrote...
You may want to refer to my Value Display Tutorial scenario.
witch my code it's not impossible to make this Counter?
danpost danpost

2017/5/2

#
skitlesas wrote...
danpost wrote...
You may want to refer to my Value Display Tutorial scenario.
witch my code it's not impossible to make this Counter?
Definitely possible.
skitlesas skitlesas

2017/5/2

#
danpost wrote...
skitlesas wrote...
danpost wrote...
You may want to refer to my Value Display Tutorial scenario.
witch my code it's not impossible to make this Counter?
Definitely possible.
Soo can you help me? Please
danpost danpost

2017/5/2

#
skitlesas wrote...
Soo can you help me? Please
Follow the tutorial.
skitlesas skitlesas

2017/5/2

#
Your browser is ignoring the <APPLET> tag for me i try everything...
danpost danpost

2017/5/2

#
Use IE, put 'https://www.greeenfoot.org/' in the java security exception site list and set the security level to 'Medium High'. There are discussion threads on this subject. Perform a search and check them out.
You need to login to post a reply.