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

2017/5/31

I am having trouble with my pong game

Tristater4 Tristater4

2017/5/31

#
I have been using the correct(?) code for my pong game, but it doesn't seem to be working properly. I have pasted the code below. Thank you in advance. I have put up the game on my page. PONGWORLD
import greenfoot.*;
import java.awt.Color;
public class PongWorld extends World
{
    public PongWorld()
    {    
        super(800, 600, 1);
        Scoreboard scoreboard = new Scoreboard("Player One: 0", "Player Two: 0");
        addObject(scoreboard, 400, 20);
        addObject(new Ball(scoreboard), 400, 300);
        addObject(new playerOne(), 10, 300);
        addObject(new playerTwo(), 790, 300);
    }
}
Ball
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;
public class Ball extends Actor
{
    public int deltaX = (Greenfoot.getRandomNumber(5)+3);
    public int deltaY = (Greenfoot.getRandomNumber(5)+3);
    private int player1Count;
    private int player2Count;
    private Scoreboard score;
    public Ball(Scoreboard scoreboard) 
    {
        score = scoreboard;
    }
    public void act()
    {
        setLocation(getX() + deltaX, getY() +deltaY);
        wallCollision();
        paddleCollision();
        scorePoints();
    }
    public void wallCollision()
    {
        if (getX() < 10 || getX() > 790)
        {
            deltaX = -deltaX;
        }
        if ( getY() < 10 || getY() > 590)
        {
            deltaY = -deltaY;
        }
    }
    public void paddleCollision()
    {
        if (getOneIntersectingObject(playerOne.class) != null)
        {
            deltaX = -deltaX;
        }
        if (getOneIntersectingObject(playerTwo.class) != null)
        {
            deltaX = -deltaX;
        }
    }
    public void scorePoints()
    {
        if (getX()<10)
        {
            player2Count++;
            if (player2Count > 4)
            {
                setLocation(400, 250);
                GreenfootImage img = new GreenfootImage(250, 50);
                img.setColor(Color.white);
                Font font = new Font ("Helvetica", Font.BOLD, 25);
                img.setFont(font);
                img.drawString("Player Two Wins!", 30, 45);
                setImage(img);
                Greenfoot.stop();
            } else
            {
                setLocation(400, 300);
                score.setText("Player One: " + player1Count, "Player Two: " + player2Count);
                Greenfoot.delay(25);
            }
        }
        if (getX()<790)
        {
            player1Count++;
            if (player1Count > 4)
            {
                setLocation(400, 250);
               GreenfootImage img = new GreenfootImage(250, 50);
               img.setColor(Color.white);
               Font font = new Font ("Helvetica", Font.BOLD, 25);
               img.setFont(font);
               img.drawString("Player One Wins!", 30, 45);
               setImage(img);
               Greenfoot.stop();
            } else
            {
                setLocation(400, 300);
                score.setText("Player One: " + player1Count, "Player Two: " + player2Count);
                Greenfoot.delay(25);
            }
        }
    }
}
Paddles - playerTwo import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class playerTwo extends Paddles { /** * Act - do whatever the playerTwo wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.isKeyDown("up")) { setLocation(getX(), getY()-6); } if (Greenfoot.isKeyDown("down")) { setLocation(getX(), getY()+6); } } } playerOne import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class playerOne extends Paddles { /** * Act - do whatever the playerOne wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.isKeyDown("w")) { setLocation(getX(), getY()-6); } if (Greenfoot.isKeyDown("s")) { setLocation(getX(), getY()+6); } } } Scoreboard import greenfoot.*; import java.awt.Color; import java.awt.Font; public class Scoreboard extends Actor { public Scoreboard(String player1, String player2) { GreenfootImage img = new GreenfootImage (800, 40); img.setColor(Color.white); Font font = new Font("Helvetica", Font.BOLD, 20); img.setFont(font); img.drawString(player1, 150, 35); img.drawString(player2, 500, 35); setImage(img); } public void setText(String player1, String player2) { GreenfootImage img = getImage(); img.clear(); img.drawString(player1, 150, 35); img.drawString(player2, 500, 35); } }
danpost danpost

2017/5/31

#
Tristater4 wrote...
it doesn't seem to be working properly.
How is it behaving as opposed to what it should do?
Tristater4 Tristater4

2017/6/2

#
danpost wrote...
Tristater4 wrote...
it doesn't seem to be working properly.
How is it behaving as opposed to what it should do?
The ball will not move when run is hit and I have inserted a move() code
Super_Hippo Super_Hippo

2017/6/2

#
Change line 66 in the Ball class from
if (getX()<790)
to
if (getX()>790)
You need to login to post a reply.