Results 1 to 5 of 5
  1. #1
    whdbstjr90 is offline Member
    Join Date
    Dec 2007
    Posts
    3
    Rep Power
    0

    Default How do I make My ball to move randomly?

    Im working on pacman and im trying to make ghosts and I have a no idea how to make them to move randomly. So I just want to see how to make my pacman to move randomly first.

    ↓ this is how I made my pacman to move, can someone edit this to make it move randomly?


    public void keyPressed(KeyEvent e)
    {
    if(e.getKeyCode() == KeyEvent.VK_RIGHT)
    {
    smoothright = true;
    smoothup = false;
    smoothleft = false;
    smoothdown = false;
    }

    if(e.getKeyCode() == KeyEvent.VK_LEFT)
    {
    smoothleft = true;
    smoothup = false;
    smoothright = false;
    smoothdown = false;
    }

    if(e.getKeyCode() == KeyEvent.VK_UP)
    {
    smoothup = true;
    smoothright = false;
    smoothleft = false;
    smoothdown = false;
    }

    if(e.getKeyCode() == KeyEvent.VK_DOWN)
    {
    smoothdown = true;
    smoothup = false;
    smoothright = false;
    smoothleft = false;

    }
    }

    public void actionPerformed(ActionEvent e)
    {
    if(smoothright == true)
    {
    right = true;

    if(right == true)
    {
    if(pacman.getX() + 40 >= 530)
    {
    }
    else
    {
    pacman.move(2, 0);
    }
    }

    int i = 0;
    while(i < squarelist.size())
    {
    if(pacman.getBounds().intersects(squarelist.get(i) .getBounds()))
    {
    right = false;
    }
    i = i + 1;
    }

    if(right == false)
    {
    pacman.move(-2, 0);
    }

    right = true;
    }

    if(smoothleft == true)
    {
    left = true;

    if(left == true)
    {
    if(pacman.getX() <= 33)
    {
    }
    else
    {
    pacman.move(-2, 0);
    }
    }

    int i = 0;
    while(i < squarelist.size())
    {
    if(pacman.getBounds().intersects(squarelist.get(i) .getBounds()))
    {
    left = false;
    }
    i = i + 1;
    }

    if(left == false)
    {
    pacman.move(2, 0);
    }


    left = true;
    }

    if(smoothup == true)
    {
    up = true;

    if(up == true)
    {
    if(pacman.getY() <= 43)
    {
    }
    else
    {
    pacman.move(0, -2);
    }
    }

    int i = 0;
    while(i < squarelist.size())
    {
    if(pacman.getBounds().intersects(squarelist.get(i) .getBounds()))
    {
    up = false;
    }
    i = i + 1;
    }
    if(up == false)
    {
    pacman.move(0, 2);
    }

    up = true;
    }

    if(smoothdown == true)
    {
    down = true;

    if(down == true)
    {
    if(pacman.getY() >= 548.1)
    {
    }
    else
    {
    pacman.move(0, 2);
    }
    }

    int i = 0;
    while(i < squarelist.size())
    {
    if(pacman.getBounds().intersects(squarelist.get(i) .getBounds()))
    {
    down = false;
    }
    i = i + 1;
    }

    if(down == false)
    {
    pacman.move(0, -2);
    }

    down = true;
    }




    import acm.program.*;
    import acm.util.*;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import acm.graphics.*;
    import java.util.*;

    public class Main extends GraphicsProgram
    {
    private ArrayList<GRect> squarelist = new ArrayList<GRect>();
    private ArrayList<GOval> dots = new ArrayList<GOval>();
    private RandomGenerator gen = new RandomGenerator();
    private Background Background;
    private GRect Center;
    private GRect LeftCenter;
    private GRect LeftCenter2;
    private GRect LeftCenter3;
    private GRect RightCenter;
    private GRect RightCenter2;
    private GRect RightCenter3;
    private GRect Boarder;
    private GRect Boarder2;
    private GRect LeftBoarder;
    private GRect LeftBoarder2;
    private GRect RightBoarder;
    private GRect RightBoarder2;
    private GRect North;
    private GRect North2;
    private GRect North3;
    private GRect South;
    private GRect South2;
    private GRect South3;
    private GRect South4;
    private GRect Northwest;
    private GRect Northwest2;
    private GRect Northwest3;
    private GRect Northeast;
    private GRect Northeast2;
    private GRect Northeast3;
    private GRect Southwest;
    private GRect Southwest2;
    private GRect Southwest3;
    private GRect Southwest4;
    private GRect Southwest5;
    private GRect Southwest6;
    private GRect Southeast;
    private GRect Southeast2;
    private GRect Southeast3;
    private GRect Southeast4;
    private GRect Southeast5;
    private GRect Southeast6;
    private Pacman pacman;
    private com com;
    private SwingTimer t;
    private boolean smoothright;
    private boolean smoothleft;
    private boolean smoothup;
    private boolean smoothdown;
    private boolean right;
    private boolean left;
    private boolean up;
    private boolean down;

    public void run()
    {
    resize(800, 650);

    Background = new Background();
    Background.setLocation(0, 0);
    add(Background);

    Center = new GRect(210, 300, 110, 50);
    Center.setColor(Color.BLUE);
    Center.setFilled(false);
    add(Center);
    squarelist.add(Center);

    LeftCenter = new GRect(150, 160, 10, 150);
    LeftCenter.setColor(Color.BLUE);
    LeftCenter.setFilled(false);
    add(LeftCenter);
    squarelist.add(LeftCenter);

    LeftCenter2 = new GRect(160, 220, 40, 10);
    LeftCenter2.setColor(Color.BLUE);
    LeftCenter2.setFilled(false);
    add(LeftCenter2);
    squarelist.add(LeftCenter2);

    LeftCenter3 = new GRect(150, 380, 10, 70);
    LeftCenter3.setColor(Color.BLUE);
    LeftCenter3.setFilled(false);
    add(LeftCenter3);
    squarelist.add(LeftCenter3);

    RightCenter = new GRect(370, 160, 10, 150);
    RightCenter.setColor(Color.BLUE);
    RightCenter.setFilled(false);
    add(RightCenter);
    squarelist.add(RightCenter);

    RightCenter2 = new GRect(330, 220, 40, 10);
    RightCenter2.setColor(Color.BLUE);
    RightCenter2.setFilled(false);
    add(RightCenter2);
    squarelist.add(RightCenter2);

    RightCenter3 = new GRect(370, 380, 10, 70);
    RightCenter3.setColor(Color.BLUE);
    RightCenter3.setFilled(false);
    add(RightCenter3);
    squarelist.add(RightCenter3);

    Boarder = new GRect(20, 30, 500, 550);
    Boarder.setColor(Color.BLUE);
    Boarder.setFilled(false);
    add(Boarder);

    Boarder2 = new GRect(30, 40, 480, 530);
    Boarder2.setColor(Color.BLUE);
    Boarder2.setFilled(false);
    add(Boarder2);

    LeftBoarder = new GRect(30, 240, 80, 70);
    LeftBoarder.setColor(Color.BLUE);
    add(LeftBoarder);
    squarelist.add(LeftBoarder);

    LeftBoarder2 = new GRect(30, 340, 80, 70);
    LeftBoarder2.setColor(Color.BLUE);
    LeftBoarder2.setFilled(false);
    add(LeftBoarder2);
    squarelist.add(LeftBoarder2);

    RightBoarder = new GRect(430, 240, 80, 70);
    RightBoarder.setColor(Color.BLUE);
    RightBoarder.setFilled(false);
    add(RightBoarder);
    squarelist.add(RightBoarder);

    RightBoarder2 = new GRect (430, 340, 80, 70);
    RightBoarder2.setColor(Color.BLUE);
    RightBoarder2.setFilled(false);
    add(RightBoarder2);
    squarelist.add(RightBoarder2);

    North = new GRect(260, 40, 10, 80);
    North.setColor(Color.BLUE);
    North.setFilled(false);
    add(North);
    squarelist.add(North);

    North2 = new GRect(210, 160, 110, 10);
    North2.setColor(Color.BLUE);
    North2.setFilled(false);
    add(North2);
    squarelist.add(North2);

    North3 = new GRect(260, 170, 10, 60);
    North3.setColor(Color.BLUE);
    North3.setFilled(false);
    add(North3);
    squarelist.add(North3);

    South = new GRect(260, 510, 10, 35);
    South.setColor(Color.BLUE);
    South.setFilled(false);
    add(South);
    squarelist.add(South);

    South2 = new GRect(180, 500, 165, 10);
    South2.setColor(Color.BLUE);
    South2.setFilled(false);
    add(South2);
    squarelist.add(South2);

    South3 = new GRect(180, 440, 170, 10);
    South3.setColor(Color.BLUE);
    South3.setFilled(false);
    add(South3);
    squarelist.add(South3);

    South4 = new GRect(260, 450, 10, 30);
    South4.setColor(Color.BLUE);
    South4.setFilled(false);
    add(South4);
    squarelist.add(South4);

    Northwest = new GRect(60, 80, 50, 40);
    Northwest.setColor(Color.BLUE);
    Northwest.setFilled(false);
    add(Northwest);
    squarelist.add(Northwest);

    Northwest2 = new GRect(150, 80, 70, 40);
    Northwest2.setColor(Color.BLUE);
    Northwest2.setFilled(false);
    add(Northwest2);
    squarelist.add(Northwest2);

    Northwest3 = new GRect(60, 160, 50, 10);
    Northwest3.setColor(Color.BLUE);
    Northwest3.setFilled(false);
    add(Northwest3);
    squarelist.add(Northwest3);

    Northeast = new GRect(420, 80, 50, 40);
    Northeast.setColor(Color.BLUE);
    Northeast.setFilled(false);
    add(Northeast);
    squarelist.add(Northeast);

    Northeast2 = new GRect(310, 80, 70, 40);
    Northeast2.setColor(Color.BLUE);
    Northeast2.setFilled(false);
    add(Northeast2);
    squarelist.add(Northeast2);

    Northeast3 = new GRect(420, 160, 50, 10);
    Northeast3.setColor(Color.BLUE);
    Northeast3.setFilled(false);
    add(Northeast3);
    squarelist.add(Northeast3);

    Southwest = new GRect(50, 535, 180, 10);
    Southwest.setColor(Color.BLUE);
    Southwest.setFilled(false);
    add(Southwest);
    squarelist.add(Southwest);

    Southwest2 = new GRect(150, 500, 10, 35);
    Southwest2.setColor(Color.BLUE);
    Southwest2.setFilled(false);
    add(Southwest2);
    squarelist.add(Southwest2);

    Southwest3 = new GRect(50, 470, 80, 10);
    Southwest3.setColor(Color.BLUE);
    Southwest3.setFilled(false);
    add(Southwest3);
    squarelist.add(Southwest3);

    Southwest4 = new GRect(120, 480, 10, 30);
    Southwest4.setColor(Color.BLUE);
    Southwest4.setFilled(false);
    add(Southwest4);
    squarelist.add(Southwest4);

    Southwest5 = new GRect(150, 470, 90, 10);
    Southwest5.setColor(Color.BLUE);
    Southwest5.setFilled(false);
    add(Southwest5);
    squarelist.add(Southwest5);

    Southwest6 = new GRect(30, 500, 65, 10);
    Southwest6.setColor(Color.BLUE);
    Southwest6.setFilled(false);
    add(Southwest6);
    squarelist.add(Southwest6);

    Southeast = new GRect(300, 535, 188, 10);
    Southeast.setColor(Color.BLUE);
    Southeast.setFilled(false);
    add(Southeast);
    squarelist.add(Southeast);

    Southeast2 = new GRect(370, 500, 10, 35);
    Southeast2.setColor(Color.BLUE);
    Southeast2.setFilled(false);
    add(Southeast2);
    squarelist.add(Southeast2);

    Southeast3 = new GRect(290, 470, 90, 10);
    Southeast3.setColor(Color.BLUE);
    Southeast3.setFilled(false);
    add(Southeast3);
    squarelist.add(Southeast3);

    Southeast4 = new GRect(400, 470, 88, 10);
    Southeast4.setColor(Color.BLUE);
    Southeast4.setFilled(false);
    add(Southeast4);
    squarelist.add(Southeast4);

    Southeast5 = new GRect(400, 480, 10, 30);
    Southeast5.setColor(Color.BLUE);
    Southeast5.setFilled(false);
    add(Southeast5);
    squarelist.add(Southeast5);

    Southeast6 = new GRect(435, 500, 75, 10);
    Southeast6.setColor(Color.BLUE);
    Southeast6.setFilled(false);
    add(Southeast6);
    squarelist.add(Southeast6);

    pacman = new Pacman();
    add(pacman);
    pacman.setLocation(257, 483);





    t = new SwingTimer(10, this);
    t.start();

    addMouseListeners();
    addKeyListeners();
    }


    public void keyPressed(KeyEvent e)
    {
    if(e.getKeyCode() == KeyEvent.VK_RIGHT)
    {
    smoothright = true;
    smoothup = false;
    smoothleft = false;
    smoothdown = false;
    }

    if(e.getKeyCode() == KeyEvent.VK_LEFT)
    {
    smoothleft = true;
    smoothup = false;
    smoothright = false;
    smoothdown = false;
    }

    if(e.getKeyCode() == KeyEvent.VK_UP)
    {
    smoothup = true;
    smoothright = false;
    smoothleft = false;
    smoothdown = false;
    }

    if(e.getKeyCode() == KeyEvent.VK_DOWN)
    {
    smoothdown = true;
    smoothup = false;
    smoothright = false;
    smoothleft = false;

    }
    }

    public void actionPerformed(ActionEvent e)
    {
    if(smoothright == true)
    {
    right = true;

    if(right == true)
    {
    if(pacman.getX() + 40 >= 530)
    {
    }
    else
    {
    pacman.move(2, 0);
    }
    }

    int i = 0;
    while(i < squarelist.size())
    {
    if(pacman.getBounds().intersects(squarelist.get(i) .getBounds()))
    {
    right = false;
    }
    i = i + 1;
    }

    if(right == false)
    {
    pacman.move(-2, 0);
    }

    right = true;
    }

    if(smoothleft == true)
    {
    left = true;

    if(left == true)
    {
    if(pacman.getX() <= 33)
    {
    }
    else
    {
    pacman.move(-2, 0);
    }
    }

    int i = 0;
    while(i < squarelist.size())
    {
    if(pacman.getBounds().intersects(squarelist.get(i) .getBounds()))
    {
    left = false;
    }
    i = i + 1;
    }

    if(left == false)
    {
    pacman.move(2, 0);
    }


    left = true;
    }

    if(smoothup == true)
    {
    up = true;

    if(up == true)
    {
    if(pacman.getY() <= 43)
    {
    }
    else
    {
    pacman.move(0, -2);
    }
    }

    int i = 0;
    while(i < squarelist.size())
    {
    if(pacman.getBounds().intersects(squarelist.get(i) .getBounds()))
    {
    up = false;
    }
    i = i + 1;
    }
    if(up == false)
    {
    pacman.move(0, 2);
    }

    up = true;
    }

    if(smoothdown == true)
    {
    down = true;

    if(down == true)
    {
    if(pacman.getY() >= 548.1)
    {
    }
    else
    {
    pacman.move(0, 2);
    }
    }

    int i = 0;
    while(i < squarelist.size())
    {
    if(pacman.getBounds().intersects(squarelist.get(i) .getBounds()))
    {
    down = false;
    }
    i = i + 1;
    }

    if(down == false)
    {
    pacman.move(0, -2);
    }

    down = true;
    }

    }
    }


    import java.awt.*;
    import acm.graphics.*;


    public class Background extends GCompound
    {
    public Background()
    {
    GRect Board = new GRect(0, 0, 800, 650);
    Board.setColor(Color.BLACK);
    Board.setFilled(true);
    add(Board);
    }
    }


    import java.awt.*;
    import acm.graphics.*;

    public class Pacman extends GCompound
    {

    public Pacman()
    {
    GOval pacman = new GOval(0, 0, 15, 15);
    add(pacman);
    pacman.setFilled(true);
    pacman.setColor(Color.YELLOW);
    }
    }

  2. #2
    dmacvittie is offline Member
    Join Date
    Aug 2007
    Posts
    30
    Rep Power
    0

    Default

    Too. Much. Source.

    Short answer (not looking too close at your source), you need to:
    1) Create a timer
    2) On timer events, generate a random number 1 to 4.
    3) Map those numbers to the cardinal directions and move the object.

    Then the timer will go off again, and you can hit #2 again...

    Don.
    Don MacVittie F5 Networks - DevCentral

  3. #3
    whdbstjr90 is offline Member
    Join Date
    Dec 2007
    Posts
    3
    Rep Power
    0

    Default s

    I've never learned that all what I know is that i gotta use random generator... :(

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    whdbstjr90, normally I wouldn't post just to say this, but since you posted so much code, please use code tags when posting your code. In fact, I imagine it's still not too late to use them, simply edit your post and make the tag correction. Thank you.

  5. #5
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Threads and Math.random()

    Hello

    I think you should use threads instead of timers. (Not that I have seen a timer component in java) If you need to do something randomly, use Math.random(). The method is fully explained by its Javadoc.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. bouncing ball issue
    By adam405 in forum New To Java
    Replies: 1
    Last Post: 03-18-2008, 04:48 AM
  2. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 10:08 PM
  3. Bouncing Ball Just Suddenly Stops Mid Bounce
    By adlb1300 in forum Java 2D
    Replies: 1
    Last Post: 12-03-2007, 03:58 PM
  4. Replies: 2
    Last Post: 12-01-2007, 08:48 AM
  5. Accessing a file randomly
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 09:15 PM

Posting Permissions

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