donderdag 19 april 2012

Game Menu Example





Use the s and w keys to move the selection cursor.
 


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

public class gamemenuexample001 extends Applet implements Runnable{

 Graphics bufferGraphics;
 Image offscreen;
 FontMetrics fm;
 int nummenuitems = 4;
 String gamemenu[] = new String[ nummenuitems ];
 int menuchoice = 0;

 public void init() {
        setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
        bufferGraphics = offscreen.getGraphics();
  Graphics g;
  g = getGraphics();
  fm = g.getFontMetrics();
        gamemenu[ 0 ] = "New Game";
        gamemenu[ 1 ] = "Load Game";
        gamemenu[ 2 ] = "Options";
        gamemenu[ 3 ] = "Credits";
  new Thread(this).start();
 }
    public void run() {
        for(;;) { // animation loop never ends
         repaint();
         try {
             Thread.sleep(10);
             }
             catch (InterruptedException e) {
             }
     }
    }



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

  String s = "";
  for ( int i = 0 ; i < nummenuitems ; i++ ){
   if ( i == menuchoice ){
          s = "-> " + gamemenu[ i ];
    bufferGraphics.drawString(s,(this.getSize().width-fm.stringWidth(s)) / 2, 80 + i * 20 );
   }else{
    s = gamemenu[ i ];
    bufferGraphics.drawString(s,(this.getSize().width-fm.stringWidth(s)) / 2, 80 + i * 20 );
   }
  }

        bufferGraphics.drawString("Game Menu Screen.",10,10);
  bufferGraphics.drawString("w/s to move cursor.",10,237);

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

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

  // key w
   if ( key == 119 ){
       if ( menuchoice > 0 ){
        menuchoice--;
       }
        }
        // key s
        if ( key == 115 ){
         if ( menuchoice < nummenuitems - 1 ){
          menuchoice++;
         }
        }
  //System.out.println(""+key);
  return true;
 }


}


Topdown patrolling ai example





 


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

public class topdownpatrollingaiexample001 extends Applet implements Runnable{

