Wednesday, November 18, 2009

Developing a Multiplayer Maze Game in Java - Part 2

A game can not be completed without a sprite. Now, what this sprite thing. If you already know then you can skip the definition below


Sprite : A 2D or 3D character/object in game.


A sprite can have various properties like its position, name, features, memory etc. The class below represents a sprite which has following properties -


int x; // x - position
int y; // y - position
double theta; // direction
String name; // name of sprite
boolean explored[][]; //memory of explored map
BufferedImage img[]; //face for our sprite

----------------------------
Code for Part two
--------------------------------
import java.awt.image.*;


class sprite{
int x;
int y;
double theta;
String name;
boolean explored[][];
BufferedImage img[];

public  sprite(String n, int x1,int y1){
name = n;
x=x1;
y=y1;
theta = 0;
explored = new boolean[20][20];
img = new BufferedImage[4];
explored[x][y] = true;
}

Developing a Multiplayer Maze Game in Java - Part 1

In this part we will learn how to make a full screen window in Java. Playing game in a full screen window is always a different experience and Java provide a very clean way of doing so. The code below is pretty easy to understand. FullFrame class can be used as a base window for any java game you want to build.
----------------------------------------------
Code for part one
----------------------------------------------
import javax.swing.*;
import java.awt.*;

class FullFrame extends JFrame{

  private GraphicsDevice gd;
  
public FullFrame()
  {
  }


  public void initFullScreen()
  {
  GraphicsEnvironment ge =
    GraphicsEnvironment.getLocalGraphicsEnvironment( );
  gd = ge.getDefaultScreenDevice( );
   
setUndecorated(true);  
setIgnoreRepaint(true);
setResizable(false);
  if (!gd.isFullScreenSupported( )) 
    {
   Toolkit tk = Toolkit.getDefaultToolkit( );
    Dimension scrDim = tk.getScreenSize( );
setVisible(true);
     setSize(scrDim);  
  }
  gd.setFullScreenWindow(this);
  }
}

----------------------------------------------