maandag 30 mei 2011

RTS Selecting Units #1 - Mouse Selecting rectangle


Press Mouse and Drag it on the Applet  to create a selection rectangle which can be used to select the Units in Real Time Strategy Games.

I had a little trouble figuring out how to do this since I forgot to check all the mouse features that Java has and forgot about the Mouse Drag method. Once I found that one I had no trouble creating the selection part. I made things like this before for simple RTS like games.

In the code I change the selection coordinates if they are drawn into negative. Meaning if the x or y starting point is smaller then the drag coordinates. This because you need to draw a rectangle in a certain way.
 

import java.awt.*;
import java.applet.*;

public class RTSSelectingUnits01 extends Applet implements Runnable {
 // Graphics for double buffering.
 Graphics      bufferGraphics;
    Image       offscreen;
 int mousebutton =    0;
 int selx1 =     0;
 int sely1 =     0;
 int selx2 =     0;
 int sely2 =     0;
 boolean drawselectrect =  false;

 public void init() {
     setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();
     new Thread(this).start();

 }

    public void run() {
        for(;;) { // animation loop never ends
         repaint();
         try {
             Thread.sleep(10);
             }
             catch (InterruptedException e) {
             }
     }
    }

   public boolean mouseMove(Event e, int x, int y){
        selx2 = x;
        sely2 = y;
  return true;
 }

    public void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
        bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Selecting Units RTS.",10,10);

  if(drawselectrect == true ){
   bufferGraphics.setColor(new Color(200,200,200));
   int tx1 = selx1;
   int ty1 = sely1;
   int tx2 = selx2;
   int ty2 = sely2;
   if (selx1 > selx2){
    tx1 = selx2;
    tx2 = selx1;
   }
   if (sely1 > sely2){
    ty1 = sely2;
    ty2 = sely1;
   }
   bufferGraphics.drawRect(tx1,ty1,tx2-tx1,ty2-ty1);
  }
  bufferGraphics.drawString(""+selx2+","+sely2,10,20);
       g.drawImage(offscreen,0,0,this);
    }

  public boolean mouseDown (Event e, int x, int y) {
        if (e.modifiers == Event.META_MASK) {
            //info=("Right Button Pressed");
         mousebutton = 2;
        } else if (e.modifiers == Event.ALT_MASK) {
            //info=("Middle Button Pressed");
        } else {
            //info=("Left Button Pressed");
            mousebutton = 1;
            selx1 = x;
            sely1 = y;
       drawselectrect = true;
        }

        return true;
    }

  public boolean mouseUp (Event e, int x, int y) {
        if (e.modifiers == Event.META_MASK) {
            //info=("Right Button Pressed");
         mousebutton = 0;
        } else if (e.modifiers == Event.ALT_MASK) {
            //info=("Middle Button Pressed");
        } else {
            //info=("Left Button Pressed");
            mousebutton = 0;
            drawselectrect = false;

       }
        return true;
    }
  public boolean mouseDrag(Event e, int x, int y){
   selx2 = x;
   sely2 = y;
   return true;
  }


}

maandag 23 mei 2011

Platformer Spikes Example


Use Cursors Left and Right to move. Space to Jump. Avoid being spiked.
 

import java.awt.*;
import java.applet.*;
import java.util.Random;
import java.awt.geom.Area;

public class PlatformerSpikes01 extends Applet implements Runnable {
 Random    r =     new Random();
 // Graphics for double buffering.
 Graphics    bufferGraphics;
    Image     offscreen;
 Image     spikeimage;
 private short map[][]={
      {1,1,1,1,1,1,1,1,1,1,1,1,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,2,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,2,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,2,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,1,1,1,1,1,1,1,1,1,1,1,1}
      };
 int     mapwidth =    20;
 int      mapheight =   13;
 int      cellwidth =   16;
 int      cellheight =   16;
 int     pstartx =    32;
 int     pstarty =    128;
 double     px =     pstartx;
 double    py =    pstarty;
 int     pwidth =    cellwidth/2;
 int     pheight =    cellheight;
 boolean    isjumping =   false;
 boolean    isfalling =   false;
 double    gravity =    0;
 boolean    ismovingright =  false;
 boolean    ismovingleft =   false;
 double    jumpforce =   3;
 int     sx1 =    3;
 int     sy1 =    0;
 int     sx2 =    7;
 int     sy2 =    32;
 int     sx3 =     0;
 int     sy3 =    32;
 short    maxnumspikes =  32;
 short     spikewidth =   8;
 short    spikeheight =   32;
 double     spikes[][] =   new double[maxnumspikes][9];  // 0 - active , 1 = x
                   // 2 = y , 3 - incx , 4 - incy
                   // 5 - addx , 6 = addy
                   // 7 - spikewait , 8 - up(1)/down(2)

 public void init() {
     setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();

  spikeimage = createImage(8,32);
  Graphics test1 = spikeimage.getGraphics();
  int[] XArray = {sx1,sx2,sx3};
     int[] YArray = {sy1,sy2,sy3};
     test1.setColor(Color.red);
     test1.fillPolygon (XArray, YArray, 3);

  initmap();
  new Thread(this).start();

 }

 public void initmap(){
  int cnt = 0;
  for ( int y = 0 ; y < mapheight ; y++){
   for ( int x = 0 ; x < mapwidth ; x++){
    if( map[x][y] == 2 ){ // if map tile is a spike (2)
     spikes[cnt][0] = 1;
     spikes[cnt][1] = x * cellwidth;
     spikes[cnt][2] = y * cellheight;
     spikes[cnt][4] = spikeheight;
     spikes[cnt][7] = r.nextInt(200);
     spikes[cnt][8] = 2;
     cnt++;
    }
   }
  }
 }

 public void paint(Graphics g) {
 }

    public void run() {
        for(;;) { // animation loop never ends
   updateplayer();
         updatespikes();
         repaint();
         try {
             Thread.sleep(10);
             }
             catch (InterruptedException e) {
             }
     }
    }

    public void updateplayer(){

  if ( isjumping == false && isfalling == false ){
   if( mapcollision( (int)px , (int)py+1 , pwidth , pheight ) == false ){
    isfalling = true;
    gravity = 0;
   }
  }
  if (ismovingright){
   if ( mapcollision( (int)(px + 1) , (int)py , pwidth , pheight ) == false ){
    px += 1;
   }
  }
  if (ismovingleft){
   if ( mapcollision( (int)(px - 1) , (int)py , pwidth , pheight ) == false ){
    px -= 1;
   }
  }

  if ( isfalling == true && isjumping == false ){
   for ( int i = 0 ; i < gravity ; i++ ){
    if ( mapcollision ( (int)px , (int)(py + 1) , pwidth , pheight ) == false ){
     py += 1;
    }else{
     gravity = 0;
     isfalling = false;
    }
   }
   gravity += .1;
  }

  if ( isjumping == true && isfalling == false ){
   for ( int i = 0 ; i < gravity ; i++){
    if ( mapcollision ( (int)px , (int)(py - 1) , pwidth , pheight ) == false ){
     py -= 1;
     //System.out.print("still jumping : " + gravity);
    }else{
     gravity = 0;
     isfalling = true;
     isjumping = false;
    }
   }
   if( gravity < 1 ) {
    gravity = 0;
    isfalling = true;
    isjumping = false;
   }
   gravity -= .1;
  }



    }