 Graphics bufferGraphics;
 Image offscreen;
 boolean ismovingleft;
 boolean ismovingright;
 boolean ismovingup;
 boolean ismovingdown;
 int mapwidth = 20;
 int mapheight = 15;
 int cellwidth = 16;
 int cellheight = 16;
 private int map[][] =  new int[][]{
 {1,1,1,1,1,1,1,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,0,0,0,0,0,0,0,1},
 {1,0,1,1,1,1,0,1,0,0,0,0,1,0,1,1,1,1,0,1},
 {1,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,1},
 {1,0,1,0,1,0,0,1,1,0,0,1,1,0,0,1,0,1,0,1},
 {1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1},
 {1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
 {1,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,1},
 {1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
 {1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1},
 {1,0,1,0,1,0,0,1,1,0,0,1,1,0,0,1,0,1,0,1},
 {1,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,1},
 {1,0,1,1,1,1,0,1,0,2,0,0,1,0,1,1,1,1,0,1},
 {1,0,0,0,0,0,0,0,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,1,1,1,1,1,1,1},
 };
 int playerstartpositionx = playerx = 10*cellwidth;
 int playerstartpositiony = playery = 13*cellheight;
 int playerx = playerstartpositionx;
 int playery = playerstartpositiony;
 // For the pattern movement ai
 int numai = 10;
 int ai[][] = new int[ numai ][ 10 ]; // ,active,x,y,direction[0-up,1-right,2-down,3-left]
 public void init() {
        setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
        bufferGraphics = offscreen.getGraphics();
        // initiate ai
        ai[ 0 ][ 0 ] = 1;
        ai[ 0 ][ 1 ] = 1 * cellwidth;
        ai[ 0 ][ 2 ] = 1 * cellheight;
        ai[ 0 ][ 3 ] = 1;
  //6 2
  ai[ 1 ][ 0 ] = 1;
  ai[ 1 ][ 1 ] = 6 * cellwidth;
  ai[ 1 ][ 2 ] = 2 * cellheight;
  ai[ 1 ][ 3 ] = 2;
  // 13 10
  ai[ 2 ][ 0 ] = 1;
  ai[ 2 ][ 1 ] = 13 * cellwidth;
  ai[ 2 ][ 2 ] = 10 * cellheight;
  ai[ 2 ][ 3 ] = 2;
  new Thread(this).start();
 }
    public void run() {
        for(;;) { // animation loop never ends
   moveplayer();
   updateai();
         repaint();
         try {
             Thread.sleep(10);
             }
             catch (InterruptedException e) {
             }
     }
    }

 public void updateai(){
  for ( int i = 0 ; i < numai ; i++ ){
   if ( ai[ i ][ 0 ] == 1 ){
    // Movement up ( 0 )
    if ( ai[ i ][ 3 ] == 0 ){
     if ( ismapcollision( ai[ i ][ 1 ] , ai[ i ][ 2 ] - 1 ) ){
      ai[ i ][ 3 ] = 2;
     }else{
      ai[ i ][ 2 ]--;
     }
    }
    // Movement right ( 1 )
    if ( ai[ i ][ 3 ] == 1 ){
     if ( ismapcollision( ai[ i ][ 1 ] + 1 , ai[ i ][ 2 ] ) ){
      ai[ i ][ 3 ] = 3;
     }else{
      ai[ i ][ 1 ]++;
     }
    }
    // Movement down ( 2 )
    if ( ai[ i ][ 3 ] == 2 ){
     if ( ismapcollision( ai[ i ][ 1 ] , ai[ i ][ 2 ] + 1 ) ){
      ai[ i ][ 3 ] = 0;
     }else{
      ai[ i ][ 2 ]++;
     }
    }
    // Movement left ( 3 )
    if ( ai[ i ][ 3 ] == 3 ){
     if ( ismapcollision( ai[ i ][ 1 ] - 1 , ai[ i ][ 2 ] ) ){
      ai[ i ][ 3 ] = 1;
     }else{
      ai[ i ][ 1 ]--;
     }
    }
   // Collision with the ai
   Rectangle rec1 = new Rectangle(  playerx ,
             playery ,
             cellwidth ,
             cellheight );
   Rectangle rec2 = new Rectangle(  ai[ i ][ 1 ],
            ai[ i ][ 2 ],
            cellwidth,
            cellheight);
   if( rec1.intersects( rec2 ) ){
     playerx = playerstartpositionx;
     playery = playerstartpositiony;
   }

   }
  }
 }

    public boolean ismapcollision(int x, int y){

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

    public void moveplayer(){
     if (ismovingright == true && ismapcollision(playerx + 1,playery) == false){
      playerx++;
     }
     if (ismovingup == true && ismapcollision(playerx,playery-1) == false){
      playery--;
     }
     if (ismovingdown == true && ismapcollision(playerx,playery+1) == false){
      playery++;
     }
     if (ismovingleft == true && ismapcollision(playerx-1,playery) == false){
      playerx--;
     }

    }

     public void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().height);
  // Draw map
        for( int y = 0 ; y < mapheight ; y++ ){
         for ( int x = 0 ; x < mapwidth ; x++){
          // walls
          if( map[y][x] == 1 ){
     bufferGraphics.setColor( Color.white );
           bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
          }
         }
        }
  // Draw ai
  for ( int i = 0 ; i < numai ; i++ ){
   if ( ai[ i ][ 0 ] == 1 ){
    bufferGraphics.setColor( Color.blue );
    bufferGraphics.fillOval(  ai[ i ][ 1 ] ,
           ai[ i ][ 2 ] ,
           cellwidth ,
           cellheight );
   }
  }

     bufferGraphics.setColor(Color.red);

  bufferGraphics.fillOval(playerx,playery,cellwidth,cellheight);

        bufferGraphics.drawString("2D Topdown Patrolling ai.",10,10);
  bufferGraphics.drawString("w/s/a/d to move player.",10,237);

        g.drawImage(offscreen,0,0,this);
     }
  public boolean keyDown (Event e, int key){
   if(key==97)
        {
         ismovingleft = true;
        }
        if(key==100)
        {
         ismovingright = true;
        }
        if(key==119)
        {
         ismovingup = true;
        }
        if(key==115)
        {
         ismovingdown = true;
        }

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

   if(key==97)
        {
         ismovingleft = false;
        }
        if(key==100)
        {
         ismovingright = false;
        }
        if(key==119)
        {
         ismovingup = false;
        }
        if(key==115)
        {
         ismovingdown = false;
        }
//  System.out.println(""+key);
  return true;
 }


}


Topdown Portals Example





Portals example. Move into the yellow blocks to switch levels.

 


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

public class topdownportalsexample001 extends Applet implements Runnable{

