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 2 of 2

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
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 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.

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, 01:00 PM
  3. Pong game - Collision detection
    By Hokap in forum Java Theory & Questions
    Replies: 73
    Last Post: May 13th, 2012, 04: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, 06: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, 01:19 PM

Tags for this Thread