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

2020/2/12

Ping Pong Game

_kr_696 _kr_696

2020/2/12

#
need help making it so when the ball hits the boundaries it adds a point to the other player and resets the whole world. Here is my game with the source code: https://www.greenfoot.org/scenarios/25316
danpost danpost

2020/2/13

#
_kr_696 wrote...
need help making it so when the ball hits the boundaries it adds a point to the other player and resets the whole world. Here is my game with the source code: https://www.greenfoot.org/scenarios/25316
I am unable to get .gfar file to unpack -- getting a javaFX applicction NullPointerException in greenfoot. Please provide codes here.
_kr_696 _kr_696

2020/2/13

#
World Code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        // theCounter = new Score();
        // addObject(theCounter, 175,48);
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Paddle1 bar1 = new Paddle1();
        addObject(bar1,550,200);
        Ball ball = new Ball();
        addObject(ball,300,200);
        Paddle2 bar2 = new Paddle2();
        addObject(bar2,50,200);
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0" );

        addObject(scoreboard, 200, 0);
    }
}
Ball code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball extends Actor
{
    public Ball()
    {
        GreenfootImage img = new GreenfootImage( 15,15);
        img.setColor(Color.RED);
        img.fill();
        setImage(img);
    }
    private int xSpeed = 5, ySpeed = 7;
    
    public int xVector = (Greenfoot.getRandomNumber(13)-6);
    public int yVector = (Greenfoot.getRandomNumber(13)-6);
    public void act() 
    {   
        movement();

        Actor paddle_1 = getOneIntersectingObject(Paddle1.class);
        if (paddle_1!=null)
        { 
            xSpeed = -xSpeed;
        }
        Actor paddle_2 = getOneIntersectingObject(Paddle2.class);
        if (paddle_2!=null)
        {  
            xSpeed = -xSpeed;
        }
        
        
        
    } 
    
    public void movement()
    {
        setLocation(getX() +  xSpeed, getY() + ySpeed);
        if(getY()<5 || getY() > getWorld().getHeight()-5)
        {
            ySpeed = -ySpeed;
        }
    }
    
    
}
paddles
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bar1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Paddle1 extends Paddles
{
    //Creates the paddle and fills it in
    public Paddle1()
    {
        GreenfootImage img = new GreenfootImage( 20,100);
        img.setColor(Color.RED);
        img.fill();
        setImage(img);
    }

    /**
     * Act - do whatever the Bar1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        if (Greenfoot.isKeyDown("up"))
        {   
            setLocation(getX(), getY()-5);
        }    
        if (Greenfoot.isKeyDown("down"))
        {   
            setLocation(getX(), getY()+5);
        }
    }    
}
Score
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Score extends Actor
{
    private int totalCount = 0;
    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("" + totalCount));
        
    }
    
    /**
     * Act - do whatever the Score wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
}
Scoreboard
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
// import java.awt.Color;

/**
 * Write a description of class Scoreboard here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Scoreboard extends Actor
{
    public Scoreboard( String Player1, String Player2)
    {
        //Sets the borders for where he txt will be
        GreenfootImage img = new GreenfootImage(500,300);
        img.setColor(Color.RED);
        
        Font font = new Font("Helvetica", true, false, 20);
        img.drawString(Player1,150,200);
        img.drawString(Player2,425,200);
        setImage(img);
        img.setFont(font);
    }
    
    public void setText(String Player1, String Player2)
    {
        GreenfootImage img = getImage();
        img.clear();
        img.drawString(Player1,150,35);
        img.drawString(Player2,500,35);
    }
}
danpost danpost

2020/2/13

#
I do not see where your scores are kept, to begin with.
_kr_696 _kr_696

4 days ago

#
oh well can you tell me what i need?
danpost danpost

4 days ago

#
_kr_696 wrote...
oh well can you tell me what i need?
Well, you need two int fields (or an int array of length two) to hold the score values. You might refer to my Value Display Tutorial scenario for assistance.
_kr_696 _kr_696

3 days ago

#
thanks. ill see what I can do.
_kr_696 _kr_696

2 hours ago

#
danpost wrote...
_kr_696 wrote...
oh well can you tell me what i need?
Well, you need two int fields (or an int array of length two) to hold the score values. You might refer to my Value Display Tutorial scenario for assistance.
so im still stuck on the counter. i tried but couldnt understand the tutorial enough to apply it to my game. can you maybe write the headers and ill fill the rest out. it would be appreciated or at least explain what i have to do. thanks in advance
_kr_696 _kr_696

2 hours ago

#
I don't have any attempted code. I erased it all.
danpost danpost

1 hour ago

#
_kr_696 wrote...
at least explain what i have to do.
Already did:
danpost wrote...
you need two int fields ... to hold the score values.
In your MyWorld class (or accessible to it).
You need to login to post a reply.