  public void updatespikes(){

   if ( pspikecollision() ) {
    px = pstartx;
    py = pstarty;
   };

   for ( int i = 0 ; i < maxnumspikes ; i++ ){
    if ( spikes[i][0] == 1 ){ // if spike is active
     if(spikes[i][7] < 0 ){
      if ( spikes[i][8] == 2 ){
       spikes[i][4] -= spikes[i][6];
       if ( spikes[i][4] < 0 ){
        spikes[i][4] = 0; // lowest value
        spikes[i][8] = 1; // go up again
        spikes[i][6] = 0;
        spikes[i][7] = 100;
       }
       spikes[i][6] += .01;
      }
      if ( spikes[i][8] == 1 ){
       spikes[i][4] += spikes[i][6];
       if ( spikes[i][4] > spikeheight ){
        spikes[i][4] = spikeheight; // highest value
        spikes[i][8] = 2; // go down again
        spikes[i][7] = 100;
       }
       spikes[i][6] += .01;
      }
     }
     spikes[i][7]--;
    }
   }
  }

 public boolean pspikecollision(){
  for ( int i = 0 ; i < maxnumspikes ; i++ ){
   if ( spikes[i][0] == 1 ){

    int[] XArray =  {(int)px, (int)px+pwidth, (int)px+pwidth,  (int)px};
       int[] YArray =  {(int)py, (int)py,  (int)py+pheight, (int)py+pheight};
       int[] XArray2 = {(int)spikes[i][1]+sx1, (int)spikes[i][1]+sx2,  (int)spikes[i][1]+sx3};
       int[] YArray2 = {(int)spikes[i][2]+(spikeheight - cellheight) - (int)spikes[i][4]+sy1, (int)spikes[i][2]+sy2, (int)spikes[i][2]+sy3};

       Polygon abc = new Polygon(XArray, YArray, 4);
       Polygon abc2= new Polygon(XArray2,YArray2,3);
       //g.drawPolygon(abc);
       //g.drawPolygon(abc2);

      Area a1 = new Area(abc);
      Area a2 = new Area(abc2);

    a1.intersect(a2);
    if (!a1.isEmpty())
     //g.drawString("Two Polygons collided", 20, 20 );
     return true;



   }
  }
 return false;
 }

  public boolean mapcollision( int x , int y , int width , int height ){
   int mapx = x / cellwidth;
   int mapy = y / cellheight;
   for ( int y1 = mapy - 1 ; y1 < mapy + 2 ; y1++ ){
    for ( int x1 = mapx - 1 ; x1 < mapx + 2 ; x1++ ){
     if ( x1 >= 0 && x1 < mapwidth && y1 >= 0 && y1 < mapheight ){
      if ( map[x1][y1] == 1 ){
       Rectangle rec1 = new Rectangle( x , y , width , height );
      Rectangle rec2 = new Rectangle( x1 * cellwidth,
              y1 * cellheight,
              cellwidth,
              cellheight);
      if( rec1.intersects( rec2 )) return true;
      }
     }
    }
   }
  return false;
  }

   public boolean mouseMove(Event e, int x, int y){
  return true;
 }

    public void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
        bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Platformer Springies Example.",10,10);

        // Draw map
        for( int y = 0 ; y < mapheight ; y++ ){
         for ( int x = 0 ; x < mapwidth ; x++){
          if( map[x][y] == 1 ){
           bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
          }
         }
        }
  // Draw Spikes
  for ( int i = 0 ; i < maxnumspikes ; i++ ){
   if ( spikes[i][0] == 1 ){
    bufferGraphics.drawImage( spikeimage,
           (int)spikes[i][1],
           (int)spikes[i][2] + (spikeheight - cellheight) - (int)spikes[i][4],
           spikewidth,
           (int)spikes[i][4],
           this);
   }
  }

        // Draw player
        bufferGraphics.fillRect( (int)px , (int)py , pwidth , pheight );

       g.drawImage(offscreen,0,0,this);
    }

  public boolean keyDown (Event e, int key){
    if( key == Event.LEFT )
        {
         ismovingleft = true;
        }
        if(key==Event.RIGHT)
        {
          ismovingright = true;
        }

      if( key == 32 ) // space bar for jump
      {
        if( isfalling == false && isjumping == false )
        {
            isjumping = true;
            gravity = jumpforce;
        }
      }

        //System.out.println (" Integer Value: " + key);

   return true;
  }

 public boolean keyUp (Event e, int key){
    if( key == Event.LEFT )
        {
          ismovingleft = false;
        }
        if( key == Event.RIGHT )
        {
          ismovingright = false;
        }

  return true;
 }

}

donderdag 19 mei 2011

Drawing Tiles ontop of Tiles with Transparancy mask Example


Here a map with 3 different tiles. The trees and mountains are drawn ontop of the grass tiles. I switched to Bufferedimage in order to get the transparent feature working. This was not that difficult to do.
 


import java.applet.*;
import java.awt.*;
import java.util.Random;
import java.awt.image.BufferedImage;


public class DataSpritesInJava02 extends Applet {
 Random r = new Random();
 Graphics bufferGraphics;
    Image offscreen;
    Image image2;
    Image image3;
    Image image4;

    // Here I have stored a sprite that will be drawn onto the screen.
 // Note : you have to switch the x and y in the create sprite part to get
 // the right sprite view since the layout of array data has switched x and y view.
 private short tree1[][]={
       {0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0},
       {0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0},
       {0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0},
       {0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
       {0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
       {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
       {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
       {0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
       {0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0},
       {0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0},
       {0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0},
       {0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0},
       {0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0}
       };
 private short mountain1[][]={
       {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
       {0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,0},
       {0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,0},
       {0,0,0,0,0,0,3,3,3,3,0,0,0,0,0,0},
       {0,0,0,0,0,0,3,3,3,3,0,0,0,0,0,0},
       {0,0,0,0,0,3,3,3,3,3,3,0,0,0,0,0},
       {0,0,0,0,0,3,3,3,3,3,3,0,0,0,0,0},
       {0,0,0,0,3,3,3,3,3,3,3,3,0,0,0,0},
       {0,0,0,0,3,3,3,3,3,3,3,3,0,0,0,0},
       {0,0,0,3,3,3,3,3,3,3,3,3,3,0,0,0},
       {0,0,0,3,3,3,3,3,3,3,3,3,3,0,0,0},
       {0,0,3,3,3,3,3,3,3,3,3,3,3,3,0,0},
       {0,0,3,3,3,3,3,3,3,3,3,3,3,3,0,0},
       {0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,0},
       {0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,0},
       {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3},
       };

 private short ground1[][]={
       {4,4,4,4,5,5,4,4,4,4,4,4,5,4,4,4},
       {4,5,4,4,4,4,4,4,4,4,4,4,5,5,4,4},
       {4,4,5,4,4,4,4,4,4,4,4,4,5,5,4,4},
       {4,4,5,5,5,5,4,4,4,4,4,4,4,5,5,4},
       {4,4,5,5,5,5,4,4,4,5,4,4,4,4,5,4},
       {5,4,4,4,5,5,4,4,4,5,4,4,4,4,4,5},
       {5,4,4,4,4,4,4,4,4,5,4,4,4,4,4,4},
       {4,4,4,4,5,4,4,4,4,5,5,4,4,4,4,4},
       {4,4,4,4,4,5,4,4,4,5,5,5,4,4,4,4},
       {4,4,4,4,4,4,4,4,4,5,5,5,5,4,4,4},
       {4,4,5,4,4,4,4,4,4,4,5,5,5,4,4,4},
       {4,4,5,5,4,4,4,4,4,4,5,5,5,5,4,4},
       {4,5,5,5,4,4,4,4,5,4,4,5,5,5,4,4},
       {4,5,5,5,5,5,4,4,5,5,4,4,5,5,4,4},
       {4,4,4,4,5,5,5,4,4,4,4,4,4,5,4,4},
       {4,4,4,4,4,5,5,5,4,4,4,4,5,4,4,4},
       };
     public void init(){
        setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
        bufferGraphics = offscreen.getGraphics();

   image2 = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
    Graphics test1 = image2.getGraphics();
      for( int y = 0 ; y < 16 ; y++ ){
       for ( int x = 0 ; x < 16 ; x++ ){
    test1.setColor(getcolor(tree1[x][y]));
        test1.fillRect(y,x,1,1);
       }
      }
   image3 = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
    Graphics test2 = image3.getGraphics();
      for( int y = 0 ; y < 16 ; y++ ){
       for ( int x = 0 ; x < 16 ; x++ ){
    test2.setColor(getcolor(mountain1[x][y]));
        test2.fillRect(y,x,1,1);
       }
      }

   image4 = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
    Graphics test3 = image4.getGraphics();
      for( int y = 0 ; y < 16 ; y++ ){
       for ( int x = 0 ; x < 16 ; x++ ){
    test3.setColor(getcolor(ground1[x][y]));
        test3.fillRect(y,x,1,1);
       }
      }


     }


 public Color getcolor(int c){
  if (c ==  0 ) return new Color(0,0,0,0);
  if (c ==  1 ) return new Color(0,240,0,255);
  if (c ==  2 ) return new Color(200,100,0,255);
  if (c ==  3 ) return new Color(200,200,200,255);
  if (c ==  4 ) return new Color(30,200,10,255);
  if (c ==  5 ) return new Color(50,220,20,255);
  return new Color(0,0,0,0);
 }

 public void paint(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().height);
     bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Data [][] Sprites - Tiny trees.",10,10);
        for( int y = 0 ; y < 10 ; y++){
         for( int x = 0 ; x < 16 ; x++){
          bufferGraphics.drawImage(image4,32+x*16,32+y*16,this);
         }
        }

    int r1 = 0;
        for( int y = 0 ; y < 10 ; y++){
         for( int x = 0 ; x < 16 ; x++){
      r1 = r.nextInt(4);
      if (r1 == 1){
       bufferGraphics.drawImage(image2,32+x*16,32+y*16,this);
      }
      if (r1 == 0){
       bufferGraphics.drawImage(image3,32+x*16,32+y*16,this);
      }
         }
        }
        g.drawImage(offscreen,0,0,this);
     }
     public void update(Graphics g){
          paint(g);
     }
 }


Data Sprites in Java (array holding sprite data)


Above you can maybe see tiny little trees on the applet window. One tree is created in a Java Array and then drawn into a image which is then drawn a number of times on the applet window.
In Java it looks like the x and y values are swapped if you create a field of data.

 


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

public class DataSpritesInJava01 extends Applet {
    Graphics bufferGraphics;
    Image offscreen;
    Image image2;
    // Here I have stored a sprite that will be drawn onto the screen.
 // Note : you have to switch the x and y in the create sprite part to get
 // the right sprite view since the layout of array data has switched x and y view.
 private short tree1[][]={
       {0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0},
       {0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0},
       {0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0},
       {0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
       {0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
       {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
       {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
       {0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0},
       {0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0},
       {0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0},
       {0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0},
       {0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0},
       {0,0,0,0,0,0,0,2,2,0,0,0,0,0,0,0}
       };

     public void init(){
        setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
        bufferGraphics = offscreen.getGraphics();
   image2 = createImage(16,16);
    Graphics test = image2.getGraphics();
      for( int y = 0 ; y < 16 ; y++ ){
       for ( int x = 0 ; x < 16 ; x++ ){
    test.setColor(Color.black);
        if (tree1[x][y] == 1 ){
          test.setColor(Color.green);
        }
        if (tree1[x][y] == 2 ){
         test.setColor(new Color(200,100,0));
        }
    // Here we draw the pixel.
        test.fillRect(y,x,1,1);
       }
      }

     }
      public void paint(Graphics g){
        bufferGraphics.clearRect(0,0,getSize().width,getSize().height);
        bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Data [][] Sprites - Tiny trees.",10,10);
        g.drawImage(offscreen,0,0,this);
        for( int y = 0 ; y < 6 ; y++){
         for( int x = 0 ; x < 6 ; x++){
      g.drawImage(image2,30+x*16,30+y*16,this);
         }
        }
     }
     public void update(Graphics g){
          paint(g);
     }
 }


maandag 16 mei 2011

Bought the ebook Game ai by Example.

I bought the ebook game ai by Example on the 12th. I am suprised that it takes so long to process the order since this is a ebook. Today the book is still not delivered.

I am hoping to learn a couple of things by studying this book.

I also bought a new Netbook a couple of weeks ago. It is a Atom Dual Core N550 model. It has Windows 7 starters on it and you can not change the background image unless you install a program to do that.
The Netbook is not as fast as I hoped it would be. It seems that compression programs do not fully use the available processor but only 2 threads of the 4 threads that are available. The bigger Harddisk space is a plus. I downloaded 120.000 music modules and still have a lot of space left. Trouble is that players can not handle that much files. The Netbook Gpu is 200Mhz which is the somewhat the same as my older Netbook.

I spend the weekend watching tv and programming three platformer examples for the blog. I had trouble making them. I still have to get used to the Java Language. Jcreator5 is buggy to which caused me to loose code while editing. I think there must be something wrong with the folding feature (weird deletions) The compiling time is also pretty slow. I hoped it would be faster since I am now using a dual core processor but no. Jcreator5 also has bugs with forgetting screen mode and key shortcut changes. They get erased. I have tried different editors but the bigger ones are slower and the smaller ones are difficult to use. I was used to Jcreator so I will stick to that.

Platformer Moveable Blocks/Tiles Example



Use Cursor Left and Right to move and Space to Jump. Move into the blocks to move them and hold x to drag them.

 
//
// I have placed the moving blocks in a regular array instead of placing them in a ArrayList.
// This because I had strange bugs with ArrayLists that I could solve. Better save then sorry.
//
//

import java.awt.*;
import java.applet.*;

public class PlatformerMovingBlocks01 extends Applet implements Runnable {
 // Graphics for double buffering.
 Graphics    bufferGraphics;
    Image     offscreen;
 private short map[][]={
      {1,1,1,1,1,1,1,1,1,1,1,1,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,2,2,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,2,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,2,2,2,1},
      {1,0,0,0,0,0,0,0,0,0,0,2,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,2,1},
      {1,1,1,1,1,1,1,1,1,1,1,1,1}
      };
 int     mapwidth =    20;
 int      mapheight =   13;
 int      cellwidth =   16;
 int      cellheight =   16;
 double     px =     200;
 double    py =    100;
 int     pwidth =    cellwidth/2;
 int     pheight =    cellheight;
 boolean    isjumping =   false;
 boolean    isfalling =   false;
 double    gravity =    0;
 boolean    ismovingright =  false;
 boolean    ismovingleft =   false;
 double    jumpforce =   3;
 short    maxnummblocks =  32; // Maximum number of moving blocks
 int     mblockwidth =  16;
 int     mblockheight =   16;
 short    currentmblock =  0;
 boolean    dragblock =   false;
 short    bbdragged =   0;
 double[][]   mblocks =   new double[maxnummblocks][8]; // 0 - active , 1 - x , 2 - y
                   // 3 - falling (1) , 4 - gravx
                   // 5 - gravy
 public void init() {
     setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();
  initmap();
  new Thread(this).start();

 }

 public void initmap(){
  int cnt = 0;
  for( int y = 0 ; y < mapheight ; y++ ){
   for ( int x = 0 ; x < mapwidth ; x++ ){
    if( map[x][y] == 2 ){
     mblocks[cnt][0] = 1;
     mblocks[cnt][1] = x * cellwidth;
     mblocks[cnt][2] = y * cellheight;
     cnt++;
    }
   }
  }
 }

 public void paint(Graphics g) {
 }

    public void run() {
        for(;;) { // animation loop never ends
   updateplayer();
         updatemblocks();
         repaint();
         try {
             Thread.sleep(10);
             }
             catch (InterruptedException e) {
             }
     }
    }

    public void updateplayer(){

  if ( isjumping == false && isfalling == false ){
   if( mapcollision( (int)px , (int)py+1 , pwidth , pheight ) == false &&
    mblockcollision ( (int)px , (int)py+1 , pwidth , pheight ) == false ){
    isfalling = true;
    gravity = 0;
   }
  }
  if (ismovingright){
   if ( mapcollision( (int)(px + 1) , (int)py , pwidth , pheight ) == false ){
    if( mblockcollision( (int)px + 1 , (int)py , pwidth , pheight ) == false ){
     px += 1;
     if (dragblock == true ){
      if(mblockcollision ( (int)px-2 , (int)py , pwidth, pheight) == true){
       mblocks[currentmblock][1] += 1;
      }
     }

    }
   }
   if ( mblockcollision( (int)px + 1 , (int)py , pwidth , pheight )){
    // touching a moveable block
    // check if it is not touching another block or map tile.
    if ( mblockblockwallc( 1 , 0 ) == false ){
     px += 1;
     mblocks[currentmblock][1] += 1;
    }
   }
  }
  if (ismovingleft){
   if ( mapcollision( (int)(px - 1) , (int)py , pwidth , pheight ) == false ){
    if( mblockcollision( (int)px - 1 , (int)py , pwidth , pheight ) == false ){
     px -= 1;
     if (dragblock == true ){
      if(mblockcollision ( (int)px+2 , (int)py , pwidth, pheight) == true){
       mblocks[currentmblock][1] -= 1;
      }
     }
    }
   }
   if ( mblockcollision( (int)px - 1 , (int)py , pwidth , pheight )){
    // touching a moveable block
    // check if it is not touching another block or map tile.
    if ( mblockblockwallc( -1 , 0 ) == false ){
     px -= 1;
     mblocks[currentmblock][1] -= 1;
    }
   }
  }

  if ( isfalling == true && isjumping == false ){
   for ( int i = 0 ; i < gravity ; i++ ){
    if ( mapcollision ( (int)px , (int)py + 1 , pwidth , pheight ) == false &&
      mblockcollision ((int)px , (int)py + 1 , pwidth , pheight ) == false){
     py += 1;
    }else{
     System.out.print("Stopped falling..");
     gravity = 0;
     isfalling = false;
    }
   }
   gravity += .1;
  }

  if ( isjumping == true && isfalling == false ){
   for ( int i = 0 ; i < gravity ; i++){
    if ( mapcollision ( (int)px , (int)(py - 1) , pwidth , pheight ) == false &&
     mblockcollision ( (int)px , (int)py - 1 , pwidth , pheight ) == false ) {
     py -= 1;
     //System.out.print("still jumping : " + gravity);
    }else{
     gravity = 0;
     isfalling = true;
     isjumping = false;
    }
   }
   if( gravity < 1 ) {
    gravity = 0;
    isfalling = true;
    isjumping = false;
   }
   gravity -= .1;
  }



    }

 public void updatemblocks(){
  // if block is floating then set to falling
  for ( int i = 0 ; i < maxnummblocks ; i++ ){
   if ( mblocks[i][0] == 1 ){
    if ( mblockblocktilec(i,0,1) == false ){
     mblocks[i][3] = 1;
    }
   }
  }
  // do falling blocks
  for ( int i = 0 ; i < maxnummblocks ; i++ ){
   if ( mblocks[i][0] == 1 ){
    for (int j = 0 ; j < mblocks[i][5] ; j++ ){
     if (mblockblocktilec(i,0,1) == false ){
      mblocks[i][2] += 1;
     }else{
      mblocks[i][3] = 0;
      mblocks[i][5] = 0;
     }
    }
    mblocks[i][5] += .1;
   }
  }

 }

 public boolean mblockblocktilec( int block , int x, int y ){
  for ( int i = 0 ; i < maxnummblocks ; i++ ){
   if ( block != i && mblocks[i][0] == 1 ){
    Rectangle rec1 = new Rectangle( (int)mblocks[block][1] + x ,
            (int)mblocks[block][2] + y ,
            mblockwidth ,
            mblockheight );
    Rectangle rec2 = new Rectangle( (int)mblocks[i][1] ,
            (int)mblocks[i][2] ,
            mblockwidth ,
            mblockheight );
    if ( rec1.intersects(rec2) ) {
     return true;
    }
   }
  }
  // check if block touches tile
  int x1 = (int)(mblocks[block][1] / cellwidth);
  int y1 = (int)(mblocks[block][2] / cellheight);
  for ( int y2 = y1 - 1 ; y2 < y1 + 2 ; y2++ ){
   for ( int x2 = x1 - 1 ; x2 < x1 + 2 ; x2++ ){
    if( x2 >= 0 && x2 < mapwidth && y2 >= 0 && y2 < mapheight ){
     if(map[x2][y2] == 1){
       Rectangle rec1 = new Rectangle( (int)mblocks[block][1] + x ,
               (int)mblocks[block][2] + y ,
               mblockwidth ,
               mblockheight);
       Rectangle rec2 = new Rectangle( x2 * cellwidth ,
               y2 * cellheight ,
               cellwidth ,
               cellheight);
       if ( rec1.intersects(rec2) ) {
        return true;
       }
     }
    }
   }
  }
  // check if on player
  Rectangle rec1 = new Rectangle( (int)px ,
          (int)py ,
          pwidth ,
          pheight);
  Rectangle rec2 = new Rectangle( (int)mblocks[block][1]+x ,
          (int)mblocks[block][2]+y ,
          mblockwidth ,
          mblockheight);
  if ( rec1.intersects(rec2) ) {
   return true;
  }


  return false;
 }

  public boolean mapcollision( int x , int y , int width , int height ){
   int mapx = x / cellwidth;
   int mapy = y / cellheight;
   for ( int y1 = mapy - 1 ; y1 < mapy + 2 ; y1++ ){
    for ( int x1 = mapx - 1 ; x1 < mapx + 2 ; x1++ ){
     if ( x1 >= 0 && x1 < mapwidth && y1 >= 0 && y1 < mapheight ){
      if ( map[x1][y1] == 1 ){
       Rectangle rec1 = new Rectangle( x , y , width , height );
      Rectangle rec2 = new Rectangle( x1 * cellwidth,
              y1 * cellheight,
              cellwidth,
              cellheight);
      if( rec1.intersects( rec2 )) return true;
      }
     }
    }
   }
  return false;
  }

 public boolean mblockblockwallc( int dirx, int diry ){
  // check for collision with other blocks.
  for ( int i = 0 ; i < maxnummblocks ; i++ ){
   if ( i != currentmblock ){
    if ( mblocks[i][0] == 1 ){
     Rectangle rec1 = new Rectangle( (int)mblocks[currentmblock][1] + dirx ,
              (int)mblocks[currentmblock][2] + diry ,
              mblockwidth ,
              mblockheight );
     Rectangle rec2 = new Rectangle( (int)mblocks[i][1] ,
              (int)mblocks[i][2] ,
              mblockwidth ,
              mblockheight );
     if (rec1.intersects(rec2)) {
      return true;
     }
    }
   }
  }
  // check for collision with map tile (1)
  int x1 = (int)mblocks[currentmblock][1] / cellwidth;
  int y1 = (int)mblocks[currentmblock][2] / cellheight;
  for ( int y = y1 - 1 ; y < y1 + 2 ; y++ ){
   for ( int x = x1 - 1 ; x < x1 + 2 ; x++ ){
    if( x >= 0 && x < mapwidth && y >= 0 && y < mapheight ){
     if( map[x][y] == 1 ){
      Rectangle rec1 = new Rectangle( x * cellwidth ,
              y * cellheight ,
              cellwidth ,
              cellheight);
      Rectangle rec2 = new Rectangle( (int)mblocks[currentmblock][1] ,
              (int)mblocks[currentmblock][2] ,
              mblockwidth ,
              mblockheight);
      if (rec1.intersects(rec2)){
       return true;
      }
     }
    }
   }
  }
  return false;
 }

  public boolean mblockcollision( int x , int y , int width , int height ){
   for ( int i = 0 ; i < maxnummblocks ; i++ ){
    if( mblocks[i][0] == 1){
     Rectangle rec1 = new Rectangle( x , y , width , height );
     Rectangle rec2 = new Rectangle(  (int)mblocks[i][1],
              (int)mblocks[i][2],
              mblockwidth,
              mblockheight);
     if(rec1.intersects(rec2)){
      currentmblock = (short)i;
      return true;
     }
    }
   }
   return false;
  }

   public boolean mouseMove(Event e, int x, int y){
  return true;
 }

    public void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
        bufferGraphics.setColor(Color.red);

        // Draw map
        for( int y = 0 ; y < mapheight ; y++ ){
         for ( int x = 0 ; x < mapwidth ; x++){
          if( map[x][y] == 1 ){
           bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
          }
         }
        }

  // Draw the moveable blocks
  for ( int i = 0 ; i < maxnummblocks ; i++ ){
   if ( mblocks[i][0] == 1 ){
    bufferGraphics.fillRect(  (int)mblocks[i][1] ,
           (int)mblocks[i][2] ,
           cellwidth ,
           cellheight );
   }
  }

        // Draw player
        bufferGraphics.fillRect( (int)px , (int)py , pwidth , pheight );
  // Draw some info
        bufferGraphics.setColor(Color.white);
        bufferGraphics.drawString("Platformer Moveable blocks Example.",10,10);
        bufferGraphics.drawString("cursor l/r, space , x to drag blocks.",10,20);
       g.drawImage(offscreen,0,0,this);
    }

  public boolean keyDown (Event e, int key){
    if( key == Event.LEFT )
        {
         ismovingleft = true;
        }
        if(key==Event.RIGHT)
        {
          ismovingright = true;
        }

      if( key == 32 ) // space bar for jump
      {
        if( isfalling == false && isjumping == false )
        {
            isjumping = true;
            gravity = jumpforce;
        }
      }

  if ( key == 120 ){ // x button block dragging mode
   dragblock = true;
  }
        //System.out.println (" Integer Value: " + key);

   return true;
  }

 public boolean keyUp (Event e, int key){
    if( key == Event.LEFT ){
          ismovingleft = false;
        }
        if( key == Event.RIGHT ){
          ismovingright = false;
        }
  if ( key == 120 ){ // block being dragged mode (x key)
   dragblock = false;
  }

  return true;
 }

}

Platformer Moving Platforms Example



Use Cursors Left and Right to move and Space to jump. Jump on the moving Platform.

 
//
// This is maybe the third time that I made a platformer thing with a moving platform.
// I got the moving platform part working quickly. I had a little trouble with
// the gravity, jumping and falling.
//
// The green rectangles I made for viewing the collision cells.
//
// Feel free to modify and use the example for your own use.
//

import java.awt.*;
import java.applet.*;

public class MovingPLatform001 extends Applet implements Runnable {
 // Graphics for double buffering.
 Graphics    bufferGraphics;
 Image     offscreen;
 int     mapwidth =    20;
 int     mapheight =   13;
 short    cellwidth =   16;
 short    cellheight =   16;
 short    maxnummplats =   16;
 private double[][] mplat =    new double[maxnummplats][8]; // 0 - active 0/1 ; 1 - x ; 2 - y ; 3 - incx ;
                   // 4 - incy
 int     mplatwidth =   cellwidth*2;
 int     mplatheight =  cellheight/2;
 int     mplatmspeed =   1;
 int     currentmplat =   0;
 boolean    isonmplat =   false;
 private short map[][]={
      {1,1,1,1,1,1,1,1,1,1,1,1,1},
      {1,0,0,0,0,0,0,0,0,0,0,1,1},
      {1,0,0,0,0,0,0,0,0,0,0,1,1},
      {1,0,0,0,0,0,0,0,0,0,1,1,1},
      {1,0,0,0,1,0,0,0,0,1,1,1,1},
      {1,0,0,0,1,0,0,0,0,1,1,1,1},
      {1,0,0,0,1,0,0,0,0,1,1,1,1},
      {1,0,0,0,0,0,0,0,0,1,1,1,1},
      {1,0,0,0,0,0,0,0,0,1,1,1,1},
      {1,0,0,0,0,0,0,0,0,1,1,1,1},
      {1,0,0,0,0,0,0,0,0,1,1,1,1},
      {1,0,0,0,0,0,0,0,0,1,1,1,1},
      {1,0,0,0,0,0,0,0,0,1,1,1,1},
      {1,0,0,0,2,0,0,0,0,0,1,1,1},
      {1,0,0,0,0,0,0,0,0,0,0,1,1},
      {1,0,0,0,0,0,0,1,0,0,0,0,1},
      {1,0,0,0,0,0,0,1,0,0,0,0,1},
      {1,0,0,0,0,0,0,1,0,0,0,0,1},
      {1,0,0,0,0,0,0,1,0,0,0,0,1},
      {1,1,1,1,1,1,1,1,1,1,1,1,1}
      };
 double    px =     100;
 double    py =    100;
 int     pwidth =   cellwidth/2;
 int     pheight =    cellheight;
 int     pmspeed =    1;
 int[][]    coldebug =    new int[9][12];
 boolean    isjumping =   false;
 boolean    isfalling =   false;
 boolean    ismovingright =  false;
 boolean    ismovingleft =   false;
 double    gravity =   0;
 double    jumpforce =   3;

 public void init() {
     setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();
  initmplat();
  new Thread(this).start();

 }
 public void paint(Graphics g) {
 }
    public void run() {
        for(;;) { // animation loop never ends
   movemplat();
   updateplayer();
         repaint();
         try {
             Thread.sleep(10);
             }
             catch (InterruptedException e) {
             }
     }
    }
 public void initmplat(){
  int curmplat = 0;
  for ( int y = 0 ; y < mapheight ; y++ ){
   for ( int x = 0 ; x < mapwidth ; x++ ){
    if ( map[x][y] == 2 ){      // if 2=mplat
     mplat[curmplat][0] = 1;    // activate platform
     mplat[curmplat][1] = x * cellwidth; // x location
     mplat[curmplat][2] = y * cellheight;// y location
     mplat[curmplat][3] = mplatmspeed; // movement speed
     curmplat++;
    }
   }
  }
 }
 public void movemplat(){
  // here the moving platforms are moved
  for ( int i = 0 ; i < maxnummplats ; i++ ) {
   if ( mplat[i][0] == 1 ){ // if moving platform exists
    if( mplat[i][3] > 0 ){ // if movement speed is positive
     if ( map[
        (int)( ( mplat[i][1] + mplat[i][3] + mplatwidth ) /cellwidth)][
        (int)(mplat[i][2]/cellheight)]
        != 1 ){
      mplat[i][1] += mplat[i][3];
     }else{
      mplat[i][3] = -mplat[i][3];
     }
    }else{ // if movement speed is negative
     if ( map[
        (int)( ( mplat[i][1] + mplat[i][3]) /cellwidth)][
        (int)(mplat[i][2]/cellheight)]
        != 1 ){
      mplat[i][1] += mplat[i][3];
     }else{
      mplat[i][3] = -mplat[i][3];
     }
    }
   }
  }
 }
 public boolean mplatcollision( int x , int y , int width , int height ){
  for ( int i = 0 ; i < maxnummplats ; i++ ){
   if ( mplat[i][0] == 1 ){
    Rectangle rec1 = new Rectangle(x,y,width,height);
    Rectangle rec2 = new Rectangle( (int)mplat[i][1] ,
            (int)mplat[i][2] ,
            mplatwidth  ,
            mplatheight );
    if(rec1.intersects(rec2)){
     currentmplat = i;
     return true;
    }
   }
  }
  return false;
 }
 public void updateplayer(){
  if (ismovingright){
   if ( mapcollision( (int)(px + pmspeed) , (int)py , pwidth , pheight ) == false ){
    px += pmspeed;
   }
  }
  if (ismovingleft){
   if ( mapcollision( (int)(px - pmspeed) , (int)py , pwidth , pheight ) == false ){
    px -= pmspeed;
   }
  }
  if (isonmplat == true){
   px += mplat[currentmplat][3];
   if (mplatcollision( (int)px , (int)(py+1) , pwidth , pheight ) == false){
    isonmplat = false;

   }
  }
  if( isfalling == false && isjumping == false && isonmplat == false ){
   if( mapcollision( (int)px , (int)( py + 1 ) , pwidth , pheight ) == false )
    isfalling = true;
  }
  if ( isfalling = true && isjumping == false && isonmplat == false){

   for ( int i = 0 ; i < gravity ; i++ ){
    if ( mapcollision ( (int)px , (int)(py + 1) , pwidth , pheight ) == false ){
     py += 1;
    }else{
     gravity = 0;
     isfalling = false;
    }
    if ( mplatcollision ((int)px , (int)(py + 1) , pwidth , pheight ) == true ){
     isfalling = false;
     gravity = 0;
     isonmplat = true;
    }
   }
   gravity += .1;
  }

  if ( isjumping == true && isfalling == false ){
   for ( int i = 0 ; i < gravity ; i++){
    if ( mapcollision ( (int)px , (int)(py - 1) , pwidth , pheight ) == false ){
     py -= 1;
     //System.out.print("still jumping : " + gravity);
    }else{
     gravity = 0;
     isfalling = true;
     isjumping = false;
    }
   }
   if( gravity < 1 ) {
    gravity = 0;
    isfalling = true;
    isjumping = false;
   }
   gravity -= .1;
  }

 }
 public boolean mapcollision( int x , int y , int width, int height ){
  // get the map coordinates from the player coordinates
  int mapx = x / cellwidth;
  int mapy = y / cellheight;
  int cnt = 0; // counter for coldebug
  for ( int y1 = mapy - 1 ; y1 < mapy + 2 ; y1++ ){
   for ( int x1 = mapx - 1 ; x1 < mapx + 2 ; x1++){
    //coldebug[cnt][0] = x1 * cellwidth;
    //coldebug[cnt][1] = y1 * cellheight;
    //coldebug[cnt][2] = cellwidth;
    //coldebug[cnt][3] = cellheight;
    //cnt++;
    if( x1 >= 0 && x1 < mapwidth && y1 >= 0 && y1 < mapheight ){
     coldebug[cnt][0] = x1 * cellwidth;
     coldebug[cnt][1] = y1 * cellheight;
     coldebug[cnt][2] = cellwidth;
     coldebug[cnt][3] = cellheight;
     cnt++;
     if ( map[x1][y1] == 1 ){
      Rectangle rec1 = new Rectangle(  x1 * cellwidth ,
                y1 * cellheight ,
                cellwidth ,
                cellheight );
      Rectangle rec2 = new Rectangle(  x,
               y,
               width,
               height);
      if(rec1.intersects(rec2)) return true;
     }
    }
   }
  }
  return false;
 }
 public boolean mouseMove(Event e, int x, int y){
  //System.out.println( "" + mapcollision( x , y , pwidth , pheight ));
  return true;
 }
    public void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
        bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Moving Platformer",10,10);
  // Draw the map
  for ( int y = 0 ; y < mapheight ; y++ ){
   for ( int x = 0 ; x < mapwidth ; x++ ){
    if ( map[x][y] == 1 ){
     bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
    }
   }
  }
  // Draw the moving platforms
  for( int i = 0 ; i < maxnummplats ; i++ ){
   if ( mplat[i][0] == 1 ){ // if moving platformer is active/used/exists
    bufferGraphics.fillRect(  (int)mplat[i][1] ,
           (int)mplat[i][2] ,
           (int)mplatwidth ,
           (int)mplatheight );
   }
  }
  // Draw the player
  bufferGraphics.fillRect((int)px,(int)py,pwidth,pheight);

  // draw the collision debug info
  bufferGraphics.setColor(Color.green);
  for ( int i = 0 ; i < 9 ; i++ ){
   bufferGraphics.drawRect(  coldebug[i][0],
          coldebug[i][1],
          coldebug[i][2],
          coldebug[i][3]);
  }
  bufferGraphics.drawString("isjumping :"+isjumping+" , isfalling :"+isfalling,10,250);

  // Draw the Graphics buffer
       g.drawImage(offscreen,0,0,this);
    }
  public boolean keyDown (Event e, int key){
   if(key==Event.LEFT)
        {
         ismovingleft = true;
        }
        if(key==Event.RIGHT)
        {
         ismovingright = true;
        }

     if(key==32)
   {
   if(isfalling==false && isjumping==false)  {
       isjumping = true;
     gravity = jumpforce;
     System.out.println("Player is in jump mode.");
      }
     }


   return true;
  }
 public boolean keyUp (Event e, int key){

  if(key==Event.LEFT)
        {
         ismovingleft = false;
        }
        if(key==Event.RIGHT)
        {
         ismovingright = false;
        }
  //System.out.println(""+key);
  return true;
 }

}

Platformer Springies Example



Platformer with Jump things / Springies. Use cursors left and right and space to move in the applet.

 

import java.awt.*;
import java.applet.*;

public class PlatformerSpringies001 extends Applet implements Runnable {
 // Graphics for double buffering.
 Graphics    bufferGraphics;
    Image     offscreen;
 private short map[][]={
      {1,1,1,1,1,1,1,1,1,1,1,1,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,2,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,2,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,1,1,1,1,1,1,1,1,1,1,1,1}
      };
 int     mapwidth =    20;
 int      mapheight =   13;
 int      cellwidth =   16;
 int      cellheight =   16;
 int      maxnumspringies = 32;
 int      swidth =    cellwidth;
 int      sheight =   cellheight/2;
 int      springyjumpforce =  5;
 int     currentspringy =    0;
 double[][]   springy =   new double[maxnumspringies][8];  // 0 - active ; 1 = x ; 2 = y
                    // 3 - springy width
                    // 4 - springy height
                    // 5 - springy jumpforce
 double     px =     200;
 double    py =    100;
 int     pwidth =    cellwidth/2;
 int     pheight =    cellheight;
 boolean    isjumping =   false;
 boolean    isfalling =   false;
 double    gravity =    0;
 boolean    ismovingright =  false;
 boolean    ismovingleft =   false;
 double    jumpforce =   3;

 public void init() {
     setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();
  initmap();
  new Thread(this).start();

 }

 public void initmap(){
  int cnt = 0;
  for ( int y = 0 ; y < mapheight ; y++ ){
   for ( int x = 0 ; x < mapwidth ; x++ ){
    if ( map[x][y] == 2 ){
     springy[cnt][0] = 1;
     springy[cnt][1] = x*cellwidth;
     springy[cnt][2] = y*cellheight+cellheight/2;
     springy[cnt][3] = cellwidth;
     springy[cnt][4] = cellheight/2;
     springy[cnt][5] = springyjumpforce;
     cnt++;
    }
   }
  }
 }

 public void paint(Graphics g) {
 }

    public void run() {
        for(;;) { // animation loop never ends
   updateplayer();
         repaint();
         try {
             Thread.sleep(10);
             }
             catch (InterruptedException e) {
             }
     }
    }

    public void updateplayer(){

  if ( isjumping == false && isfalling == false ){
   if( mapcollision( (int)px , (int)py+1 , pwidth , pheight ) == false ){
    isfalling = true;
    gravity = 0;
   }
  }
  if (ismovingright){
   if ( mapcollision( (int)(px + 1) , (int)py , pwidth , pheight ) == false ){
    px += 1;
   }
  }
  if (ismovingleft){
   if ( mapcollision( (int)(px - 1) , (int)py , pwidth , pheight ) == false ){
    px -= 1;
   }
  }

  if ( isfalling == true && isjumping == false ){
   for ( int i = 0 ; i < gravity ; i++ ){
    if ( mapcollision ( (int)px , (int)(py + 1) , pwidth , pheight ) == false ){
     py += 1;
    }else{
     gravity = 0;
     isfalling = false;
    }
    if( springycollision( (int)px , (int)(py + 1) , pwidth , pheight ) == true ){
     gravity = springy[currentspringy][5];
     isfalling = false;
     isjumping = true;
    }
   }
   gravity += .1;
  }

  if ( isjumping == true && isfalling == false ){
   for ( int i = 0 ; i < gravity ; i++){
    if ( mapcollision ( (int)px , (int)(py - 1) , pwidth , pheight ) == false ){
     py -= 1;
     //System.out.print("still jumping : " + gravity);
    }else{
     gravity = 0;
     isfalling = true;
     isjumping = false;
    }
   }
   if( gravity < 1 ) {
    gravity = 0;
    isfalling = true;
    isjumping = false;
   }
   gravity -= .1;
  }



    }

 public boolean springycollision( int x , int y , int width , int height){
  for ( int i = 0 ; i < maxnumspringies ; i++ ){
   if( springy[i][0] == 1 ){
     Rectangle rec1 = new Rectangle( x , y , width , height );
    Rectangle rec2 = new Rectangle( (int)springy[i][1],
            (int)springy[i][2],
            (int)springy[i][3],
            (int)springy[i][4]);
    if( rec1.intersects( rec2 )){
     currentspringy = i;
     return true;
    }
   }
  }
  return false;
 }

  public boolean mapcollision( int x , int y , int width , int height ){
   int mapx = x / cellwidth;
   int mapy = y / cellheight;
   for ( int y1 = mapy - 1 ; y1 < mapy + 2 ; y1++ ){
    for ( int x1 = mapx - 1 ; x1 < mapx + 2 ; x1++ ){
     if ( x1 >= 0 && x1 < mapwidth && y1 >= 0 && y1 < mapheight ){
      if ( map[x1][y1] == 1 ){
       Rectangle rec1 = new Rectangle( x , y , width , height );
      Rectangle rec2 = new Rectangle( x1 * cellwidth,
              y1 * cellheight,
              cellwidth,
              cellheight);
      if( rec1.intersects( rec2 )) return true;
      }
     }
    }
   }
  return false;
  }

   public boolean mouseMove(Event e, int x, int y){
  return true;
 }

    public void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
        bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Platformer Springies Example.",10,10);

        // Draw map
        for( int y = 0 ; y < mapheight ; y++ ){
         for ( int x = 0 ; x < mapwidth ; x++){
          if( map[x][y] == 1 ){
           bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
          }
         }
        }
        // Draw Springies
        for ( int i = 0 ; i < maxnumspringies ; i++ ){
         if ( springy[i][0] == 1 ){
          bufferGraphics.fillRect(  (int)springy[i][1],
                 (int)springy[i][2],
                 (int)springy[i][3],
                 (int)springy[i][4] );
         }
        }

        // Draw player
        bufferGraphics.fillRect( (int)px , (int)py , pwidth , pheight );

       g.drawImage(offscreen,0,0,this);
    }

  public boolean keyDown (Event e, int key){
    if( key == Event.LEFT )
        {
         ismovingleft = true;
        }
        if(key==Event.RIGHT)
        {
          ismovingright = true;
        }

      if( key == 32 ) // space bar for jump
      {
        if( isfalling == false && isjumping == false )
        {
            isjumping = true;
            gravity = jumpforce;
        }
      }

        System.out.println (" Integer Value: " + key);

   return true;
  }

 public boolean keyUp (Event e, int key){
    if( key == Event.LEFT )
        {
          ismovingleft = false;
        }
        if( key == Event.RIGHT )
        {
          ismovingright = false;
        }

  return true;
 }

}

dinsdag 10 mei 2011

Explosions Effect Example




I tried making the explosions with Arraylists first but I kept getting out of bounds errors so I switched to using regular Arrays. I got that working almost straight away. Below is the source code of how the effect in the applet screen above is done.

 


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

public class Explosion001 extends Applet implements Runnable {
 // Graphics for double buffering.
 Graphics    bufferGraphics;
    Image     offscreen;
 int     startx;
 int     starty;
 int     numparticles =   1024;
 double[]   px =    new double[numparticles];
 double[]   py =    new double[numparticles];
 double[]   incx =     new double[numparticles];
 double[]   incy =    new double[numparticles];
 double[]   addx =     new double[numparticles];
 double[]   addy =    new double[numparticles];
 boolean[]   active =   new boolean[numparticles];

 Random    r =     new Random();

 public void init() {
     setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();

  new Thread(this).start();

 }

 public void paint(Graphics g) {
 }
    public void run() {
        for(;;) { // animation loop never ends

   if(r.nextInt(100)<5){
    startx = r.nextInt(getSize().width);
    starty = r.nextInt(getSize().height);
    for(int i = 0 ; i < 64 ; i++){
     newexplosion();
    }
   }
         updateexplosions();
         repaint();
         try {
             Thread.sleep(10);
             }
             catch (InterruptedException e) {
             }
     }
    }

 public void updateexplosions(){

  for(int i = 0 ; i < numparticles ; i++){
   if(active[i] == true ){
    px[i] += incx[i];
    py[i] += incy[i];
    if(incx[i] > 0){
     incx[i] -= addx[i];
     if(incx[i]<0) active[i] = false;
    }else{
     incx[i] += addx[i];
     if(incx[i]>0) active[i] = false;
    }

    if(incy[i] > 0){
     incy[i] -= addy[i];
     if(incy[i]<0) active[i] = false;
    }else{
     incy[i] += addy[i];
     if(incy[i]>0) active[i] = false;
    }


   }
  }

 }

 public void newexplosion(){

  for (int i = 0 ; i < numparticles ; i++){
   if(active[i] == false){
    px[i] = startx;
    py[i] = starty;
    incx[i] = r.nextDouble()*4-2;
    incy[i] = r.nextDouble()*4-2;
    addx[i] = r.nextDouble()/15;
    addy[i] = r.nextDouble()/15;
    active[i] = true;
    break;
   }
  }

 }

    public void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
        bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Explosions Example",10,10);

  for(int i = 0 ; i < numparticles ; i++){
   if(active[i]){
         bufferGraphics.drawOval((int)px[i],(int)py[i],4,4);
   }
        }

       g.drawImage(offscreen,0,0,this);
    }

}


Key Scan Codes for Java. (keyUp key)

Below a Key Code List for Java. I made it by using System.out.println(""+key); in the keyUp Method.

a 97
s 115
d 100
f 102
g 103
h 104
j 106
k 107
l 108
z 122
x 120
c 99
v 118
b 98
n 110
m 109
, 44
. 46
/ 47
q 113
w 119
e 101
r 114
t 116
y 121
u 117
i 105
o 111
p 112
[ 91
] 93
\ 92
` 96
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
0 48
- 45
= 61
Space 32
Backspace 8
Escape 27
F1 1008
F2 1009
F3 1010
F4 1011
F5 1012
F6 1013
F7 1014
F8 1015
F9 1016
F10 1017
F11 1018
F12 1019
Print Screen 1020
Pause Break 1024
Insert 1025
Del 127
; 59
' 39
Enter 10
Curs Left 1006
Curs Up 1004
Curs Right 1007
Curs Down 1005
Windows Key 65535
Page Up 1002
Page Down 1003

dinsdag 3 mei 2011

Isometric map Example with mouse Collision



I spend a while looking for a way to draw a isometric map and have collision to. On the java gaming forum there was a solution that I could use. Move the mouse on the applet and see which tile is underneath the mouse.

 

import java.awt.*;
import java.applet.*;

public class IsometricCollision02 extends Applet implements Runnable {
 // Graphics for double buffering.
 Graphics bufferGraphics;
 Image offscreen;
 int tilewidth = 64;
 int tileheight =32;
 int mtilex,mtiley;

 public void init() {
     setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();

  new Thread(this).start();

 }

 public void paint(Graphics g) {

 }
    public void run() {
        for(;;) { // animation loop never ends
         repaint();
         try {
             Thread.sleep(10);
             }
             catch (InterruptedException e) {
             }
     }
    }

   public boolean mouseMove(Event e, int x, int y){
    int x0 = 130 + tilewidth/2, y0 = 0;
    mtilex = (int)Math.floor( (y - y0)/(double)tileheight - (x - x0)/(double)tilewidth );
       mtiley = (int)Math.floor( (y - y0)/(double)tileheight + (x - x0)/(double)tilewidth );
   if(mtilex<0)mtilex=mtiley=-1;
   if(mtilex>4)mtilex=mtiley=-1;
   if(mtiley<0)mtiley=mtilex=-1;
   if(mtiley>4)mtiley=mtilex=-1;
   return true;
 }
    public void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
        bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Isometric Example",10,10);

  int x,y;
     for(int i=0;i<5;i++){
         x = 130 - (i*(tilewidth/2));
         y = 0 + (i*(tileheight/2));
         for(int j=0;j<5;j++){
             drawiso(x,y,bufferGraphics);
             bufferGraphics.drawString(""+i+","+j,x+tilewidth/2,y+tileheight/2);
             x = x + tilewidth/2;
             y = y + tileheight/2;
         }
     }

     bufferGraphics.drawString("Mouse Tile Position : "+mtilex+","+mtiley,10,getSize().height-45);

       g.drawImage(offscreen,0,0,this);
    }
  public boolean keyDown (Event e, int key){
   return true;
  }
 public boolean keyUp (Event e, int key){
  return true;
 }

 public void drawiso(int x1, int y1, Graphics bufferGraphics){
 //Rect x1,y1,64,32,0
  bufferGraphics.drawLine( x1+31,y1,x1+61,y1+16);
  bufferGraphics.drawLine( x1+31,y1,x1,y1+16);
  bufferGraphics.drawLine( x1+31,y1+31,x1+63,y1+16);
  bufferGraphics.drawLine( x1+31,y1+31,x1,y1+16);
 }

}

Oval angle movement towards mouse position.



Simple example of how to move something in a straight line towards something. In this example the circle moves towards the mouse position.

 


import java.awt.*;
import java.applet.*;

public class AngleMovement01 extends Applet implements Runnable {
 // Graphics for double buffering.
 Graphics    bufferGraphics;
    Image     offscreen;

 double    playerx    = 100;
 double    playery    = 100;
 double    playermx   = 0;
 double    playermy   = 0;
 int     playerwidth   = 16;
 int     playerheight  = 16;
 double    targetx    = 0;
 double    targety    = 0;

 public void init() {
     double angle = 0;
     setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();

     targetx = getSize().width/2;
     targety = getSize().height/2;
     angle = getangle(playerx,playery,targetx,targety);
     playermx = Math.sin(Math.toRadians(angle));
     playermy = Math.cos(Math.toRadians(angle));
  new Thread(this).start();

 }

 public void paint(Graphics g) {
 }
    public void run() {
        for(;;) { // animation loop never ends
         playerx += playermx;
         playery += playermy;

   if(playerx < 0){
    playerx = getSize().width/2;
    setdirection();
   }
   if(playery < 0){
    playery = getSize().height/2;
    setdirection();
   }
   if(playerx > getSize().width){
    playerx = getSize().width/2;
    setdirection();
   }
   if(playery > getSize().height){
    playery = getSize().height/2;
    setdirection();
   }
         repaint();
         try {
             Thread.sleep(10);
             }
             catch (InterruptedException e) {
             }
     }
    }

 public void setdirection(){
  double angle;
  angle = getangle(playerx,playery,targetx,targety);
     playermx = 1*Math.sin(Math.toRadians(angle));
     playermy = 1*Math.cos(Math.toRadians(angle));
 }

   public boolean mouseMove(Event e, int x, int y){
  double angle;
  targetx = x;
  targety = y;
  setdirection();
  return true;
 }
    public void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
        bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Angle Movement Example",10,10);
        bufferGraphics.drawString("Move mouse on Applet",10,20);
  bufferGraphics.fillOval((int)playerx,(int)playery,playerwidth,playerheight);
       g.drawImage(offscreen,0,0,this);
    }
  public boolean keyDown (Event e, int key){
   return true;
  }
 public boolean keyUp (Event e, int key){
  return true;
 }
 public double getangle( double x1, double y1, double x2 , double y2 ){
     double at = Math.toDegrees(Math.atan2( x1 - x2 , y1 - y2 ));
     at = at - 180;
     if (at > 360 ){
      at = at - 360;
     }else if ( at < 0) {
      at = at + 360;
     }
     return at;
  }
}