Results 1 to 12 of 12
  1. #1
    vlan is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Question How to do a score board?

    hey

    I wanna make a score board.
    for exempel: When you touch the cirkel you should get one point.

    could any one please help me:)?

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.geom.Ellipse2D;
    import java.awt.event.KeyListener;
    import java.awt.event.KeyEvent;
    import java.awt.image.BufferedImage;
    import java.util.*;
    
    
    public class TE extends Applet implements KeyListener {
    	Image bild1;
        float x = 80.0f, xPlayer = 20.0f;
        float y = 20.0f, yPlayer = 60.0f;
        int horPlayer = 0, verPlayer = 0;
    
        float hor = 1.0f, ver = 0.5f;
        Random rnd = new Random();
    
    
        public void init() {
            this.addKeyListener(this);
        }
    
        public void paint(Graphics g) {
        	bild1=getImage(getCodeBase(),"prog3.GIF");
            BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
            Graphics bufferedGraphics = image.getGraphics();
    
            bufferedGraphics.setColor(Color.WHITE);
            bufferedGraphics.fillRect(0, 0, image.getWidth(), image.getHeight());
    
            bufferedGraphics.setColor(Color.BLACK);
            bufferedGraphics.drawRect(10, 10, 150, 150);
    
            bufferedGraphics.setColor(Color.RED);
            int x = (int) this.x;
            int y = (int) this.y;
            bufferedGraphics.fillOval(x, y, 10, 10);
    
            bufferedGraphics.setColor(Color.BLUE);
            x = (int) this.xPlayer;
            y = (int) this.yPlayer;
            bufferedGraphics.fillOval(x, y, 10, 10);
            bufferedGraphics.dispose();
            g.drawImage(image, 0, 0, null);
            
    
            
    
            try {
                Thread.sleep(15);
            } catch (InterruptedException e) {
                System.out.println("EERRRROOOOORRRR");
            }
            uppdatera();
        }
    
        public void uppdatera() {
            if (x + hor > 150.0 || x + hor < 10.0)
                hor = -hor;
    
            x += hor;
    
            if (y + ver > 150.0 || y + ver < 10.0)
                ver = -ver;
    
            y += ver;
    
    
            if (xPlayer + horPlayer > 150.0 || xPlayer + horPlayer < 10.0)
                horPlayer = 0;
    
            xPlayer += horPlayer;
    
    
            if (yPlayer + verPlayer > 150.0 || yPlayer + verPlayer < 10.0)
                verPlayer = 0;
    
            yPlayer += verPlayer;
    
            Shape circlePC = new Ellipse2D.Float(x, y, 10.0f, 10.0f);
            Shape circlePlayer = new Ellipse2D.Float(xPlayer, yPlayer, 10.0f, 10.0f);
            if (circlePC.intersects(circlePlayer.getBounds())) {
                hor = -hor;
                ver = -ver;
                do {
    
    
                    circlePC = new Ellipse2D.Float(x, y, 10.0f, 10.0f);
                    circlePlayer = new Ellipse2D.Float(xPlayer, yPlayer, 10.0f, 10.0f);
    
                    x += hor;
                    y += ver;
    
                } while (circlePC.intersects(circlePlayer.getBounds()));
            }
            repaint();
        }
    
        private int mathrandom() {
    // TODO Auto-generated method stub
            return 0;
        }
    
    
        public void keyTyped(KeyEvent e) {
            //empty
        }
    
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
    
            if (key == KeyEvent.VK_LEFT)
                horPlayer = -2;
            if (key == KeyEvent.VK_RIGHT) {
                horPlayer = 2;
                System.out.println("right");
            }
            if (key == KeyEvent.VK_UP)
                verPlayer = -2;
            if (key == KeyEvent.VK_DOWN)
                verPlayer = 2;
        }
    
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
    
            if (key == KeyEvent.VK_LEFT && horPlayer != 0)
                horPlayer = 0;
            if (key == KeyEvent.VK_RIGHT && horPlayer != 0)
                horPlayer = 0;
            if (key == KeyEvent.VK_UP && verPlayer != 0)
                verPlayer = 0;
            if (key == KeyEvent.VK_DOWN && verPlayer != 0)
                verPlayer = 0;
        }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    What does the program now do or not do? Please explain.

    Add some debugging code using println() to show where in the code its executing and what the values are there.

  3. #3
    vlan is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    The program does:
    One cirkel moves randomly in a box and bounce on the walls.
    the second cirkel can i move with the arrow keys.
    If the second cirkel touch the first cirkel (the one that moves random), then the first cirkel will bounce the other direction.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    Does that explains what you want the program to do.

    What does it do now? How do you want to change it? What does it not do?

  5. #5
    vlan is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    This is what is does now:

    One cirkel moves randomly in a box and bounce on the walls.
    the second cirkel can i move with the arrow keys.
    If the second cirkel touch the first cirkel (the one that moves random), then the first cirkel will bounce the other direction.

    I wanna make a score board so you can get points if for exampel the blue cirkel touch the first cirkel, then you get one point and so on.

    Do you understand?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    if for exampel the blue cirkel touch the first cirkel
    OK, How do you trying to detect if the circles touch? One test would be if the distance between the centers of the circles is < sum of their radius's.

  7. #7
    vlan is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    I mean it was en exempel.
    If you can make up some easier way to get score plz help me.

    Just so i get score somehow i don,t really care how.

    I just wanna learn how to use score. It is my first time.

    hmm, i can,t come up with some easy way.....to get score.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    You need to find where in the program it detects that the circles are touching.
    Have you found that yet?

    Use System.out.println() to output the results. Later you'll need to learn how to add an output field in the applet and display the results there.

  9. #9
    vlan is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    Here i suppose:

    Java Code:
    public void uppdatera() {
            if (x + hor > 150.0 || x + hor < 10.0)
                hor = -hor;
    
            x += hor;
    
            if (y + ver > 150.0 || y + ver < 10.0)
                ver = -ver;
    
            y += ver;
    
    
            if (xPlayer + horPlayer > 150.0 || xPlayer + horPlayer < 10.0)
                horPlayer = 0;
    
            xPlayer += horPlayer;
    
    
            if (yPlayer + verPlayer > 150.0 || yPlayer + verPlayer < 10.0)
                verPlayer = 0;
    
            yPlayer += verPlayer;
    
            Shape circlePC = new Ellipse2D.Float(x, y, 10.0f, 10.0f);
            Shape circlePlayer = new Ellipse2D.Float(xPlayer, yPlayer, 10.0f, 10.0f);
            if (circlePC.intersects(circlePlayer.getBounds())) {
                hor = -hor;
                ver = -ver;
                do {
    
    
                    circlePC = new Ellipse2D.Float(x, y, 10.0f, 10.0f);
                    circlePlayer = new Ellipse2D.Float(xPlayer, yPlayer, 10.0f, 10.0f);
    
                    x += hor;
                    y += ver;
    
                } while (circlePC.intersects(circlePlayer.getBounds()));
            }
            repaint();
        }

  10. #10
    vlan is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

    Default

    I have tried something like

    Java Code:
    if( player_pos == boll_pos )
    {
         playerscore += 200;
    }
    diffrent varibels on player_pos and boll_pos

    But i coulden,t get it to work right.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default

    What are player_pos and boll_pos?

    It looks like:
    circlePC.intersects(circlePlayer.getBounds())
    is the test to make.

  12. #12
    vlan is offline Member
    Join Date
    Apr 2010
    Posts
    10
    Rep Power
    0

Similar Threads

  1. how can I draw a board in 2d array?
    By java_fun2007 in forum New To Java
    Replies: 8
    Last Post: 12-28-2011, 05:58 PM
  2. need help making a game board
    By Don k in forum New To Java
    Replies: 2
    Last Post: 04-30-2010, 12:09 AM
  3. game board design
    By tomitu in forum New To Java
    Replies: 12
    Last Post: 02-27-2010, 05:31 PM
  4. how to create board in java...
    By zenith in forum New To Java
    Replies: 5
    Last Post: 01-20-2010, 04:42 AM
  5. Programming a Board Game
    By makanti in forum New To Java
    Replies: 7
    Last Post: 03-10-2009, 02:32 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
  •