Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    OldSkoolBray is offline Member
    Join Date
    Apr 2014
    Posts
    17
    Rep Power
    0

    Default Don't know how to add a scorekeeper for my game?

    Hello I am creating a simple AirHockey game in Eclipse Java Jframe. I was just wondering how it is i add a score keeper to my game. for example if player 1 scores a goal 1 is added onto there score at the top.

    Any help would be much appreciated.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Don't know how to add a scorekeeper for my game?

    Store the score somewhere in the code?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    OldSkoolBray is offline Member
    Join Date
    Apr 2014
    Posts
    17
    Rep Power
    0

    Default Re: Don't know how to add a scorekeeper for my game?

    I don't know where it would go though? need it so that player 1 has a score and player 2. player 1 and player 2 are j buttons that move like the paddles in air hockey

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Don't know how to add a scorekeeper for my game?

    Create a class for each player and store the score in the class.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    OldSkoolBray is offline Member
    Join Date
    Apr 2014
    Posts
    17
    Rep Power
    0

    Default Re: Don't know how to add a scorekeeper for my game?

    how will the score be adding up etc such as when the puck goes into the net red gets 1? would i need a collision?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Don't know how to add a scorekeeper for my game?

    Very hard to make any recommendations without seeing the code.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    OldSkoolBray is offline Member
    Join Date
    Apr 2014
    Posts
    17
    Rep Power
    0

    Default Re: Don't know how to add a scorekeeper for my game?

    I will upload my code now now, thanks for your help Norm. been trying to search how to get a scoring system and i just can't find anything that helps

  8. #8
    OldSkoolBray is offline Member
    Join Date
    Apr 2014
    Posts
    17
    Rep Power
    0

    Default Re: Don't know how to add a scorekeeper for my game?

    i have created another class for my collisions. also i still need to add a couple of collisions. just need to know how the game will identify when a goal is scored by scoring it in a scoring system.

    ....



    package assignmnet2AirHockey;

    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.ImageIcon;
    import java.awt.Color;
    import javax.swing.border.MatteBorder;
    import javax.swing.SwingConstants;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.JLabel;

    import javax.swing.Timer;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;

    public class AirHockeyGame extends JFrame {
    Timer timer;
    int timerCount = 100;
    int ballSpeedX = 2;
    int ballSpeedY = 2;

    private JPanel contentPane;
    private JButton btnPuck;
    private JButton btnRedPlayer;
    private JButton btnBluePlayer;
    private JButton btnRedGoal;
    private JButton btnBlueGoal;

    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    AirHockeyGame frame = new AirHockeyGame();
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }

    /**
    * Create the frame.
    */
    public AirHockeyGame() {
    addComponentListener(new ComponentAdapter() {
    public void componentShown(ComponentEvent e) {
    timer.start();
    }
    });

    {

    };
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 400, 500);
    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setBorder(new MatteBorder(10, 10, 10, 10, (Color) Color.RED));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    btnPuck = new JButton("");
    btnPuck.setIcon(new ImageIcon(AirHockeyGame.class.getResource("/images/ImageBlackPuck.png")));
    btnPuck.setBounds(197, 215, 30, 39);
    contentPane.add(btnPuck);

    btnRedPlayer = new JButton("");
    btnRedPlayer.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent arg0) {
    if (arg0.getKeyCode() == KeyEvent.VK_LEFT)
    {
    // Move Red Player Left
    btnRedPlayer.setBounds(btnRedPlayer.getX() - 6, btnRedPlayer.getY(),
    btnRedPlayer.getWidth(), btnRedPlayer.getHeight());
    }

    if (arg0.getKeyCode() == KeyEvent.VK_RIGHT)
    {
    // Move Red Player Right
    btnRedPlayer.setBounds(btnRedPlayer.getX() + 6, btnRedPlayer.getY(),
    btnRedPlayer.getWidth(), btnRedPlayer.getHeight());
    }
    }
    });
    btnRedPlayer.setIcon(new ImageIcon(AirHockeyGame.class.getResource("/images/ImageRedPaddle.png")));
    btnRedPlayer.setBounds(175, 373, 70, 71);
    contentPane.add(btnRedPlayer);

    btnBluePlayer = new JButton("");
    btnBluePlayer.setIcon(new ImageIcon(AirHockeyGame.class.getResource("/images/ImageBluePaddle.png")));
    btnBluePlayer.setBounds(175, 34, 70, 71);
    contentPane.add(btnBluePlayer);

    btnRedGoal = new JButton("");
    btnRedGoal.setIcon(new ImageIcon(AirHockeyGame.class.getResource("/images/GoalRedSide.png")));
    btnRedGoal.setBounds(108, 449, 196, 23);
    contentPane.add(btnRedGoal);

    btnBlueGoal = new JButton("");
    btnBlueGoal.setVerticalAlignment(SwingConstants.TO P);
    btnBlueGoal.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    }
    });
    btnBlueGoal.setIcon(new ImageIcon(AirHockeyGame.class.getResource("/images/GoalBlueSide.png")));
    btnBlueGoal.setBounds(108, 6, 196, 23);
    contentPane.add(btnBlueGoal);

    JLabel lblNewLabel = new JLabel("");
    lblNewLabel.setIcon(new ImageIcon(AirHockeyGame.class.getResource("/images/Middle of Pitch.png")));
    lblNewLabel.setBounds(0, 154, 394, 167);
    contentPane.add(lblNewLabel);

    timer = new Timer (timerCount, new ActionListener() {
    public void actionPerformed (ActionEvent count) {
    btnPuck.setBounds(btnPuck.getX() + ballSpeedX, btnPuck.getY() + ballSpeedY,
    btnPuck.getWidth(), btnPuck.getHeight());

    if (LogicClass.collisionBotTop(btnPuck.getX(), btnPuck.getWidth(),
    contentPane.getWidth(), 0) == true)
    {
    ballSpeedX *= -1;
    }
    }});
    }
    }

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Don't know how to add a scorekeeper for my game?

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    I don't see any comments in the code saying when a score is made and for what team.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    OldSkoolBray is offline Member
    Join Date
    Apr 2014
    Posts
    17
    Rep Power
    0

    Default Re: Don't know how to add a scorekeeper for my game?

    let me just adjust a few things and i will put it back up, cheers for this mate

  11. #11
    OldSkoolBray is offline Member
    Join Date
    Apr 2014
    Posts
    17
    Rep Power
    0

    Default Re: Don't know how to add a scorekeeper for my game?

    before adding the scoring system i have come to a brick wall, the air hockey game that i am creating has two players, the user as the red player and the computer as the blue player. I have got it so that my red player moves left and right with the left and right key but when i try to move the computer on its own, it moves down and then up instead of left and right. Below is my code where the computer blue player is in my logic class ....

    public static int player2pos (int player2Pos, int ballPos)
    {
    int player2SpeedX = 0 ;

    if (ballPos < player2Pos)
    {
    player2SpeedX = -2;
    }

    if (ballPos > player2Pos)
    {
    player2SpeedX = 2;
    }

    return player2SpeedX;
    }

    }


    and here is the code in my main class ...

    int player2Speed = LogicClass.player2pos(btnBluePlayer.getX(), btnPuck.getX());

    btnBluePlayer.setBounds(btnBluePlayer.getX(), btnBluePlayer.getY() + player2Speed,
    btnBluePlayer.getHeight(), btnBluePlayer.getWidth());
    }});
    }
    }


    Can anybody help me with my problem, cheers

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Don't know how to add a scorekeeper for my game?

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    it moves down and then up instead of left and right
    Do you have the x and y axis swapped? x is left-right, y is up-down
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    OldSkoolBray is offline Member
    Join Date
    Apr 2014
    Posts
    17
    Rep Power
    0

    Default Problems with moving computer controlled Jbutton

    before adding the scoring system i have come to a brick wall, the air hockey game that i am creating has two players, the user is the red player and the computer as the blue player. I have got it so that my red player moves left and right with the left and right key but when i try to move the computer blue player on its own, it moves down and then up instead of left and right. Below is my code where the computer blue player is in my logic class ....

    public static int player2pos (int player2Pos, int ballPos)
    {
    int player2SpeedX = 0 ;

    if (ballPos < player2Pos)
    {
    player2SpeedX = -2;
    }

    if (ballPos > player2Pos)
    {
    player2SpeedX = 2;
    }

    return player2SpeedX;
    }

    }


    and here is the code in my main class ...

    int player2Speed = LogicClass.player2pos(btnBluePlayer.getX(), btnPuck.getX());

    btnBluePlayer.setBounds(btnBluePlayer.getX(), btnBluePlayer.getY() + player2Speed,
    btnBluePlayer.getHeight(), btnBluePlayer.getWidth());
    }});
    }
    }


    Can anybody help me with my problem, cheers

  14. #14
    OldSkoolBray is offline Member
    Join Date
    Apr 2014
    Posts
    17
    Rep Power
    0

    Default Re: Don't know how to add a scorekeeper for my game?

    no because my red player moves left and right fine but when i run the code the blue player moves down. The reason i can't understand why its doing it is because the red player moves left and right.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Don't know how to add a scorekeeper for my game?

    Can you post a version of the code that will compile, execute and show the problem? Be sure to wrap it in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Problems with moving computer controlled Jbutton

    Threads merged
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    OldSkoolBray is offline Member
    Join Date
    Apr 2014
    Posts
    17
    Rep Power
    0

    Default Re: Don't know how to add a scorekeeper for my game?

    so write a post with the full code in commented and wrapped in tag codes?

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Don't know how to add a scorekeeper for my game?

    Or work it out yourself.
    Doesn't need to be the full code, just enough to compile, execute and show the problem.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    OldSkoolBray is offline Member
    Join Date
    Apr 2014
    Posts
    17
    Rep Power
    0

    Default Re: Don't know how to add a scorekeeper for my game?

    Main Class >
    package assignmnet2AirHockey;

    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.ImageIcon;
    import java.awt.Color;
    import javax.swing.border.MatteBorder;
    import javax.swing.SwingConstants;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.JLabel;

    import javax.swing.Timer;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;

    [Timer for the ball]
    public class AirHockeyGame extends JFrame {
    Timer timer;
    int timerCount = 100;
    int ballSpeedX = 2;
    int ballSpeedY = 2;

    private JPanel contentPane;
    private JButton btnPuck;
    private JButton btnRedPlayer;
    private JButton btnBluePlayer;
    private JButton btnRedGoal;
    private JButton btnBlueGoal;

    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    AirHockeyGame frame = new AirHockeyGame();
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }

    /**
    * Create the frame.
    */
    public AirHockeyGame() {
    addComponentListener(new ComponentAdapter() {
    public void componentShown(ComponentEvent e) {
    timer.start();
    }
    });

    {

    };
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 400, 500);
    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setBorder(new MatteBorder(10, 10, 10, 10, (Color) Color.RED));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    btnPuck = new JButton("");
    btnPuck.setIcon(new ImageIcon(AirHockeyGame.class.getResource("/images/ImageBlackPuck.png")));
    btnPuck.setBounds(197, 215, 30, 39);
    contentPane.add(btnPuck);

    btnRedPlayer = new JButton("");
    btnRedPlayer.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent arg0) {
    if (arg0.getKeyCode() == KeyEvent.VK_LEFT)
    {
    // Move Red Player Left
    btnRedPlayer.setBounds(btnRedPlayer.getX() - 6, btnRedPlayer.getY(),
    btnRedPlayer.getWidth(), btnRedPlayer.getHeight());
    }

    if (arg0.getKeyCode() == KeyEvent.VK_RIGHT)
    {
    // Move Red Player Right
    btnRedPlayer.setBounds(btnRedPlayer.getX() + 6, btnRedPlayer.getY(),
    btnRedPlayer.getWidth(), btnRedPlayer.getHeight());
    }
    }
    });
    btnRedPlayer.setIcon(new ImageIcon(AirHockeyGame.class.getResource("/images/ImageRedPaddle.png")));
    btnRedPlayer.setBounds(175, 373, 70, 71);
    contentPane.add(btnRedPlayer);

    btnBluePlayer = new JButton("");
    btnBluePlayer.setIcon(new ImageIcon(AirHockeyGame.class.getResource("/images/ImageBluePaddle.png")));
    btnBluePlayer.setBounds(175, 34, 70, 71);
    contentPane.add(btnBluePlayer);

    btnRedGoal = new JButton("");
    btnRedGoal.setIcon(new ImageIcon(AirHockeyGame.class.getResource("/images/GoalRedSide.png")));
    btnRedGoal.setBounds(108, 449, 196, 23);
    contentPane.add(btnRedGoal);

    btnBlueGoal = new JButton("");
    btnBlueGoal.setVerticalAlignment(SwingConstants.TO P);
    btnBlueGoal.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    }
    });
    btnBlueGoal.setIcon(new ImageIcon(AirHockeyGame.class.getResource("/images/GoalBlueSide.png")));
    btnBlueGoal.setBounds(108, 6, 196, 23);
    contentPane.add(btnBlueGoal);

    JLabel lblNewLabel = new JLabel("");
    lblNewLabel.setIcon(new ImageIcon(AirHockeyGame.class.getResource("/images/Middle of Pitch.png")));
    lblNewLabel.setBounds(0, 154, 394, 167);
    contentPane.add(lblNewLabel);

    timer = new Timer (timerCount, new ActionListener() {
    public void actionPerformed (ActionEvent count) {
    btnPuck.setBounds(btnPuck.getX() + ballSpeedX, btnPuck.getY() + ballSpeedY,
    btnPuck.getWidth(), btnPuck.getHeight());
    // Allow Puck to bounce of the left/right of the game area
    if (LogicClass.collisionBotTop(btnPuck.getX(), btnPuck.getWidth(),
    contentPane.getWidth(), 0) == true)
    {
    ballSpeedX *= -1;
    }
    // Allow Puck to bounce of the top/bottom of game area
    if (LogicClass.collisionBotTop1(btnPuck.getY(), btnPuck.getHeight(),
    contentPane.getHeight(), 0) == true)
    {
    ballSpeedY *= -1;
    }
    if (LogicClass.batBallCollision(btnRedPlayer, btnPuck) == true)
    {
    ballSpeedY *= -1;
    }
    if (LogicClass.batBallCollision(btnBluePlayer, btnPuck) == true)
    {
    ballSpeedY *= -1;
    }

    [BLUE PLAYER CONTROLS]
    int player2Speed = LogicClass.player2pos(btnBluePlayer.getX(), btnPuck.getX());

    btnBluePlayer.setBounds(btnBluePlayer.getX(), btnBluePlayer.getY() + player2Speed,
    btnBluePlayer.getWidth(), btnBluePlayer.getHeight());
    }});
    }
    }


    This is my other class for collisions etc called LogicClass ...

    package assignmnet2AirHockey;

    import java.awt.Rectangle;
    import javax.swing.JButton;

    public class LogicClass {

    // Collisions between left/right side of game area and the ball
    static boolean collisionBotTop (int ObjectPosX, int ObjectHeight, int top, int bottom)
    {
    if(ObjectPosX < bottom || ObjectPosX > top - ObjectHeight)
    {
    return true;
    }
    else
    return false;
    }
    // Collisions between top/bottom of game area and the ball
    static boolean collisionBotTop1 (int ObjectPosY, int ObjectHeight, int top, int bottom)
    {
    if(ObjectPosY < bottom || ObjectPosY > top - ObjectHeight)
    {
    return true;
    }
    else
    return false;
    }
    // Checking Collisions between objects
    static boolean batBallCollision (JButton object1, JButton object2)
    {
    Rectangle one = new Rectangle (object1.getX(), object1.getY(),
    object1.getWidth(), object1.getHeight());

    Rectangle two = new Rectangle (object2.getX(), object2.getY(),
    object2.getWidth(), object2.getHeight());
    return one.intersects(two);
    }
    [BLUE PLAYER CONTROLS]
    public static int player2pos (int player2Pos, int ballPos)
    {
    int player2SpeedX = 0 ;

    if (ballPos < player2Pos)
    {
    player2SpeedX = -2;
    }

    if (ballPos > player2Pos)
    {
    player2SpeedX = 2;
    }

    return player2SpeedX;
    }
    [/QUOTE]
    }


    sorry i am not sure on how to tag the code etc. I have shown where the blue players controls are. i will upload an image of whats happening sorry i am pretty new to java and this is an assignment i need to get done as soon as.

  20. #20
    OldSkoolBray is offline Member
    Join Date
    Apr 2014
    Posts
    17
    Rep Power
    0

    Default Re: Don't know how to add a scorekeeper for my game?

    Don't know how to add a scorekeeper for my game?-screen-shot-2014-04-03-22.42.16.png
    Click image for larger version. 

Name:	Screen Shot 2014-04-03 at 22.42.16.png 
Views:	8 
Size:	46.2 KB 
ID:	6036


    this picture shows that the blue player moves down instead of left to right.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 03-03-2014, 07:48 PM
  2. Replies: 4
    Last Post: 07-08-2012, 10:07 AM
  3. Replies: 1
    Last Post: 06-02-2011, 10:29 AM
  4. Replies: 2
    Last Post: 08-05-2010, 05:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •