Hi.
In my last blog I described how we can control a car using the Sun SPOTs. Here I will describe how we can make a silly 2 Player Game using Sun Spots and Basestation.
What will we do..
Here we will build a simple 2-D Game that will have 9 X 9 Grid. There will be two Balls - Greeen and Red. The Movements of the balls in the grid will be controlled by Sun SPOTs!
If the dots overlap, a message will be displayed. The goal of Green Dot is to catch the Red Dot!!(I know its really silly ;-))
Note that this is a simple prototype to demonstrate how we can control games using Sun SPOT.
Requirements:
Whole Sun Spot Development Kit.
Building the Remote SPOT Application.
Remote Spot App will contain following code:
/**
*
* @author Jay Mahadeokar
*/
public class StartApp extends MIDlet {
protected void startApp() throws MIDletStateChangeException {
EDemoBoard demoboard = EDemoBoard.getInstance();
IAccelerometer3D acc = demoboard.getAccelerometer();
while(true)
{
try{
RadiogramConnection conn = (RadiogramConnection)Connector.open("radiogram://0014.4F01.0000.1231:10");
Radiogram rdg = (Radiogram)conn.newDatagram(conn.getMaximumLength());
try
{
rdg.writeUTF(""+demoboard.getAccelerometer().getTiltX()+","+demoboard.getAccelerometer().getTiltY());
conn.send(rdg);
Utils.sleep(100);
}
catch (NoRouteException e)
{
}
finally
{
conn.close();
}
}
catch(IOException e){
}
}
}
protected void pauseApp() {
// This will never be called by the Squawk VM
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// Only called if startApp throws any exception other than MIDletStateChangeException
}
}
Building the Host Application
The Host Application will contain following Four Files:
1. Configuration.java
/**
* @author Jay Mahadeokar
*/
public class Configuration {
//Number of rows in the Grid
public static int MAXROWS = 9;
//Number of columns in the Grid
public static int MAXCOLUMNS = 9;
}
2. GridPanel.java
/**
* @author Jay Mahadeokar
*/
public class GridPanel extends JPanel
{
public PlayBoard playboard = new PlayBoard();
double xInc, yInc;
final int
ROWS = Configuration.MAXROWS,
COLUMNS = Configuration.MAXCOLUMNS,
DRAW = 0,
FILL = 1,
PAD = 20;
public GridPanel()
{
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
double w = getWidth();
double h = getHeight();
xInc = (w - 2*PAD)/COLUMNS;
yInc = (h - 2*PAD)/ROWS;
// row lines
double x1 = PAD, y1 = PAD, x2 = w - PAD, y2 = h - PAD;
for(int j = 0; j <= ROWS; j++)
{
g2.draw(new Line2D.Double(x1, y1, x2, y1));
y1 += yInc;
}
// col lines
y1 = PAD;
for(int j = 0; j <= COLUMNS; j++)
{
g2.draw(new Line2D.Double(x1, y1, x1, y2));
x1 += xInc;
}
x1 = PAD + playboard.xPos1 * xInc + 1;
y1 = PAD + playboard.yPos1 * yInc + 1;
g2.setPaint(Color.red);
g2.fill(new Ellipse2D.Double(x1+5, y1+5, xInc - 15, yInc - 15));
x1 = PAD + playboard.xPos2 * xInc + 1;
y1 = PAD + playboard.yPos2 * yInc + 1;
g2.setPaint(Color.GREEN);
g2.fill(new Ellipse2D.Double(x1+5, y1+5, xInc - 15, yInc - 15));
}
public void check()
{
if(playboard.check())
JOptionPane.showMessageDialog(this,"Caught!!!");
}
}
3. PlayBoard.java
/**
*
* @author Jay Mahadeokar
*/
public class PlayBoard {
public int xPos1 = 5,yPos1 = 5;
public int xPos2 = 4,yPos2 = 4;
public PlayBoard()
{
}
public void move1(int x,int y)
{
if(x==1)
if(x+xPos1<Configuration.MAXCOLUMNS)
xPos1++;
if(x==-1)
if(xPos1>0)
xPos1--;
if(y==1)
if(y+yPos1<Configuration.MAXROWS)
yPos1++;
if(y==-1)
if(yPos1>0)
yPos1--;
System.out.println("x1: "+x+" y1: "+y);
}
public void move2(int x,int y)
{
if(x==1)
if(x+xPos20)
xPos2--;
if(y==1)
if(y+yPos20)
yPos2--;
}
public boolean check()
{
if(xPos1 == xPos2 && yPos1 == yPos2)
{
xPos1 = 4; yPos1 = 4;
xPos2 = 5;
yPos2 = 5;
return true;
}
else
return false;
}
}
4. SunSpotHostApplication
/**
*
* @author Jay Mahadeokar
*/
public class SunSpotHostApplication {
public void runGame()
{
GridPanel gridPanel = new GridPanel();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(gridPanel);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
while(true)
{
try
{
RadiogramConnection conn = (RadiogramConnection) Connector.open("radiogram://:10");
Radiogram rdg = (Radiogram)conn.newDatagram(conn.getMaximumLength());
try
{
conn.receive(rdg);
System.out.println(rdg.getAddress());
String s = rdg.readUTF();
System.out.println(s);
String accArr[] = s.split(",");
double x = Double.parseDouble(accArr[0]);
double y = Double.parseDouble(accArr[1]);
System.out.println("xtilt: "+x+", ytilt: "+y);
int passx = 0,passy = 0;
if(x>0.5)
passx = 1;
if(x<-0.5)
passx = -1;
if(y>0.5)
passy = 1;
if(y<-0.5)
passy = -1;
if(rdg.getAddress().equals("0014.4F01.0000.12B6"))
gridPanel.playboard.move1(passx, passy);
if(rdg.getAddress().equals("0014.4F01.0000.1A29"))
gridPanel.playboard.move2(passx, passy);
gridPanel.repaint();
gridPanel.check();
}
catch (NoRouteException e)
{
}
finally
{
conn.close();
}
}
catch(Exception e){
}
}
}
public static void main(String[] args) {
SunSpotHostApplication app = new SunSpotHostApplication();
app.runGame();
}
}
Deploy the Spot Applications on Sun Spots. Connect the Basestation and Run the Host Application!
PS: Though the game looks really stupid, we can use it as a platform to control better games having better GUI's. We have the basic logic ready to control anything using Sun Spots. We just need to do the tedious Game Building Code now!
We are currently working on it!! Stay tuned!!!
Cheers!