 Graphics bufferGraphics;
 Image offscreen;
 boolean ismovingleft;
 boolean ismovingright;
 boolean ismovingup;
 boolean ismovingdown;
 int isonmap = 1;
 boolean isonportal =  false;
 long portaltimeout = 0;
 int mapwidth = 20;
 int mapheight = 15;
 int cellwidth = 16;
 int cellheight = 16;
 private int map[][] = new int[ mapheight ][ mapwidth ];
 private int map2[][] = new int[][]{
 {1,1,1,1,1,1,1,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,0,0,0,0,0,0,0,1},
 {1,0,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,0,1},
 {1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
 {1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
 {1,0,1,0,0,0,0,1,0,1,1,0,1,0,0,0,0,1,0,1},
 {1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},
 {1,0,1,1,1,1,0,1,0,2,0,0,1,0,1,1,1,1,0,1},
 {1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},
 {1,0,1,0,0,0,0,1,0,1,1,0,1,0,0,0,0,1,0,1},
 {1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
 {1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
 {1,0,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,1},
 {1,0,0,0,0,0,0,0,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,1,1,1,1,1,1,1},
 };
 private int map1[][] =  new int[][]{
 {1,1,1,1,1,1,1,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,0,0,0,0,0,0,0,1},
 {1,0,1,1,1,1,0,1,0,0,0,0,1,0,1,1,1,1,0,1},
 {1,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,1},
 {1,0,1,0,1,0,0,1,1,0,0,1,1,0,0,1,0,1,0,1},
 {1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1},
 {1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
 {1,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,1},
 {1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
 {1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1},
 {1,0,1,0,1,0,0,1,1,0,0,1,1,0,0,1,0,1,0,1},
 {1,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,1},
 {1,0,1,1,1,1,0,1,0,2,0,0,1,0,1,1,1,1,0,1},
 {1,0,0,0,0,0,0,0,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,1,1,1,1,1,1,1},
 };
 int playerx = 10*cellwidth;
 int playery = 13*cellheight;
 public void init() {
        setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
        bufferGraphics = offscreen.getGraphics();
  // Copy map1 to map so that we have something to show
        for ( int y = 0 ; y < mapheight ; y++ ){
         for ( int x = 0 ; x < mapwidth ; x++ ){
          map[ y ][ x ] = map1[ y ][ x ];
         }
        }
  new Thread(this).start();
 }
    public void run() {
        for(;;) { // animation loop never ends
   moveplayer();
   updateportals();
         repaint();
         try {
             Thread.sleep(10);
             }
             catch (InterruptedException e) {
             }
     }
    }

    public void updateportals(){
      if ( isportalcollision( playerx , playery ) == true ){
       boolean selected = false;
       if ( isonmap == 1 ){
        selected = true;
        for ( int y = 0 ; y < mapheight ; y++ ){
         for ( int x = 0 ; x < mapwidth ; x++ ){
          map[ y ][ x ] = map2[ y ][ x ];
         }
        }
        isonmap = 2;
       }
       if ( isonmap == 2 && selected == false ){
        for ( int y = 0 ; y < mapheight ; y++ ){
         for ( int x = 0 ; x < mapwidth ; x++ ){
          map[ y ][ x ] = map1[ y ][ x ];
         }
        }
        isonmap = 1;
       }
      }
    }

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

    public boolean ismapcollision(int x, int y){

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

    public void moveplayer(){
     if (ismovingright == true && ismapcollision(playerx + 1,playery) == false){
      playerx++;
     }
     if (ismovingup == true && ismapcollision(playerx,playery-1) == false){
      playery--;
     }
     if (ismovingdown == true && ismapcollision(playerx,playery+1) == false){
      playery++;
     }
     if (ismovingleft == true && ismapcollision(playerx-1,playery) == false){
      playerx--;
     }

    }

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

        for( int y = 0 ; y < mapheight ; y++ ){
         for ( int x = 0 ; x < mapwidth ; x++){
          // walls
          if( map[y][x] == 1 ){
     bufferGraphics.setColor(Color.white);
           bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
          }
          // portals
          if( map[ y ][ x ] == 2 ){
           bufferGraphics.setColor(Color.yellow);
           bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
          }
         }
        }

     bufferGraphics.setColor(Color.red);

  bufferGraphics.fillOval(playerx,playery,cellwidth,cellheight);

        bufferGraphics.drawString("2D Topdown Portals.",10,10);
  bufferGraphics.drawString("w/s/a/d to move player.",10,237);

        g.drawImage(offscreen,0,0,this);
     }
  public boolean keyDown (Event e, int key){
   if(key==97)
        {
         ismovingleft = true;
        }
        if(key==100)
        {
         ismovingright = true;
        }
        if(key==119)
        {
         ismovingup = true;
        }
        if(key==115)
        {
         ismovingdown = true;
        }

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

   if(key==97)
        {
         ismovingleft = false;
        }
        if(key==100)
        {
         ismovingright = false;
        }
        if(key==119)
        {
         ismovingup = false;
        }
        if(key==115)
        {
         ismovingdown = false;
        }
//  System.out.println(""+key);
  return true;
 }


}


zaterdag 7 april 2012

Platformer Time Dissapearing Tiles Example



In this example you can move the player across tiles that dissapear after 2 seconds after they have been touched.

 


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

public class tdtilesexample001 extends Applet implements Runnable {
 Graphics     bufferGraphics;
    Image      offscreen;
 private int map[][] =  new int[][]{
 {1,1,1,1,1,1,1,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,1,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,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,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,1,1,1,2,2,2,2,2,2,2,1,1,1},
 {1,0,0,0,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,0,0,0,0,0,0,0,1},
 {1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,2,2,1,1,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,0,0,0,1},
 {1,0,0,0,0,0,0,0,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,1,1,1,1,1,1,1},
 };
 int mapwidth = 20;
 int mapheight = 15;
 int cellwidth = 16;
 int cellheight = 16;
 double     px =     132;
 double    py =    200;
 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     numdoors =   10;
 int     numtdt =   32;
 int[][]    tdt =    new int[ numtdt ][ 3 ]; // active , x , y , timeout
 long[]    tdttimeout =  new long[ numtdt ];

 public void init() {
  setBackground(Color.black);
     offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();
     // read time dissapearing tiles
     for ( int y = 0 ; y < mapheight ; y++ ){
     for ( int x = 0 ; x < mapwidth ; x++ ){
      if ( map[ y ][ x ] == 2 ){
       int n = freetdt();
       tdt[ n ][ 0 ] = 1;
       tdt[ n ][ 1 ] = x;
       tdt[ n ][ 2 ] = y;
       tdttimeout[ n ] = -1;
       map[ y ][ x ] = 0;
      }
     }
     }
  new Thread(this).start();
 }

 public int freetdt(){
  for ( int i = 0 ; i < numtdt ; i++ ){
   if ( tdt[ i ][ 0 ] == 0 ){
    return i;
   }
  }
  return -1;
 }

    public void run() {
     for(;;) { // animation loop never ends
         repaint();
         try {
       updateplayer();
    updatetdt();
             Thread.sleep(16);
            }
             catch (InterruptedException e) {
             }
     }
    }
    public void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
        // Draw map
        for( int y = 0 ; y < mapheight ; y++ ){
         for ( int x = 0 ; x < mapwidth ; x++){
          if( map[y][x] == 1 ){
           bufferGraphics.setColor(Color.white);
           bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
          }

         }
        }
        // Draw time dissapearing tiles
        bufferGraphics.setColor(Color.yellow);
        for ( int i = 0 ; i < numtdt ; i++ ){
         if ( tdt[ i ][ 0 ] == 1 ){
          bufferGraphics.fillRect(  tdt[ i ][ 1 ] * cellwidth ,
                 tdt[ i ][ 2 ] * cellheight ,
                 cellwidth ,
                 cellheight );
         }
        }
        // Draw player
        bufferGraphics.setColor(Color.red);
        bufferGraphics.fillRect( (int)px , (int)py , pwidth , pheight );

        bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Platformer Time Dissapearing tiles Example.",10,10);
        bufferGraphics.drawString("a - left, d - right, space - jump.",10,240);

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

    public void updatetdt(){
     for ( int i = 0 ; i < numtdt ; i++ ){
      if ( tdt[ i ][ 0 ] == 1 ){
       if ( tdtcollision( (int)px , (int)py + 1 ) == i ){
        tdttimeout[ i ] = System.currentTimeMillis() + 2000;
       }
      }
     }
     for ( int i = 0 ; i < numtdt ; i++ ){
      if ( tdttimeout[ i ] > -1 ){
       if ( tdttimeout[ i ] < System.currentTimeMillis() ){
        tdttimeout[ i ] = -1;
        tdt[ i ][ 0 ] = -1;
       }
      }
     }
    }

 public void updateplayer(){

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

  if ( isfalling == true && isjumping == false ){
   for ( int i = 0 ; i < gravity ; i++ ){
    if (  mapcollision ( (int)px , (int)(py + 1) , pwidth , pheight ) == false &&
      tdtcollision( (int)px , (int)py + 1) == -1 ){
     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 &&
      tdtcollision( (int)px , (int)py - 1 ) == -1 ){
     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 int tdtcollision( int x , int y ){
     for ( int i = 0 ; i < numtdt ; i++ ){
      if ( tdt[ i ][ 0 ] == 1 ){
     Rectangle rec1 = new Rectangle( x , y , pwidth , pheight );
    Rectangle rec2 = new Rectangle( tdt[ i ][ 1 ] * cellwidth,
            tdt[ i ][ 2 ] * cellheight,
            cellwidth,
            cellheight);
    if( rec1.intersects( rec2 )) return i;

      }
     }
     return -1;
    }

  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[y1][x1] == 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 keyDown (Event e, int key){
    if( key == 97 ) // a key
        {
         ismovingleft = true;
        }
        if(key== 100) // d key
        {
          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 == 97 ) // a key
        {
          ismovingleft = false;
        }
        if( key == 100 ) // d key
        {
          ismovingright = false;
        }
  return true;
 }



}

donderdag 5 april 2012

2d Topdown Doors and keys Example




In this example there is a map where you can grab the yellow keys and open the blue doors. The controls hints are shown in the applet window. The sourcecode is below.

 


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

public class topdowndoorsandkeysexample001 extends Applet implements Runnable{

 Graphics bufferGraphics;
 Image offscreen;
 boolean ismovingleft;
 boolean ismovingright;
 boolean ismovingup;
 boolean ismovingdown;
 private int map[][] =  new int[][]{
 {1,1,1,1,1,1,1,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,0,0,0,0,0,0,3,1},
 {1,0,1,1,1,1,0,1,1,1,0,0,1,1,1,0,1,1,0,1},
 {1,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,0,0,1},
 {1,0,1,0,1,0,0,1,0,1,0,1,1,0,1,0,0,1,0,1},
 {1,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1},
 {1,0,1,0,2,0,0,1,0,2,0,0,1,0,2,0,0,1,0,1},
 {1,0,1,1,1,0,0,1,1,1,0,1,1,1,1,0,0,0,0,1},
 {1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
 {1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,1},
 {1,0,1,0,1,0,0,1,1,0,0,1,1,0,0,1,0,1,0,1},
 {1,0,0,0,1,0,0,1,0,0,0,0,1,0,0,1,0,0,0,1},
 {1,0,1,1,1,1,0,1,0,0,0,0,1,0,1,1,1,1,0,1},
 {1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,1},
 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
 };
 int mapwidth = 20;
 int mapheight = 15;
 int cellwidth = 16;
 int cellheight = 16;
 int pwidth = 16;
 int pheight = 16;
 int playerx = 10*cellwidth;
 int playery = 13*cellheight;
 int numkeys = 4;
 int[][] keys = new int[ numkeys ][ 3 ]; // active, x , y
 int numdoors = 4;
 int[][] doors = new int[ numdoors ][ 3 ]; // active, x , y
 int playerkeys = 0;

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

 public void readkeysanddoors(){
  for ( int y = 0 ; y < mapheight ; y++ ){
  for ( int x = 0 ; x < mapwidth ; x++ ){
   if ( map[ y ][ x ] == 2 ){ // if door
    int n = findfreedoor();
    doors[ n ][ 0 ] = 1;
    doors[ n ][ 1 ] = x;
    doors[ n ][ 2 ] = y;
    map[ y ][ x ] = 0;
   }
   if ( map[ y ][ x ] == 3 ){ // if key
    int n = findfreekey();
    keys[ n ][ 0 ] = 1;
    keys[ n ][ 1 ] = x;
    keys[ n ][ 2 ] = y;
    map[ y ][ x ] = 0;
   }
  }
  }
 }

 public int findfreedoor(){
  for ( int i = 0 ; i < numdoors ; i++ ){
   if ( doors[ i ][ 0 ] == 0 ){
    return i;
   }
  }
  return -1;
 }
 public int findfreekey(){
  for ( int i = 0 ; i < numkeys ; i++ ){
   if ( keys[ i ][ 0 ] == 0 ){
    return i;
   }
  }
  return -1;
 }

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

 public int isdoorcollision( int x , int y ){
  for ( int i = 0 ; i < numdoors ; i++ ){
   if ( doors[ i ][ 0 ] == 1 ){
    Rectangle rec1 = new Rectangle(  doors[ i ][ 1 ] * cellwidth ,
              doors[ i ][ 2 ] * cellheight ,
              cellwidth ,
              cellheight );
    Rectangle rec2 = new Rectangle(  x,
             y,
             cellwidth,
             cellheight);
    if(rec1.intersects(rec2)) return i;
   }
  }
  return -1; }

 public int iskeycollision( int x , int y ){
  for ( int i = 0 ; i < numkeys ; i++ ){
   if ( keys[ i ][ 0 ] == 1 ){
    Rectangle rec1 = new Rectangle(  keys[ i ][ 1 ] * cellwidth ,
              keys[ i ][ 2 ] * cellheight ,
              cellwidth ,
              cellheight );
    Rectangle rec2 = new Rectangle(  x,
             y,
             cellwidth,
             cellheight);
    if(rec1.intersects(rec2)) return i;
   }
  }
  return -1;
 }

    public boolean ismapcollision(int x, int y){

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

 public void playerkeys(){
  if ( ismovingleft ){
   int thekey = iskeycollision( playerx - 1 , playery );
   if ( thekey > -1 ){
    playerkeys++;
    keys[ thekey ][ 0 ] = 0;
   }
  }
  if ( ismovingright ){
   int thekey = iskeycollision( playerx + 1 , playery );
   if ( thekey > -1 ){
    playerkeys++;
    keys[ thekey ][ 0 ] = 0;
   }
  }
  if ( ismovingup ){
   int thekey = iskeycollision( playerx , playery - 1);
   if ( thekey > -1 ){
    playerkeys++;
    keys[ thekey ][ 0 ] = 0;
   }
  }
  if ( ismovingdown ){
   int thekey = iskeycollision( playerx , playery + 1 );
   if ( thekey > -1 ){
    playerkeys++;
    keys[ thekey ][ 0 ] = 0;
   }
  }

 }

 public void playerdoors(){
    if ( ismovingleft == true ){
   if ( playerkeys > 0 ){
    int thedoor = isdoorcollision( playerx - 1 , playery );
    if ( thedoor > -1 ){
     playerkeys--;
     doors[ thedoor ][ 0 ] = 0;
    }
   }
  }
  if ( ismovingright == true ){
   if ( playerkeys > 0 ){
    int thedoor = isdoorcollision( playerx + 1 , playery );
    if ( thedoor > -1 ){
     playerkeys--;
     doors[ thedoor ][ 0 ] = 0;
    }
   }
  }
  if ( ismovingup == true ){
   if ( playerkeys > 0 ){
    int thedoor = isdoorcollision( playerx , playery - 1 );
    if ( thedoor > -1 ){
     playerkeys--;
     doors[ thedoor ][ 0 ] = 0;
    }
   }
  }
  if ( ismovingdown == true ){
   if ( playerkeys > 0 ){
    int thedoor = isdoorcollision( playerx , playery + 1 );
    if ( thedoor > -1 ){
     playerkeys--;
     doors[ thedoor ][ 0 ] = 0;
    }
   }
  }

 }

    public void moveplayer(){
     if (  ismovingright == true &&
       ismapcollision( playerx + 1 , playery ) == false &&
       isdoorcollision( playerx + 1 , playery ) < 0 ){
      playerx++;
     }
     if ( ismovingup == true &&
       ismapcollision( playerx , playery - 1 ) == false &&
       isdoorcollision( playerx , playery - 1 ) < 0 ){
      playery--;
     }
     if ( ismovingdown == true &&
       ismapcollision( playerx , playery + 1 ) == false &&
       isdoorcollision( playerx , playery + 1 ) < 0 ){
      playery++;
     }
     if ( ismovingleft == true &&
       ismapcollision( playerx - 1 , playery ) == false &&
       isdoorcollision( playerx - 1 , playery ) < 0 ){
      playerx--;
     }


    }

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

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

  // Draw doors
  bufferGraphics.setColor( Color.blue );
  for ( int i = 0 ; i < numdoors ; i++ ){
   if ( doors[ i ][ 0 ] == 1 ){
    bufferGraphics.fillRect(  doors[ i ][ 1 ] * cellwidth ,
           doors[ i ][ 2 ] * cellheight ,
           cellwidth ,
           cellheight );
   }
  }
  // Draw keys
  bufferGraphics.setColor( Color.yellow );
  for ( int i = 0 ; i < numkeys ; i++ ){
   if ( keys[ i ][ 0 ] == 1 ){
    bufferGraphics.fillOval( keys[ i ][ 1 ] * cellwidth ,
           keys[ i ][ 2 ] * cellheight ,
           cellwidth ,
           cellheight );
   }
  }

  // Draw player
     bufferGraphics.setColor(Color.red);
  bufferGraphics.fillOval(playerx,playery,cellwidth,cellheight);

        bufferGraphics.drawString("2D Topdown Doors and keys.",10,10);
        bufferGraphics.drawString("Keys : " + playerkeys , 200 , 10 );
  bufferGraphics.drawString("w/s/a/d = movement, yellow = key, blue = door.",10,237);

        g.drawImage(offscreen,0,0,this);
     }
  public boolean keyDown (Event e, int key){
   if(key==97)
        {
         ismovingleft = true;
        }
        if(key==100)
        {
         ismovingright = true;
        }
        if(key==119)
        {
         ismovingup = true;
        }
        if(key==115)
        {
         ismovingdown = true;
        }

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

   if(key==97)
        {
         ismovingleft = false;
        }
        if(key==100)
        {
         ismovingright = false;
        }
        if(key==119)
        {
         ismovingup = false;
        }
        if(key==115)
        {
         ismovingdown = false;
        }
//  System.out.println(""+key);
  return true;
 }


}

Platformer Doors Example



Platformer Doors Example. The instructions are shown in the applet window. The yellow blocks are the doors. Sourcecode is below.

 


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

public class platformerdoorsexample001 extends Applet implements Runnable {
 Graphics     bufferGraphics;
    Image      offscreen;
 private int map[][] =  new int[][]{
 {1,1,1,1,1,1,1,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,1,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,1},
 {1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,1,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,0,0,0,1},
 {1,0,0,0,0,0,0,0,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,1,1,1,1,1,1,1},
 };
 int mapwidth = 20;
 int mapheight = 15;
 int cellwidth = 16;
 int cellheight = 16;
 double     px =     132;
 double    py =    200;
 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     numdoors =   10;
 private int[][] door = new int[ numdoors ][ 4 ]; // door ] active , x , y , teleport_to

 public void init() {
  setBackground(Color.black);
     offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();
     // Door 1
     door[ 0 ][ 0 ] = 1;
     door[ 0 ][ 1 ] = 14;
     door[ 0 ][ 2 ] = 13;
     door[ 0 ][ 3 ] = 1;
     // Door 2
     door[ 1 ][ 0 ] = 1;
     door[ 1 ][ 1 ] = 15;
     door[ 1 ][ 2 ] = 10;
     door[ 1 ][ 3 ] = 0;
     // Door 3
     door[ 2 ][ 0 ] = 1;
     door[ 2 ][ 1 ] = 10;
     door[ 2 ][ 2 ] = 10;
     door[ 2 ][ 3 ] = 3;
     // Door 4
     door[ 3 ][ 0 ] = 1;
     door[ 3 ][ 1 ] = 14;
     door[ 3 ][ 2 ] = 3;
     door[ 3 ][ 3 ] = 2;
     //
  new Thread(this).start();
 }
    public void run() {
     for(;;) { // animation loop never ends
         repaint();
         try {
       updateplayer();
             Thread.sleep(16);
            }
             catch (InterruptedException e) {
             }
     }
    }
    public void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
        // Draw map
        for( int y = 0 ; y < mapheight ; y++ ){
         for ( int x = 0 ; x < mapwidth ; x++){
          if( map[y][x] == 1 ){
           bufferGraphics.setColor(Color.white);
           bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
          }
          if( map[y][x] == 2 ){
           bufferGraphics.setColor(Color.yellow);
           bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
          }

         }
        }
        // Draw doors
        bufferGraphics.setColor(Color.yellow);
        for ( int i = 0 ; i < numdoors ; i++ ){
         if ( door[ i ][ 0 ] == 1 ){
          bufferGraphics.fillRect(  door[ i ][ 1 ] * cellwidth ,
                 door[ i ][ 2 ] * cellheight ,
                 cellwidth ,
                 cellheight );
         }
        }
        // Draw player
        bufferGraphics.setColor(Color.red);
        bufferGraphics.fillRect( (int)px , (int)py , pwidth , pheight );

        bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Platformer Doors Example.",10,10);
        bufferGraphics.drawString("a - left, d - right, space - jump, w - enter door.",10,240);

       g.drawImage(offscreen,0,0,this);
    }
    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 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[y1][x1] == 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 Integer doorcollision( int x , int y , int w , int h ){
  int returnvalue = -1;
  for ( int i = 0 ; i < numdoors ; i++ ){
   if ( door[ i ][ 0 ] == 1 ){
    Rectangle rec1 = new Rectangle( x , y , w , h );
    Rectangle rec2 = new Rectangle( door[ i ][ 1 ]  * cellwidth,
            door[ i ][ 2 ]  * cellheight,
            cellwidth,
            cellheight);
    if( rec1.intersects( rec2 )) return i;
   }
  }
  return returnvalue;
 }

  public boolean keyDown (Event e, int key){
    if( key == 97 ) // a key
        {
         ismovingleft = true;
        }
        if(key== 100) // d key
        {
          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 == 97 ) // a key
        {
          ismovingleft = false;
        }
        if( key == 100 ) // d key
        {
          ismovingright = false;
        }
  if( key == 119 ) // w key
  {
   int thedoor = -1;
   thedoor = doorcollision( (int)px , (int)py , pwidth , pheight );
   if ( thedoor > -1 ){
    int tdoor = door[ thedoor ][ 3 ];
    px = door[ tdoor ][ 1 ] * cellwidth + cellwidth / 4;
    py = door[ tdoor ][ 2 ] * cellheight;
   }
  }
  return true;
 }


}


woensdag 4 april 2012

Platformer Ladders Example




Use w s a d to control the player block. Move to the ladders and press w or s to move up or down.


 

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

public class platformerladdersexample001 extends Applet implements Runnable {
 Graphics bufferGraphics;
    Image offscreen;
 private int map[][] =  new int[][]{
 {1,1,1,1,1,1,1,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,1,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,1,2,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,1,2,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1},
 {1,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,2,1,1,1,1,1,1,1,2,1,0,0,1},
 {1,0,0,0,0,0,0,2,0,0,0,0,1,1,1,2,1,0,0,1},
 {1,0,0,0,1,1,1,2,1,0,0,0,0,0,0,2,0,0,0,1},
 {1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,2,1,1,1},
 {1,1,1,2,1,0,0,0,0,0,0,0,0,0,0,0,2,0,0,1},
 {1,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,1},
 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
 };
 int     mapwidth =    20;
 int     mapheight =   15;
 int     cellwidth =   16;
 int     cellheight =   16;
 double     px =     132;
 double    py =    200;
 int     pwidth =    cellwidth/2;
 int     pheight =    cellheight;
 boolean    isjumping =   false;
 boolean    isfalling =   false;
 double    gravity =    0;
 boolean    ismovingright =  false;
 boolean    ismovingleft =   false;
 boolean    ismovingup =  false;
 boolean    ismovingdown =   false;
 double    jumpforce =   3;
 boolean    onladder =    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
     updateplayer();
         repaint();
         try {
             Thread.sleep(16);
            }
             catch (InterruptedException e) {
            }
     }
    }

 public void update (Graphics g) {
  bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().height );
        // Draw map
        for( int y = 0 ; y < mapheight ; y++ ){
         for ( int x = 0 ; x < mapwidth ; x++){
          if( map[ y ][ x ] == 1 ){
        bufferGraphics.setColor ( Color.white );
           bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , cellheight );
          }
          if( map[ y ][ x ] == 2 ){ // Draw ladder
           bufferGraphics.setColor ( new Color( 170 , 130 , 0 ) );
           bufferGraphics.fillRect( x * cellwidth , y * cellheight , cellwidth , 3 );
           bufferGraphics.fillRect( x * cellwidth , y * cellheight +6 , cellwidth , 3 );
           bufferGraphics.fillRect( x * cellwidth , y * cellheight + 12 , cellwidth , 3 );

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

     bufferGraphics.setColor ( Color.green );
     bufferGraphics.drawString( "Platformer Ladders Eample." , 10 , 10 );
       g.drawImage(offscreen,0,0,this);
  }
    public void updateplayer(){
  boolean ontheladder = laddercollision( (int)px , (int)py , pwidth , pheight );
  if ( ontheladder ) {
   if ( ismovingup ) {
    py--;
   }
  }
  if (  laddercollision( (int)px , (int)py+pheight , pwidth , 1 ) ||
     mapcollision( (int)px , (int)py+pheight , pwidth , pheight ) == false ) {
   if ( ismovingdown ) {
    py++;
   }
  }

  if ( isjumping == false && isfalling == false && ontheladder == 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 &&
      laddercollision( (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 boolean laddercollision( 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[y1][x1] == 2 ){
       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 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[y1][x1] == 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 keyDown (Event e, int key){
    if( key == 97 ) // a key
        {
         ismovingleft = true;
        }
        if(key== 100) // d key
        {
          ismovingright = true;
        }
    if( key == 119 ) // w key
        {
         ismovingup = true;
        }
        if(key== 115) // s key
        {
          ismovingdown = 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 == 97 ) // a key
        {
          ismovingleft = false;
        }
        if( key == 100 ) // d key
        {
          ismovingright = false;
        }
    if( key == 119 ) // w key
        {
          ismovingup = false;
        }
        if( key == 115 ) // s key
        {
          ismovingdown = false;
        }

  return true;
 }

}