Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: java Pong game

  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question java Pong game

    i have a problem with the pong game im making with java...

    i have everything done in the game exept i cant make the ball bounce off the paddle. does anyone know how to do this?
    also, i would like to know how to generate a random number in java

    thanks

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ive figured out how to make a random number...

    double rn=Math.floor(Math.random()*5);

    same as javascript!!! i still need help with the collision thing but now i have yet another question... i have a function called shootBall:

    public void shootBall(){
    double rn1=Math.floor(Math.random()*2);
    double rn2=Math.floor(Math.random()*2);
    x_pos=150;
    y_pos=150;
    if(rn1==0.0)x_speed=1;
    if(rn1==1.0)x_speed=-1;
    if(rn2==0.0)y_speed=1;
    if(rn2==1.0)y_speed=-1;
    }

    x_speed is the balls x speed and y_speed is the balls y speed.

    it works for the most part but the ball will only will shoot right... how do i fix this?

    again thanks...

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello warhammerdude20,

    I am very interesting on how to create the game, can you send me your code?

    you can email to me weronpc@yahoo.ca

    Thank you.

  • #4
    New to the CF scene
    Join Date
    May 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    pong game error

    hi guys...i am thru wth the code for pong game but i have a few prblems..
    whenever my ball is hitting or missing the score doesnt changes on the panel instead of that it changes on the ms dos window..can anyone help me in this...also i am not able to change the color of the ball and bar and background does anyone knows about this???

  • #5
    Senior Coder twodayslate's Avatar
    Join Date
    Mar 2007
    Location
    VA
    Posts
    1,042
    Thanks
    67
    Thanked 39 Times in 39 Posts
    Here is how I do random numbers.
    http://java.sun.com/j2se/1.4.2/docs/...il/Random.html
    Code:
    import java.util.Random;
    Random gen = new Random();
    double random double = gen.nextDouble() * 50; //Random double of 1-50
    twitter | Quality Hosting - $5.95/mo*
    Feel free to PM me!

  • #6
    Regular Coder
    Join Date
    May 2008
    Posts
    187
    Thanks
    7
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by samamateurcoder View Post
    hi guys...i am thru wth the code for pong game but i have a few prblems..
    whenever my ball is hitting or missing the score doesnt changes on the panel instead of that it changes on the ms dos window..can anyone help me in this...also i am not able to change the color of the ball and bar and background does anyone knows about this???
    Post up some code and we might be able to help.


  •  

    Posting Permissions

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