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

2013/3/15

Question about pinball game

1
2
3
maartendrift maartendrift

2013/3/16

#
By the way, when I use your idea the Paddle rotates a full round and will not come back.
Gevater_Tod4711 Gevater_Tod4711

2013/3/16

#
The IllegalArgumentException occurs because of this part of your code:
if (getY() >= (world.getHeight() - 10))  
        {  
            world.removeObject(this);  
            world.addObject(ball, 460, 530);  
            removed++;                   
        }  
          
        if (getX() <= 7 || getX() >= (getWorld().getWidth()-7))  
        {  
            motionX = -motionX;  
        }  
After the world.removeObject(this); you can't use the method getX() because the actor is not in the world anymore and then you get this exception. If you just change the two if statements it should work. To the second problem: You have to turn the paddle back by using the same counter. If the counter is greater than 60 and less than 120 the paddle has to turn back.
maartendrift maartendrift

2013/3/16

#
The first problem is solved! Thank you. But still I don't get the second solved. I thnk there's somewhere a stupid error made by me, but I can't find it... My code is the following:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Right here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Right extends Flippers
{
    private boolean isDown;
    private int paddleRotation;
    /**
     * Act - do whatever the Right wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public Right()
    {
        this(30);        
    }
    
    public Right(int rotation)
    {
        setRotation(getRotation()-rotation);
        paddleRotation = 0;
    }
    
    public void act()
    {
        rotatePaddle();
    }    
    
    public void rotatePaddle()
    {
        if (!isDown && Greenfoot.isKeyDown("right"))
        {
            isDown = true;
            int xPosition = this.getX();
            int yPosition = this.getY();
            
            if (paddleRotation <= 60 && paddleRotation >= 120)
            {
                setRotation(getRotation()+1);
                paddleRotation++;
            }
                       
            int newX = xPosition + 10;
            int newY = yPosition - 50;
            
            setLocation(newX, newY);
        }
        if (isDown && !Greenfoot.isKeyDown("right"))
        {
            isDown = false;

            if (paddleRotation >= 60 && paddleRotation <= 120)
            {
                setRotation(getRotation()-1);
                paddleRotation--;
            }

           
            int xPosition = this.getX();
            int yPosition = this.getY();
       
            int newX = xPosition - 10;
            int newY = yPosition + 50;
           
            setLocation(newX, newY);
        }
    }
}
danpost danpost

2013/3/16

#
A more simplistic way of thinking about what you want to happen may be in order. My thoughts were: if the key is down and the flip amount is less than a certain value, increase the flip amount; and, if the key is not down and the flip amount is greater than zero, decrease the flip amount. The position of the flippers does not have to move at all. If you draw the image of the flipper on another image using the same width, but almost twice the length (placing the image so that the end of the flipper is at the end of the length of the image), then just by rotating the image, you will get your flipping action. The center of the image is the point of rotation for the flipper (where the peg would come up out of the playing surface that the flipper is set on).
Gevater_Tod4711 Gevater_Tod4711

2013/3/16

#
In line 42 is says if (paddleRotation <= 60 && paddleRotation >= 120). I don't know any number that is less than 60 and the same time greater thatn 120. Do you? This probably is one problem but there probably will be another. If you click the right arrowbutton the paddle will move up. But not always 60 acts. So if you click the key not long enought the counter will never reach 60. Also in the next if statement there is a problem. There it says if (isDown &&...) and if this is true you set isDown = false so this will only be executed one time. Instead of this you should do it like this:
if (!isDown && Greenfoot.isKeyDown("right"))  
        {  
            isDown = true;  
            int xPosition = this.getX();  
            int yPosition = this.getY();  
              
            if (paddleRotation < 60)  
            {  
                setRotation(getRotation()+1);  
                paddleRotation++;  
            }  
                         
            int newX = xPosition + 10;  
            int newY = yPosition - 50;  
              
            setLocation(newX, newY);  
        }  
        if (isDown && !Greenfoot.isKeyDown("right"))  
        {  
            isDown = false;  
             
            int xPosition = this.getX();  
            int yPosition = this.getY();  
         
            int newX = xPosition - 10;  
            int newY = yPosition + 50;  
             
            setLocation(newX, newY);  
        }  
        if (!isDown && paddleRotation > 0) {
            setRotation(getRotation()-1);  
            paddleRotation++;  
        }
danpost danpost

2013/3/16

#
There is no need for the 'isDown' field in this code. The previous state of the mouse button is not relevent to future actions in this case. As far as what to do at any instance, what matters is the current state of the mouse button. Removing the 'isDown' field and eliminating redundancies, the method can be reduced to the following:
public void rotatePaddle()
{
    int xPosition = this.getX();  
    int yPosition = this.getY();
    int change = 0;
    if (Greenfoot.isKeyDown("right") && paddleRotation < 60)  change = 1;
    if (!Greenfoot.isKeyDown("right") && paddleRotation > 0) change = -1;
    if (change == 0) return;  
    setRotation(getRotation()+change);  
    paddleRotation += change;  
    int newX = xPosition + 10 * change;  
    int newY = yPosition - 50 * change;  
    setLocation(newX, newY);
}
maartendrift maartendrift

2013/3/16

#
@Gaveter When I copy + paste this code in my Game, then the Paddle will turn rounds when I push down "right". When I stop pushing the Paddle doesn't turn back. So that doesn't work, I think. @danpost I think the position of the Flippers isn't the problem. Because I fixed that with the newX = xPosition + 10 and - 10. And when I use your code the Flipper will move all over the game. And it doesn't come back. And the code of the Ball is indeed complicated, but it both comes down on what you said, isn't it? So both codes don't work. I think I do something wrong, but I don't know what.
Gevater_Tod4711 Gevater_Tod4711

2013/3/16

#
In the code I posted abouve in line 32 is a bug. It should be paddleRotation--; But I don't get why it isn't turning. If it just isn't turning this is not the bug you are searching for I think.
danpost danpost

2013/3/16

#
I'm sorry, I realize you only have two positions for your flipper. Here is revised code that may work better. It still may not look quite right, but should be much closer. The problem was in your code you are changing the rotation by one, yet moving the location abruptly.
public void rotatePaddle()
{
    int xPosition = this.getX();  
    int yPosition = this.getY();
    int change = 0;
    if (Greenfoot.isKeyDown("right") && paddleRotation < 60)  change = 1;
    if (!Greenfoot.isKeyDown("right") && paddleRotation > 0) change = -1;
    if (change == 0) return;  
    setRotation(getRotation() + 6 * change);  
    paddleRotation += 6 * change;  
    int newX = xPosition + 1 * change;  
    int newY = yPosition - 5 * change;  
    setLocation(newX, newY);
}
danpost danpost

2013/3/16

#
In order for the flipping movement of the paddle to really look correct (if you keep the image you are using now), the points of location for the paddle should be along an arc, not a line. This is why I suggested making the image of the paddle almost twice the paddle's length, putting the center of the image at the point of rotation. This avoids having to relocate the paddle as it rotates.
maartendrift maartendrift

2013/3/16

#
So I copied the code of danpost and it worked! Thank you!
maartendrift maartendrift

2013/3/16

#
@danpost Excuse me. I think I don't understand you...
danpost danpost

2013/3/16

#
If you are satisfied with the way it is now, I would not worry about it. You are welcome.
maartendrift maartendrift

2013/3/16

#
That's true! I will show it, if it is finished on this forum!
maartendrift maartendrift

2013/3/16

#
I got, I hope the last, another question. When the Ball is at the bottom of the screen, the Ball has to be removed. But he stays on the screen. Even when I correct this with getHeight()-10 he does not remove itself. This is my code for the Ball at the moment:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.lang.Math;

/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball extends Actor
{
    private double motionY;
    private double fired;
    private int motionX;
    private double rightDown;
    private double leftDown;
    private boolean hitFlipper;
    private int changeMotionX;
    private int score;
    private boolean worldTwo;
    
    public Ball()
    {
        this(0.0);
    }
    
    public Ball(double speed)
    {
        motionY = -speed;
        motionX = 0;
        fired = 0;
        rightDown = 0;
        leftDown = 0;
        hitFlipper = true;
        score = 0;
        worldTwo = false;
    }

    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
       
    public void act() 
    {
        newLevel();
        moveBall();         
    }
    
    public void moveBall()
    {
        int xPosition = this.getX();
        double yPosition = this.getY();
        
        simulateGravity();
        checkForSpringle();
        fire();
        changeMotionX++;
        changeMotionX();
        checkForLine();
        checkForCirkel();
        checkForBlock();
        checkForFlipper();
        checkForProtonWave();
                                
        float newY = Math.round(yPosition + motionY);
        int newX = xPosition + motionX;
               
        setLocation(newX,(int)newY);
        
        if (fired != 0)
        {
            ATM myATM = (ATM) getWorld();
            myATM.addScore();
            score++;
        }        
    }
    
    private void newLevel()
    {
        if(score >= 3000)
        {            
            Greenfoot.setWorld(new ATM(1));
            worldTwo = true;
        }
        if (worldTwo && score >= 3000)
        {
            ATM myATM = (ATM) getWorld();
            myATM.youWon();
        }
    }
    
    private void changeMotionX()
    {
        if(changeMotionX == 10 && getX() != 460)
        {
            motionX = -(1 + Greenfoot.getRandomNumber(5));
            changeMotionX = 0;
        }
    }
    
    private void fire()
    {
        if (getX()==460 && Greenfoot.isKeyDown("space"))
        {
            fired++;
        }
        
        if (fired == 500)
        {
            fired = 500;
        }
    }
    
    private void checkForSpringle()
    {
        Actor actor = getOneIntersectingObject(Springle.class);
        if (actor != null)
        {
            motionY = (-0.3 * fired);
        }
    }
    
    private void simulateGravity()
    {
        if (fired >= 1)
        {
            if (motionY < 0)
            {
                motionY = motionY - (motionY * 0.005);
            }
        
            if (motionY > -0.5 && motionY <= 0)
            {
                motionY = 0.48;
            }
            
            if (motionY > 0)
            {
                motionY = motionY + (motionY * 0.005);
            }
        }  
    }
    
    private void checkForLine()
    {
        Actor line = getOneIntersectingObject(Line.class);
        if (getX() >=430 && getY() <=60)
        {
            if (line != null)
            {
                motionX = -(1+Greenfoot.getRandomNumber(1));
                motionY = -motionY;
                ATM myATM = (ATM) getWorld();
                myATM.addFiveScore();
                score = score + 5;
            }
        }
        else
        {
            if (line != null)
            {
                motionX = -motionX;
            }
        }
        if (line != null && getY() >=420)
        {
            motionY = -motionY;
        }
    }
    
    private void checkForCirkel()
    {
         Actor cirkel = getOneIntersectingObject(Cirkel.class);
         if (cirkel != null)
         {
             motionX = -motionX;
             motionY = -motionY;
             ATM myATM = (ATM) getWorld();
             myATM.addFiveScore();
             score = score + 5;
         }
    }
    
    private void checkForBlock()
    {
         Actor block = getOneIntersectingObject(Block.class);
         if (block != null)
         {
             motionX = -motionX;
             motionY = -motionY;
             ATM myATM = (ATM) getWorld();
             myATM.addFiveScore();
             score = score + 5;
         }
    }
    
    private void checkForFlipper()
    {
        Actor right = getOneIntersectingObject(Right.class);
        if (right != null && hitFlipper)
        {
            motionY = -motionY;
            motionX = -motionX;
            hitFlipper = false;
                        
            if(Greenfoot.isKeyDown("right"))
            {
                rightDown = rightDown + 0.3;
            }
            else
            {
                rightDown = 1;
            }
            
            if (rightDown == 2)
            {
                rightDown = 1.9;
            }
            
            motionY = (2-rightDown) * motionY;
            ATM myATM = (ATM) getWorld();
            myATM.addTenScore();
            score = score + 10;
        }
        
        Actor left = getOneIntersectingObject(Left.class);
        if (left != null && hitFlipper)
        {
            motionY = -motionY;
            motionX = -motionX;
            hitFlipper = false;
            
            if(Greenfoot.isKeyDown("left"))
            {
                leftDown = leftDown + 0.3;
            }
            else
            {
                leftDown = 1;
            }
            
            if (rightDown == 2)
            {
                leftDown = 1.9;
            }
            
            motionY = (2-leftDown)*motionY;
            ATM myATM = (ATM) getWorld();
            myATM.addTenScore();
            score = score + 10;
        }
        
        if (getY() <=530)
        {
            hitFlipper = true;
        }
    }
    
    private void checkForProtonWave()
    {
        Actor protonwave = getOneIntersectingObject(ProtonWave.class);
        if (protonwave != null)
        {
            ATM myATM = (ATM) getWorld();
            myATM.endGame();
        }
    }
    
    private void checkSide()    
    {
        World world = getWorld();
        Ball ball = new Ball();
        Springle springle = new Springle();
        
        if (getY() == 7)
        {
            motionY = -motionY;
            ATM myATM = (ATM) getWorld();
            myATM.addFiveScore();
            score = score + 5;
        }
        
        if (getX() <= 7 || getX() >= (getWorld().getWidth()-7))
        {
            motionX = -motionX;
        }
        
        if (getY() >= (world.getHeight() - 10))
        {
            ATM myATM = (ATM) getWorld();
            myATM.endGame();
            world.removeObject(this);
            Greenfoot.stop();
        }
        
    }
}
There are more replies on the next page.
1
2
3