2 Replies - 1953 Views - Last Post: 08 May 2013 - 11:44 AM Rate Topic: -----

#1 javoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-May 13

Java Paddle Ball Game Problem

Posted 08 May 2013 - 11:27 AM

Hi,
I am making a simple breakout game in Java using an applet.
The problem I'm having is when the game starts the ball drops from the top, which renders the game kind of pointless as it destroys blocks on the way down, and when the ball misses the paddle it reappears at the top, which destroys more blocks, meaning that you barely get a chance to play the game. How can I make the ball start at the bottom and also reappear there?
I have included my main class and ball class.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;



public class Main extends Applet implements Runnable, KeyListener 
{
	//Double buffering variables
	private Image dbImage;
	private Graphics dbGraphics;

	private Thread th = new Thread(this);
	private boolean running = true;
	
	private int fps = 50;
	private int score = 0;
	private int blocksKilled = 0;
	private int totalBlocks = 0;
	
	private Ball ball;
	private Paddle paddle;
	private ArrayList <Block> blocks = new ArrayList<Block>();
	
	
	
	private String level[] = {".........................",
							  ".........................",
							  "b....b....b....b.........",
							  ".........................",
							  "..b....b....b............",
							  ".........................",
							  "b....b....b....b.........",
							  ".........................",
							  "..b....b....b............",
							  ".........................",
							  "b....b....b....b.........",
							  ".........................",
							  ".........................",
							  ".........................",
							  ".........................",
							  ".........................",
							  ".........................",
							  ".........................",
							  ".........................",
							  ".........................",
							  ".........................",
							  ".........................",
							  ".........................",
							  ".........................",
							  ".........................", 
};
	
	
	public void init()
	{
	 ball = new Ball(getSize().width, 1, 10, Color.white);
	 paddle = new Paddle(0, getSize().height-32);
	 
	int y;
	for(y = 0; y < level.length; y++)
	 {
		 for(int x =0; x < level[y].length(); x++)
		 {
			 if(level[y].charAt(x) == 'b')
			 {
			 blocks.add(new Block(x*16,y*16));
			 totalBlocks++;
			 }
			 
			 
		 }
	 }
			 
			 
	 addKeyListener(this);
	}
	
	public void start()
	{
		th.start();
		
	}
	
	public void stop()
	{
		running = false;
	}
	
	public void destroy()
	{
		running = false;
		
	}
	
	public void run()
	{
		while(running)
		{
			ball.move(getSize(), paddle);
			paddle.move(getSize().width);
			
			for(int i = 0; i < blocks.size(); i++)
			{
				ball.checkCollision(blocks.get(i));
				if(!blocks.get(i).alive)
				{
					blocks.remove(i);
					score ++;
					blocksKilled++;
				}
					
			}
			
			repaint();
			try
			{
				Thread.sleep(1000/fps);
			}
			catch(Exception e)
		{
				}
		}
	}
	
	public void update (Graphics g)
	{
	if (dbImage == null)
	{
		dbImage = createImage(this.getSize().width, this.getSize().height);
		dbGraphics = dbImage.getGraphics();
	}
	
	dbGraphics.setColor(this.getBackground());
	dbGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);

	
	dbGraphics.setColor(this.getForeground());
	paint(dbGraphics);
	
	g.drawImage(dbImage,0,0,this);
	
	}

	public void paint(Graphics g)
	{
		g.fillRect(0, 0, getSize().width, getSize().height);
	ball.draw(g);
	paddle.draw(g);
	for(Block b:blocks)
	{
		b.draw(g);
	}
	
		g.setColor(Color.gray);
		g.drawString("Score: " +score, 10,20);
		g.drawString(blocksKilled+"/"+totalBlocks,350,20);
	}

	public void keyPressed(KeyEvent e)
	{
		if(e.getKeyCode() == KeyEvent.VK_LEFT)
		{
			paddle.goingLeft = true;
		}
		
		if(e.getKeyCode() == KeyEvent.VK_RIGHT)
		{
			paddle.goingRight = true;
		}
	}

	public void keyReleased(KeyEvent e)
	{
	
		if(e.getKeyCode() == KeyEvent.VK_LEFT)
		{
			paddle.goingLeft = false;
		}
		
		if(e.getKeyCode() == KeyEvent.VK_RIGHT)
		{
			paddle.goingRight = false;
		}
}
	public void keyTyped(KeyEvent e)
	{
		
	}
}



import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.*;

public class Ball 
{

	private double x=0, y =0, xspeed =4, yspeed =4, radius =15;
	private Color color = Color.blue;
	private Ellipse2D.Double bounds;
	private Random rand = new Random();
	
	public Ball(int x, int y)
	{
		this.x = x;
		this.y = y;

	}
	
	public Ball(int x, int y, int radius, Color color)
	{
		this.x = x;
		this.y = y;
		this.radius = radius; 
		this.color = color;
		
	}
	//Collision
	public void checkCollision(Block B)/>/>
	{
		Rectangle2D.Double self = new Rectangle2D.Double(x-radius, y-radius, radius*2, radius*2);
		Rectangle2D.Double block = new Rectangle2D.Double(b.x,b.y,b.width,b.height);
		if(self.intersects(block))
		{
			b.alive = false;
			
			if(x > b.x && x+radius*2 < b.x +b.width)
			{
				yspeed = -yspeed;
			}
			
			else
			{
				xspeed = -xspeed;
			}
		}
	}
	
	public void move(Dimension dim, Paddle paddle)
	{
		x+= xspeed;
		y+= yspeed;
		
		if(x < 0)
			xspeed = Math.abs(xspeed);
		if (x > dim.width)
			xspeed = -Math.abs(xspeed);
		
		if(y < 0)
			yspeed = Math.abs(yspeed);
		
		
		

		Rectangle2D.Double paddleBounds = new Rectangle2D.Double(paddle.getX(), paddle.getY(), paddle.getWidth(), paddle.getHeight());
		Rectangle2D.Double selfBounds = new Rectangle2D.Double(x-radius,y-radius,radius*2,radius*2);
		if(paddleBounds.intersects(selfBounds))
		{
			yspeed = -Math.abs(yspeed);
			yspeed -= .2;
		
			double dist = x-(paddle.getX() +(paddle.getWidth()/2));
			xspeed = dist/10;
		}
		if (y > dim.height+200)
		{
			y = 0;
		}
	}
	

public void draw(Graphics g)
{
	g.setColor(color);
	g.fillOval((int)(x-radius), (int)(y-radius),(int)(radius*2), (int)(radius*2));
}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Java Paddle Ball Game Problem

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Java Paddle Ball Game Problem

Posted 08 May 2013 - 11:43 AM

You create the Ball

ball = new Ball(getSize().width, 1, 10, Color.white);

at Y = 1 how can you expect it comes from anything else than the top ?

And what is the idea of always making the sp-eed positive ?

		if(x < 0)
			xspeed = Math.abs(xspeed);
		if (x > dim.width)
			xspeed = -Math.abs(xspeed);

		if(y < 0)
			yspeed = Math.abs(yspeed);


if you bounce, your speed sign changes
Was This Post Helpful? 0
  • +
  • -

#3 javoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-May 13

Re: Java Paddle Ball Game Problem

Posted 08 May 2013 - 11:44 AM

thank you for the help, I changed the value, which worked but it still reappears at the top, I know the reason for this is simple but I just can't find it :/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1