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

2020/2/4

Ping Pong Game

_kr_696 _kr_696

2020/2/4

#
So I have this as my scoreboard code but I have an error saying "incompatible types; java.awt.Font cannot be converted to greenfoot.Font". I need help, please.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
/**
 * 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)
    {
        GreenfootImage img = new GreenfootImage(800,40);
        Color.HSBtoRGB(10, 45, 100);
        Font font = new Font("Helvetica", Font.PLAIN, 20);
        img.drawString(Player1,150,35);
        img.drawString(Player1,500,35);
        setImage(img);
        img.setFont(font);
    }
    
    public void setText(String Player1, String Player2)
    {
        GreenfootImage img = getImage();
        img.clear();
        img.drawString(Player1,150,35);
        img.drawString(Player1,500,35);
    }
}
_kr_696 _kr_696

2020/2/4

#
I just found out java.awt is not associated but I have it working now but my scoreboard still isn't showing up on the screen. Here is the code for my world.
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); 
        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);
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        Paddle2 bar2 = new Paddle2();
        addObject(bar2,50,200);
        
    }
}
Here is the code for the 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)
    {
        GreenfootImage img = new GreenfootImage(800,40);
        Color.HSBtoRGB(10, 45, 100);
        Font font = new Font("Helvetica", true, false, 20);
        img.drawString(Player1,150,35);
        img.drawString(Player1,500,35);
        setImage(img);
        img.setFont(font);
    }
    
    public void setText(String Player1, String Player2)
    {
        GreenfootImage img = getImage();
        img.clear();
        img.drawString(Player1,150,35);
        img.drawString(Player1,500,35);
    }
}
_kr_696 _kr_696

2020/2/4

#
Switched line 15 to
img.setColor(Color.BLACK);
        img.fill();
and removed java.awt.color thinking it was because the text had no color but still nothing.
_kr_696 _kr_696

2020/2/4

#
i fixed it but only player one comes out on the screen not player 2.
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); 
        

        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, 105, 100);
    }
}
_kr_696 _kr_696

2020/2/4

#
here is the scenario
danpost danpost

2020/2/4

#
_kr_696 wrote...
i fixed it but only player one comes out on the screen not player 2. << Code Omitted >>
You cannot expect the second text string to show when you draw it starting just off the right edge of the image.
_kr_696 _kr_696

2020/2/4

#
danpost wrote...
_kr_696 wrote...
I fixed it but only player one comes out on the screen, not player 2. << Code Omitted >>
You cannot expect the second text string to show when you draw it starting just off the right edge of the image.
ah ok, thanks I changed the coordinates and they show up now. Thanks!
_kr_696 _kr_696

2020/2/4

#
now I need to make a score counter I have this so far. I don't know what else to do.
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.
    }    
}
World Class
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
{
    private Score theCounter;
    
    
    /**
     * 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: ", "Player Two: " );
        
        
        addObject(scoreboard, 200, 0);
    }
}
You need to login to post a reply.