Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  SOLVED Problem with a paddle for pong game  (Read 751 times)
0 Members and 1 Guest are viewing this topic.
Offline Majeh

Senior Newbie





« Posted 2011-03-23 20:44:05 »

I'm making a simple little pong game for my java class but I can't figure out how to get the paddle on the right side to bounce the ball back over to the left side, the ball just goes through the paddle. So here's some of the code I got and a screenshot.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
int x_pos = 100;
    int y_pos = 100;
    int x_width = 30;
    int y_height = 30;
    int right = 5;
    int left = -5;
    int up = -5;
    int down = 5;
    int width, height;
    int player1X = 50;
    int player1Y = 200;
    int player1W = 10;
    int player1H = 100;
    int player2X;
    int player2Y = 200;
    int player2W = 10;
    int player2H = 100;
    int player1Score = 0;
    int player2Score = 0;
    boolean moveBallDown, moveBallRight;
    boolean movePlayer1Up, movePlayer1Down;
    boolean movePlayer2Up, movePlayer2Down;
    boolean game, gameOver;

    public void start ()
    {

        Thread th = new Thread (this);
        th.start ();
        game = true;
    }

    @Override
    public void paintComponent( Graphics g )
    {
        super.paintComponent( g );

        g.setColor(Color.blue);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.black);
        g.fillOval(x_pos, y_pos, x_width, y_height);
        g.fillRect(player1X, player1Y, player1W, player1H);
        g.fillRect(player2X, player2Y, player2W, player2H);
        g.setColor(Color.yellow);
        g.drawString("Player1 Score: ", 20, 10);
        g.drawString(Integer.toString(player1Score), 105, 10);
        g.drawString("Player2 Score: ", getWidth() - 150, 10);
        g.drawString(Integer.toString(player2Score), getWidth() - 70, 10);

        if (gameOver)
            g.drawString("GAME OVER", getWidth()/2-30, getHeight()/2);


    }

    public void drawShape (int new_x, int new_y)
    {
        x_pos = new_x;
        y_pos = new_y;
    }

    public void drawPlayer1 ( int new_y)
    {
       
        player1Y = new_y;
    }

    public void drawPlayer2 ( int new_y)
    {
        player2X = getWidth() - 50;
        player2Y = new_y;
    }
   

    public void run ()
    {

        moveBallRight = true;
        moveBallDown = true;

        while (game)
        {

            moveShape();
            drawShape(x_pos, y_pos);
            movePlayer();
            drawPlayer1(player1Y);
            drawPlayer2(player2Y);
            repaint();

            try
            {
                Thread.sleep (25);
            }
            catch (InterruptedException ex){}

            if (player1Score == 10 || player2Score == 10)
            {
                game = false;
                gameOver = true;
            }

        }
       
    }


    public void moveShape()
    {

        if(moveBallRight)
        {
            x_pos += right;
            if (x_pos >= (getWidth() - x_width)){
                moveBallRight = false;
                player1Score++;
            }
        }
        else
        {
            x_pos += left;
            if ( x_pos <= 0){
                moveBallRight =  true;
                player2Score++;
            }
        }
        if(moveBallDown)
        {
            y_pos += down;
            if(y_pos >= (getHeight()-y_height))
                moveBallDown = false;
        }
        else
        {
            y_pos += up;
            if(y_pos <= 0)
                moveBallDown = true;
        }
       
        //bounces ball off left paddle
        if(x_pos == (player1X + player1W) && (y_pos >= player1Y || (y_pos+30) >= player1Y) && y_pos <= (player1Y+75))
            moveBallRight = true;
        //Supposed to bounce ball off right paddle
        if((x_pos + x_width) == player2X && (y_pos >= player2Y || (y_pos+30) >= player2Y) && y_pos <= (player2Y+75))
            moveBallRight = false;


    }




Just to clarify a couple things x/y_pos is the starting point to draw the ball player x/y is the starting point for the paddles. I have getWidth() for the right paddle in the drawPlayer2 if whoever runs this makes the window bigger.

Anyone know what I'm doing wrong here for the last two lines of code?
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-03-23 23:47:12 »

Well in if statement, if the (x_pos+x_width) is not exactly player2X, it will return false. What if you are 1 pixel off?

Try this:
1  
2  
if((x_pos+x_width) >= player2X && (x_pos+x_width) <= (player2X+player2W) && (y_pos+y_height) >= player2Y && y_pos <= (player2Y+player2H))
   moveBallRight=false;

This will check if he intersects anywhere.

Also, have you checked the java.awt.geom package? There is Ellipse2D and Rectangle2D and you can do ellipse.intersects(rectangle).
http://ra4king.is-a-geek.net/javadocs/java/awt/geom/package-summary.html

Offline Majeh

Senior Newbie





« Reply #2 - Posted 2011-03-24 02:32:52 »

Well in if statement, if the (x_pos+x_width) is not exactly player2X, it will return false. What if you are 1 pixel off?

Try this:
1  
2  
if((x_pos+x_width) >= player2X && (x_pos+x_width) <= (player2X+player2W) && (y_pos+y_height) >= player2Y && y_pos <= (player2Y+player2H))
   moveBallRight=false;

This will check if he intersects anywhere.

Also, have you checked the java.awt.geom package? There is Ellipse2D and Rectangle2D and you can do ellipse.intersects(rectangle).
http://ra4king.is-a-geek.net/javadocs/java/awt/geom/package-summary.html

Thank you for your help! I'll try looking into that some time. I'm thinking of making another simple game soon. Thanks again for your help Tongue
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-03-24 04:50:19 »

Glad to help Grin

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (36 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (56 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (44 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!