Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How To Make Pong Ball Slowly Increase Speed

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How To Make Pong Ball Slowly Increase Speed

    I was making a pong game, part copied code off the internet, part my own code, part my friends code. It works well enough, but the ball starts at one speed, and stays the same. I would like to know how to make the ball slowly increase.

    It is a java applet, which shouldn't casue any harm right?

    I am quite newish to programming and i dont know the technigal jargon, so please try to give me an answer in laymans terms or just fix the code period.

    My code:
        /**
     * Pong Applet
     * Coded by Jordan
     */
     
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Pong extends Applet implements MouseMotionListener, KeyListener {
     
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	int my,bx,by,px,py,xcomp,ycomp,width,height,xspeed,yspeed,ballwidth,ballheight,paddlewidth,paddleheight,score;
    	boolean started;
    	private Timer timer1;
     
    	public void init() {
    		setSize(1280,800);
    		width = getSize().width;
    		height = getSize().height;
    		setBackground(Color.white);
    		paddleheight = 120;
    		paddlewidth = 20;
    		ballheight = 30;
    		ballwidth = 30;
    		addKeyListener(this);
    		addMouseMotionListener(this);
    		px = 35;
    		xcomp = width - 35 - paddlewidth;
    		newgame();
    		timer1 = new Timer(10,new ActionListener() {
        		public void actionPerformed(ActionEvent e) {
        			height = getSize().height;
        			width = getSize().width;
    				bx += xspeed;
    				by += yspeed;
    				if (by <= 0 || by + ballheight >= height) {
    					yspeed = -yspeed;
    				}
    				if (bx <= px + paddlewidth && by + ballheight >= py && by <= py + paddleheight && bx > px) {
    					xspeed = -xspeed;
    					++score;
    				}
    				if (bx + ballwidth >= xcomp && by + ballheight >= ycomp && by <= ycomp + paddleheight && bx < xcomp + paddlewidth) {
    					xspeed = -xspeed;
    				}
    				if (xspeed < 0) {
    					if (ycomp + paddleheight / 2 != height / 2) {
    						if (ycomp + paddleheight / 2 > height / 2) {
    							ycomp -= -xspeed;
    						}
    						else {
    							ycomp += -xspeed;
    						}
    					}
    				}
    				else {
    					if (by + ballheight / 2 <= ycomp + paddleheight / 2) {
    						ycomp -= xspeed;
    					}
    					else {
    						ycomp += xspeed;
    					}
    				}
    				if (ycomp < 0) {
    					ycomp = 0;
    				}
    				if (ycomp + paddleheight > height) {
    					ycomp = height - paddleheight;
    				}
    				if (bx + ballwidth < 0) {
    					py = height / 2 - paddleheight / 2;
    					timer1.stop();
    					started = false;
    				}
    				repaint();
        		}    
    		});
    	}
     
    	public void mouseMoved(MouseEvent e) {
    		if (started) {
    			my = e.getY();
    			if (my + paddleheight / 2 > height) {
    				my = height - paddleheight / 2;
    			}
    			if (my < paddleheight / 2) {
    				my = paddleheight / 2;
    			}
    			py = my - paddleheight / 2;
     
    		}
    	}
     
    	public void mouseDragged(MouseEvent e) { }
     
    	public void paint(Graphics g) {
    		Font font1 = new Font("Arial", Font.BOLD, 18);
    		Font font2 = new Font("Arial", Font.BOLD,70);
    		g.setColor(Color.black);
    		g.drawRect(0,0,width - 1,height - 1);
    		g.setColor(Color.gray);
    		g.fillRect(px,py,paddlewidth,paddleheight);
    		g.fillRect(xcomp,ycomp,paddlewidth,paddleheight);
    		g.setFont(font1);
    		g.setColor(Color.black);
    		g.drawString("Points: " + score,20,20);
    		if (started) {
    			g.fillArc(bx,by,ballwidth,ballheight,0,360);
    		}
    		else {
    			g.setFont(font2);
    			g.setColor(Color.blue);
    			g.drawString("Pong",width / 2 - 76,height / 2 - 16);
    			g.setFont(font1);
    			g.setColor(Color.cyan);
    			g.drawString("Use The Mouse To Control The Paddle",width / 2 - 145,height / 2 + 30);
    			g.drawString("Press 'Space' To Start",width / 2 - 85,height / 2 + 50);
    		}
    	}
     
    	public void newgame() {
    		py = height / 2 - paddleheight / 2;
    		ycomp = py;
    		bx = width / 2 - ballwidth / 2;
    		by = height / 2 - ballheight / 2;
    		xspeed = 14;
    		yspeed = 14;
    		score = 0;
    	}
     
    	public void keyPressed(KeyEvent e) {
    		if (e.getKeyChar() == ' ') {
    			started = true;
    			newgame();
    			timer1.start();
    		}
    	}
     
    	public void keyTyped(KeyEvent e) { }
     
    	public void keyReleased(KeyEvent e) { }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,263
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: How To Make Pong Ball Slowly Increase Speed

    how to make the ball slowly increase.
    Increase the distance the ball moves each time interval slowly.
    If the ball is moving 2 units/time unit, change it to move 3 units/time unit then 4 etc
    Make the changes slowly, say every x time units. Experiment to see what gives the effect you want.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How To Make Pong Ball Slowly Increase Speed

    or, if you want a constant acceleration, you could have a second variable to represent a rate of acceleration and use that. if you have taken physics(particularly kinematics and gravity), then this next bit may help spring a few ideas. if not, it may still help a little bit

    in physics, velocity is calculated by a certain equation, velocitycurrent=velocityprior + acceleration*time.
    for example, gravitational acceleration is 9.8 meters/second^2. so say at one second a ball is dropping at 10meters a second. one second later, the ball has accelerated 9.8 meters a second, to produce a velocitycurrent=velocityprior + accelration*time, which would be 10 + 9.8*1, or turn the velocity into 19.8 meters a second.

    since fundamentally adding to the x coordinate or y coordinate by a certain amount is the same as a velocity( say if you add 1 each cycle to the coordinate, your "velocity" left or right would be 1 pixel moved each program cycle.), by constantly increasing the value added by a constant amount, that would produce an acceleration effect. say first i have
    int coordx=0;(represents x coord)
    int spdinc=0;(represents a speed increase)
    at first maybe i want to add one, simple- coordx=coordx+1+spdinc;
    since spdinc is just 0, nothing changed because of it. but to have it change next time, i should have
    coordx=coordx+spdinc+1;
    spdinc++;

    so next it would be again coordx=coordx+spdinc+1; but now that spdinc has changed to 1, now it has become coordx+2 effectively, which caused the "velocity"to "increase".

    by constantly adding a "1" or whatever you want the second value to change by, it will mimic the example of real life of how gravity's acceleration is cumulative.
    the reason this (or something incredibly similar) works is that there is not hardcoding in exactly how much the ball accelerates, the math to do it will be done for you through this
    Hopefully this will help you out onto the right track.

Similar Threads

  1. Pong--paddle collision algorithm help
    By sora628 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 5th, 2013, 09:55 PM
  2. How to defeat computer in a Java game? (Pong)
    By DJBENZ10 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 13th, 2012, 02:00 PM
  3. Pong game - Collision detection
    By Hokap in forum Java Theory & Questions
    Replies: 73
    Last Post: May 13th, 2012, 05:11 PM
  4. [SOLVED] Pong: Ricochet off Paddle Issue
    By Staticity in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 25th, 2011, 07:57 AM
  5. [SOLVED] Fixing of bug for Pong game
    By phoenix in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 14th, 2009, 02:19 PM

Tags for this Thread