Results 1 to 3 of 3
  1. #1
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default Dodging Game Applet - Suggestions?

    I have created a game where you dodge balls that bounce around the screen. Any suggestions to simplify or add things to the game?

    Here is the code:

    Start.java

    Java Code:
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.Random;
    
    public class Start extends Applet implements Runnable, KeyListener {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private Image i;
    	private Graphics doubleG;
    	private collisionChecker c;
    	Player p;
    	private Ball b[] = new Ball[4];
    	int score = 0;
    	String scoreStr;
    	double newDx,newDy;
    	private int width = 800;
    	private int height = 600;
    	private int scoredec = 0;
    	public boolean collision = false;
    	public void init() {
    		//starting settings
    		setSize(width,height);
    		setFocusable(true);
    		addKeyListener(this);
    	}
    	public void start() {
    		//initializing all of the objects of classes
    		for(int i = 0; i < b.length; i++){
    			Random r = new Random();
    			b[i] = new Ball(r.nextInt(700) + 50,r.nextInt(500) + 50,r.nextInt(10) + 5,r.nextInt(5)+1);
    		}
    		c = new collisionChecker();
    		p = new Player();
    		Thread thread = new Thread(this);
    		thread.start();
    	}
    	public void run() {	
    		while(true){
    		//updating movement of ball,player, and collision checker
    		p.update(this);
    		for(int i =0; i <b.length; i++){
    				b[i].update(this);
    			}
    		for(int x = 0; x < b.length;x++){
    			c.update(b[x], p);
    		}
    			//score adding
    			scoreAdd();
    			
    			repaint();
    			//sleeping for FPS
    			try {
    				Thread.sleep(17);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	public void scoreAdd(){
    		if(!collision){
    			score += 1;
    			scoreStr = Integer.toString(score);
    		}
    	}
    	
    	public boolean isCollision() {
    		return collision;
    	}
    	public void setCollision(boolean collision) {
    		this.collision = collision;
    	}
    	public void destroy() {}
    	public void stop() {}
    	//double buffer
    	public void update(Graphics g) {
    	if(i == null){	
    		i = createImage(this.getSize().width, this.getSize().height);
    		doubleG = i.getGraphics();
    	}
    	doubleG.setColor(getBackground());
    	doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);
    	doubleG.setColor(getForeground());
    	paint(doubleG);
    	g.drawImage(i,0,0,this);
    	}
    	//painting the balls and player
    	public void paint(Graphics g){
    		g.setColor(Color.GRAY);
    		g.fillRect(0, 0, getWidth(), getHeight());
    		p.paint(g);
    	//painting all of the balls
    		for(int i = 0;i < b.length;i++){
    		b[i].paint(g);
    	}
    		//score display
    		g.setColor(Color.GREEN);
    		g.setFont(new Font("Arial",Font.PLAIN,20));
    		g.drawString(scoreStr, 10, 30);
    		g.setColor(Color.RED);
    	}
    	//Key Listening for player movement
    	public void keyPressed(KeyEvent e) {
    		switch(e.getKeyCode()){
    		case KeyEvent.VK_RIGHT:
    			p.moveRight();
    			break;
    		case KeyEvent.VK_LEFT:
    			p.moveLeft();
    			break;
    		case KeyEvent.VK_UP:
    			p.moveUp();
    			break;
    		case KeyEvent.VK_DOWN:
    			p.moveDown();
    			break;
    		}	
    	}
    	//Key releases for stopping ball
    	public void keyReleased(KeyEvent e) {
    		switch(e.getKeyCode()){
    		case KeyEvent.VK_RIGHT:
    			newDx = 0;
    			 p.setDx(newDx);
    			break;
    		case KeyEvent.VK_LEFT:
    			 newDx = 0;
    			 p.setDx(newDx);
    			break;
    		case KeyEvent.VK_UP:
    			newDy = 0;
    			p.setDy(newDy);
    			break;
    		case KeyEvent.VK_DOWN:
    			newDy = 0;
    			p.setDy(newDy);
    			break;
    		}
    		
    	}
    	public void keyTyped(KeyEvent arg0) {}
    }
    Player.java

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class Player {
    	Start s;
    	Ball b;
    	private int x,y,radius;
    	double dx,dy;
    	
    	public Player() {
    		radius = 30;
    		x = 30;
    		y = 30;
    		dx = 0;
    		dy = 0;
    	}
    	public double getDx() {
    		return dx;
    	}
    	public void setDx(double d) {
    		this.dx = d;
    	}
    	public double getDy() {
    		return dy;
    	}
    	public void setDy(double newDy) {
    		this.dy = newDy;
    	}
    	public void update(Start s){
    		if(x + radius > s.getWidth() || x - radius < 0){
    			dx*=-1;
    		}
    		if(y + radius > s.getHeight() || y - radius < 0){
    			dy*=-1;
    		}
    		x += dx;
    		y += dy;
    	}
    	public int getX() {
    		return x;
    	}
    	public void setX(int x) {
    		this.x = x;
    	}
    	public int getY() {
    		return y;
    	}
    	public void setY(int y) {
    		this.y = y;
    	}
    	public int getRadius() {
    		return radius;
    	}
    	public void setRadius(int radius) {
    		this.radius = radius;
    	}
    	public void moveRight(){
    			dx = 10;
    	}
    	public void moveLeft(){
    		dx = -10;
    	}
    	public void moveUp(){
    		dy = -10;
    	}
    	public void moveDown(){
    		dy = 10;
    	}
    	public void stopMovement(){
    		dy = 0;
    		dx = 0;
    	}
    	public void paint(Graphics g){
    	g.setColor(Color.BLACK);
    	g.fillOval(x - radius, y - radius, radius * 2,radius *2);
    	}	
    }
    Ball.java

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    public class Ball {
    	private int x, y, radius,dx,dy;
    	public Ball(int x, int y,int speedX,int speedY) {
    		this.x = x;
    		this.y = y;
    		radius = 30;
    		dx = speedX;
    		dy = speedY;
    	}
    	public int getX() {
    		return x;
    	}
    	public void setX(int x) {
    		this.x = x;
    	}
    	public int getY() {
    		return y;
    	}
    	public void setY(int y) {
    		this.y = y;
    	}
    	public int getRadius() {
    		return radius;
    	}
    	public void setRadius(int radius) {
    		this.radius = radius;
    	}
    	public void update(Start s){
    		if(x + radius > s.getWidth() || x - radius < 0){
    			dx*=-1;
    		}	
    		if(y + radius > s.getHeight() || y - radius < 0){
    			dy*=-1;
    		}
    		x+=dx;
    		y+=dy;
    	
    	}
    
    	public void paint(Graphics g){
    		g.setColor(Color.blue);
    		g.fillOval(x-radius, y-radius, radius*2, radius*2);
    	}
    	public int getDx() {
    		return dx;
    	}
    	public void setDx(int dx) {
    		this.dx = dx;
    	}
    	public int getDy() {
    		return dy;
    	}
    	public void setDy(int dy) {
    		this.dy = dy;
    	}
    }
    And lastly,
    collisionChecker.java

    Java Code:
    public class collisionChecker {
    private Start s;
    	public void update(Ball b, Player p){
    		int ballX = b.getX();
    		int ballY = b.getY();
    		int ballR = b.getRadius();
    		int playerX = p.getX();
    		int playerY = p.getY();
    		int playerR = p.getRadius();
    		int a = playerX - ballX;
    		int bb = playerY - ballY;
    		int collide = playerR + ballR;
    		double c = Math.sqrt((double) a*a + (double) bb*bb);	
    		if(c < collide - 5){
    			s.setCollision(true);
    			b.setDx(0);
    			b.setDy(0);
    			p.setDx(0);
    			p.setDy(0);
    		}
    	}	
    }
    Thank you all for the help!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: Dodging Game Applet - Suggestions?

    Is there a place we can play this to see the current progress? Looking at the code, it's hard to tell exactly what the game is about.

    Shameless self-promotion: if you're looking for a host for your games, check out the link in my signature!
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Dodging Game Applet - Suggestions?

    I slowed down the app to watch it. The balls don't appear to bounce off of each other. And you may want to repaint only updated graphics using a clip region.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Game "Grid" idea need suggestions...
    By Alkor in forum Java Gaming
    Replies: 3
    Last Post: 03-01-2012, 09:32 AM
  2. Managing my Game State... suggestions?
    By 0026sd in forum Java Gaming
    Replies: 5
    Last Post: 12-07-2011, 08:06 PM
  3. Simple Dodging Game
    By Tb0h in forum New To Java
    Replies: 1
    Last Post: 07-18-2009, 02:30 AM
  4. Beginning Game Development - Suggestions?
    By JDCAce in forum Advanced Java
    Replies: 1
    Last Post: 12-08-2008, 05:42 PM
  5. An Applet Game
    By T3000 in forum New To Java
    Replies: 1
    Last Post: 11-18-2008, 04:18 PM

Posting Permissions

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