vrijdag 30 september 2011

Sticking and moving on/to walls and ceilings.





In the ninetees there was this game called Flood that I played on an Amiga Computer. In this game you could stick and move on walls and ceilings. I watched some footage of the game on youtube recently and decided to program that sticky wall/ceiling part for myself. Use the W,S,A,D keys and space to move.



 



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

public class Stickingtowallsandceilings 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,1,0,0,0,1},
      {1,0,0,0,0,0,0,0,1,0,0,0,1},
      {1,0,0,0,0,0,0,0,1,0,0,0,1},
      {1,0,0,0,0,0,0,0,1,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,1,0,0,0,0,0,1},
      {1,0,0,0,0,0,1,0,0,0,0,0,1},
      {1,0,0,0,0,0,1,0,0,0,0,0,1},
      {1,0,0,1,0,0,1,0,0,0,0,0,1},
      {1,0,0,1,0,0,0,0,0,0,0,0,1},
      {1,0,0,1,0,0,0,0,0,0,0,0,1},
      {1,0,0,1,0,0,0,0,0,0,0,0,1},
      {1,0,0,1,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,1,0,0,0,0,0,1},
      {1,0,0,0,0,0,1,0,0,0,0,0,1},
      {1,0,0,0,0,0,1,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;
 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;
 boolean    ismovingup =   false;
 boolean    ismovingdown =   false;
 boolean    issticking =   false;
 boolean    isstickingceiling = false;
 boolean    isstickingwall = 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(){
 }

 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 ( issticking == true ) {
   //
   // Here you stick on a wall or a ceiling
   //

   if ( ismovingup == true ) {
    if ( mapcollision ( (int)px , (int)(py - 1) , pwidth , pheight ) == false ){
     py -= 1;
    }
   }
   if ( ismovingdown == true ) {
    if ( mapcollision ( (int)px , (int)(py + 1) , pwidth , pheight ) == false ){
     py += 1;
    }
    if ( mapcollision ( (int)px , (int)(py - 1) , pwidth , pheight ) == false ){
    if ( mapcollision ( (int)( px - 1 ) , (int)py , pwidth , pheight ) == false ){
    if ( mapcollision ( (int)( px + 1 ) , (int)py , pwidth , pheight ) == false ){
     issticking = false;
    }}}
   }
   if ( ismovingleft == true ) {
    if ( mapcollision ( (int)( px - 1 ), (int)py , pwidth , pheight ) == false ){
    if ( mapcollision ( (int)px, (int)( py - 1 ) , pwidth , pheight ) == false ){
     px -= 1;
     issticking = false;
    }}
    if ( mapcollision ( (int)px, (int)( py - 1 ) , pwidth , pheight ) == true ){
    if ( mapcollision ( (int)( px - 1 ), (int)py , pwidth , pheight ) == false ){
     px -= 1;
    }}
   }
   if ( ismovingright == true ) {
    if ( mapcollision ( (int)( px + 1 ) , (int)py , pwidth , pheight ) == false ){
    if ( mapcollision ( (int)px, (int)( py - 1 ) , pwidth , pheight ) == false ){
     px += 1;
     issticking = false;
    }}
    if ( mapcollision ( (int)px, (int)( py - 1 ) , pwidth , pheight ) == true ){
    if ( mapcollision ( (int)( px + 1 ), (int)py , pwidth , pheight ) == false ){
     px += 1;
    }}
   }

  }

  if ( issticking == false ) {
   //
   // Here you do not stick on a wall or ceiling
   //
   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;
    }else{
     issticking = true;
     isjumping = false;
     isfalling = false;
    }
   }
   if (ismovingleft){
    if ( mapcollision( (int)(px - 1) , (int)py , pwidth , pheight ) == false ){
     px -= 1;
    }else{
     issticking = true;
     isjumping = false;
     isfalling = false;
    }
   }

   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;
      issticking = true;
      isfalling = false;
      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[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);
        // Draw map
        bufferGraphics.setColor(Color.red);
        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 );
          }
         }
        }
        bufferGraphics.setColor(Color.white);
        bufferGraphics.drawString("Platformer sticky walls.",10,10);

        // Draw player
        bufferGraphics.fillRect( (int)px , (int)py , pwidth , pheight );
        bufferGraphics.setColor(Color.white);
  bufferGraphics.drawString("W, S , A , D = movement. Space = jump." , 10 , 220 );
       g.drawImage(offscreen,0,0,this);
    }

  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 for jump
      {
        if( isfalling == false && isjumping == false && issticking == 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;
 }

}

Multiple Levels/Maps Tilemap



In this example you can switch maps/levels by pressing the cursor keys.



 


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

public class Multiple_Levels_Tilemap extends Applet implements Runnable {
 // Graphics for double buffering.
 Graphics    bufferGraphics;
    Image     offscreen;
 private short map[][][]={
      // Level 1
      {
      {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,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,1,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,1,1,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,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}
      },
      // Level 2
      {
      {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,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,1,0,0,0,0,0,0,0,0,1},

      {1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,1,1,1,1,1,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,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}
      },
      // Level 3
      {
      {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,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,1,1,1,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,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     currentlevel =   0;
 int     mapwidth =    20;
 int      mapheight =   15;
 int      cellwidth =   16;
 int      cellheight =   16;

 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 void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
        bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Multiple Levels Tilemap.",20,30);
        bufferGraphics.drawString("Cursors Left and Right for levels.",20,50);

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

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

  public boolean keyDown (Event e, int key){

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

   return true;
  }

 public boolean keyUp (Event e, int key){
    if( key == Event.LEFT )
        {
   if (currentlevel > 0) currentlevel--;
        }
        if(key==Event.RIGHT)
        {
   if (currentlevel < 2) currentlevel++;
        }

  return true;
 }

}



zondag 18 september 2011

Preparing Amiga Rodlands and Bubble Bobble platform example

The last couple of days I have been watching videos of Rodlands and Bubble Bubble. I have been thinking about how to program the games. I want to either program a couple of features out of the games or a one level example for the weblog.

I still need to study the videos more. The features I have been going through I have never done before so it will be a challenge. But I am doing exercises and drinking beer. I have no idea how soon I will be programming the game programming examples.

On a other note. I have read that the things to get started with on the Internet with java is dissapointing. I agree with that. It took me quite a long while to get a grips with Java and game programming. Java is the most populair programming language but there seems to be a lack of good game programming resources. There are a couple of books but they are not that good.

I am going to get some suplies now. After that I will keep thinking on how to program the game examples for the weblog.

Update :
I have not done anything with rodlands and bubble bobble yet. I can not sit and program for some reason. I keep deciding to do other things in stead.