game in java

how to code a game in java

Page 1 of 1

2 Replies - 2124 Views - Last Post: 08 July 2009 - 08:25 PM Rate Topic: -----

#1 idasyah   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-July 09

game in java

Post icon  Posted 08 July 2009 - 05:44 AM

:)
Is This A Good Question/Topic? 0
  • +

Replies To: game in java

#2 ayman_mastermind   User is offline

  • human.setType("geek");
  • member icon

Reputation: 127
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: game in java

Posted 08 July 2009 - 05:52 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this: :code:

Btw, please add your question inside the post and not in the description, furthermore please be more specific about what you want and please explain more about what your are looking for.

Thank you for helping us helping you :)
Was This Post Helpful? 1
  • +
  • -

#3 pbl   User is offline

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

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: game in java

Posted 08 July 2009 - 08:25 PM

Your lucky....
a friend of mine had a homework assignment ... to make symbols to turn around in a wheel
it's code worked but I said: balls should slow down when they go up and accelerate when they go down
so this is what I wrote

import java.awt.BorderLayout;

import javax.swing.*;

public class GameFrame extends JFrame {
	private static final int NB_BALLS = 20, DEF_SPEED = 1;
	GameFrame() {
		// frame description
		super("Rotating balls");
		// our Canvas 
		GameCanvas canvas = new GameCanvas(NB_BALLS, DEF_SPEED);
		add(canvas, BorderLayout.CENTER);
		// set it's size and make it visible
		setSize(600+12, 600);
		setVisible(true);		
		// now that is visible we can tell it that we will use 2 buffers to do the repaint
		// befor being able to do that, the Canvas as to be visible
		canvas.createBufferStrategy(2);
	}
	// just to start the application
	public static void main(String[] args) {
		// instance of our stuff
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new GameFrame();
			}
		});
	}	
}



import java.awt.*;
import java.awt.image.BufferStrategy;
import java.util.Random;

import javax.swing.Timer;

// OK this the class where we will draw
public class GameCanvas extends Canvas{
	// the number of balls
	int nbBalls;
	// the initial position of the balls
	int[] ballDegree;
	// the colors
	Color[] color;
	// the deltaDegree we will apply to each balls times the gravity for the angle
	int deltaDegree;
	
	// the size of the ball in pixels
	final int BALLSIZE = 25, HALF_SIZE = BALLSIZE / 2, QUARTER_SIZE = HALF_SIZE / 2;
	// a flag if repaint in progress (needed if our computation are to long)
	boolean repaintInProgress = false;
	// to have the sin/cos/gravity
	SinCosGravity scg = new SinCosGravity();
	
	// this is a Canvas but I wont't let the system when to repaint it I will do it myself
	GameCanvas(int nbBalls, int deltaDegree) {
		super();
		this.nbBalls = nbBalls;
		this.deltaDegree = deltaDegree;
		// so ignore System's paint request I will handle them
		setIgnoreRepaint(true);
		ballDegree = new int[nbBalls];
		color = new Color[nbBalls];
		// determine initial position of the balls
		Random ran = new Random();						// for random color

		int deltaEach = 360 / nbBalls;
		for(int i = 0; i < nbBalls; i++) {
			ballDegree[i] = i * deltaEach;
			color[i] = new Color(ran.nextInt(125), ran.nextInt(125), ran.nextInt(125));
		}
		// a random place to start the ball
		// build Chrono that will call me 
		Chrono chrono = new Chrono(this);
		// ask the chrono to calll me every 60 times a second so every 16 ms
		new Timer(16, chrono).start();
	}
	
	// my own paint method that repaint off line and switch the displayed buffer
	// according to the VBL
	public void myRepaint() {
		// wasting too much time doing the repaint... ignore it
		if(repaintInProgress)
			return;
		// so I won't be called 2 times in a row for nothing
		repaintInProgress = true;
		// get actual Canvas size so I can check if I am out of bounds
		Dimension size = getSize();
		int centerX = size.width / 2 - HALF_SIZE;
		int centerY = size.height / 2 - HALF_SIZE;
		double width = size.width / 3.0;
		double height = size.height / 3.0;
		// ok doing the repaint on the not showed page
		BufferStrategy strategy = getBufferStrategy();
		Graphics graphics = strategy.getDrawGraphics();
		graphics.setColor(Color.YELLOW);
		
		graphics.fillRect(0, 0, size.width, size.height);
		// now we draw the center
		graphics.setColor(Color.BLACK);
		// ce center
		graphics.fillOval(centerX - QUARTER_SIZE, centerY - QUARTER_SIZE, HALF_SIZE, HALF_SIZE);
		for(int i = 0; i < nbBalls; i++) {
			graphics.setColor(color[i]);
			// gravity factor
			int g = scg.gravity(ballDegree[i]);
			// by how much I will increase my degrees
			g *= deltaDegree;
			ballDegree[i] += g;
			ballDegree[i] %= 360;
			double sin = scg.sin(ballDegree[i]);
			double cos = scg.cos(ballDegree[i]);
			sin *= height;
			cos *= width;
			int ballX = (int) Math.floor(cos);
			int ballY = (int) Math.floor(sin);

			graphics.fillOval(ballX + centerX, ballY + centerY, BALLSIZE, BALLSIZE);
		}
		if(graphics != null)
			graphics.dispose();
		// show next buffer
		strategy.show();
		// synchronized the blitter page shown
		Toolkit.getDefaultToolkit().sync();

		// ok I can be called again
		repaintInProgress = false;
	}
}



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/** Will be called at each blitter page */
public class Chrono implements ActionListener {

	GameCanvas gc;
	// constructor that receives the GameCanvas that we will repaint every 60 milliseconds
	Chrono(GameCanvas gc) {
		this.gc = gc;
	}
	// calls the method to repaint the anim
	public void actionPerformed(ActionEvent arg0) {
		gc.myRepaint();
	}

}



public class SinCosGravity {

	// for how much I increment for every 30 degrees
	static final int[] weight = {2, 2, 1, 1, 2, 2, 3, 3, 4, 4, 3, 3};
	static double[] sinValue = new double[360];
	static double[] cosValue = new double[360];
	static int[] gravityValue = new int[360];
	
	// all instances share this array 
	static {
		for(int i = 0; i < 360; i++) {
			// have to convert in radian
			double radiant = -Math.toRadians((double) i);
			// because Java sin/cos method use radian
			sinValue[i] = Math.sin(radiant);
			cosValue[i] = Math.cos(radiant);
			gravityValue[i] = weight[i / 30];
		}
	}
	// ok just a constructor
	SinCosGravity() {	
	}
	
	double sin(int degree) {
		return sinValue[degree % 360];
	}
	double cos(int degree) {
		return cosValue[degree % 360];
	}
	int gravity(int degree) {
		return gravityValue[degree % 360];
	}
}



Have fun :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1