Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-13-2007, 12:00 AM
Member
 
Join Date: Dec 2007
Posts: 3
whdbstjr90 is on a distinguished road
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);
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-13-2007, 12:24 AM
Member
 
Join Date: Aug 2007
Posts: 30
dmacvittie is on a distinguished road
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 -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-13-2007, 01:01 AM
Member
 
Join Date: Dec 2007
Posts: 3
whdbstjr90 is on a distinguished road
s
I've never learned that all what I know is that i gotta use random generator...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-31-2007, 06:05 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-31-2007, 06:32 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
bouncing ball issue adam405 New To Java 1 03-18-2008 04:48 AM
Problem deleting ball from bouncing ball app adlb1300 New To Java 2 12-03-2007 10:08 PM
Bouncing Ball Just Suddenly Stops Mid Bounce adlb1300 Java 2D 1 12-03-2007 03:58 PM
Need help making ball move and bounce off of sides of JPanel adlb1300 New To Java 2 12-01-2007 08:48 AM
Accessing a file randomly Java Tip Java Tips 0 11-10-2007 09:15 PM


All times are GMT +3. The time now is 10:29 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org