woensdag 29 februari 2012

I received a new book - Ai for game developers.

I ordered a book yesterday. Today I got it. It is a book about Artificial Intelligence. It is for Novice programmers. There is a whole chapter on a* in it. I think I will be making more artificial intelligence examples for the weblog in the future.

On google books you can read through a large part of the book. Last year I made a random obstacle avoidance example in another language from this book. This method is great for moving ai through forrest map parts.

woensdag 22 februari 2012

The Gimp - investing time in art creation.

It has been years ago that I last draw art for games. Sometimes I get the idea to spend time in drawing programs. I now downloaded the latest version of Gimp after looking around for drawing programs. I could find nothing better then the Gimp. I already found my favourite brush, the Smudge brush. This one can be used to smear the pixels around. It is easy to make clouds with the smudge brush for instance.
I want to spend time in the future drawing. I want to get good at it. This means getting to the 10.000 hours of experience with it. This means it will probably be old before I am good enough.

In blogger you can upload png files so I will be placing example drawings and step by step drawings in posts in the future. I hope that I will be productive. I also still think of things that I want to code.

zaterdag 18 februari 2012

Copperbar Example



In the code below you can see how the copperbar shown above is made. Copperbars were used in games to create a filled background. The effect looks quite nice.


 

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

public class CopperBarExample001 extends Applet {

 public void init() {
  // Set the applet background color to black.
  setBackground(Color.black);
 }

 public void paint(Graphics g) {
  g.setColor( Color.white );
  g.drawString( "Copperbar Example" , 10, 10 );
  int y = 50;
  double col = 0;
  while ( y < 100 ){ // draw the first 50 lines of the copperbar , dark to light
   g.setColor( new Color( 0 , 0 , (int)col ) );
   col += 255 / 50;
   g.drawLine( 0 , y , getSize().width , y );
   y++;
  }
  while ( y < 150 ){ // draw the last 50 lines of the copperbar, light to dark.
   g.setColor( new Color( 0 , 0 , (int)col ) );
   col -= 255 / 50;
   g.drawLine( 0 , y , getSize().width , y );
   y++;
  }

 }


}


Sidescrolling Shooter Shooting Example



In this example you can control a block that shoots 3 types of blocks. The control instructions are on the applet screen.


 

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

public class ShooterShootingExample001 extends Applet implements Runnable {
 Graphics bufferGraphics;
    Image offscreen;
 int px; // player x coordinate.
 int py; // player y coordinate.
 int pw = 16; // player width.
 int ph = 16; // player height.
 boolean pmoveup = false; // player move up.
 boolean pmovedown = false; // player move down.
 int shoottype = 0; // shooting , 0 = no shot, 1 = type 1, 2 = type 2, 3 = type 3
 long lastshottime = 0;
 int numshots = 32;
 double shots[][] = new double[ numshots ][ 5 ]; //active, x, y, mx, my
 int shotw = 3; // shot width.
 int shoth = 3; // shot height.

 public void init() {
  setBackground(Color.black);
     offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();
     px = 16;
     py = getSize().height / 2 - ph / 2;
     // Start the runnable thread.
  new Thread(this).start();
 }
    public void run() {
     for(;;) { // animation loop never ends
   moveplayer();
   playershoot();
   updateshots();
         repaint();
         try {
             Thread.sleep(16);
            }
             catch (InterruptedException e) {
             }
     }
    }
 public void update (Graphics g)
  {
  bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().height );
     bufferGraphics.setColor ( Color.white );
     bufferGraphics.drawString( "Sidescroller Shooter Shooting Example." , 10 , 10 );
  bufferGraphics.drawString( "use : w and s to move up and down." , 10 , 20 );
  bufferGraphics.drawString( "use : i , o and p to shoot." , 10 , 30 );
  bufferGraphics.setColor( Color.red );
  // draw shots.
  for ( int i = 0 ; i < numshots ; i++ ){
   if ( shots[ i ][ 0 ] == 1 ){
    bufferGraphics.fillOval( (int)shots[ i ][ 1 ] , (int)shots[ i ][ 2 ] , shotw , shoth );
   }
  }
  // draw player.
  bufferGraphics.fillRect( px , py , pw , ph );
     g.drawImage(offscreen,0,0,this);
  }
  public boolean keyUp (Event e, int key){
  if ( key == 119 ){ // w key, up
   pmoveup = false;
  }
  if ( key == 115 ){ // s key, down
   pmovedown = false;
  }
  if ( key == 105 ){ // i key, fire 1
   shoottype = 0;
  }
  if ( key == 111 ){ // o key, fire 2
   shoottype = 0;
  }
  if ( key == 112 ){ // p key, fire 3
   shoottype = 0;
  }
    return true;
  }

   public boolean keyDown (Event e, int key){
  if ( key == 119 ){ // w key, up
   pmoveup = true;
  }
  if ( key == 115 ){ // s key, down
   pmovedown = true;
  }
  if ( key == 105 ){ // i key, fire 1
   shoottype = 1;
  }
  if ( key == 111 ){ // o key, fire 2
   shoottype = 2;
  }
  if ( key == 112 ){ // p key, fire 3
   shoottype = 3;
  }
        System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
        return true;
  }

 public void moveplayer(){
  if ( pmoveup ){
   if ( py > 30 ){
    py--;
   }
  }
  if ( pmovedown ){
   if ( py < getSize().height - ph ){
    py++;
   }
  }
 }

 public void playershoot(){
  int n = -1;
  if ( lastshottime < System.currentTimeMillis() ){
   if ( shoottype == 1 ){
    n = getemptyshot();
    if ( n > -1 ){
     shots[ n ][ 0 ] = 1;
     shots[ n ][ 1 ] = px + pw;
     shots[ n ][ 2 ] = py + ph / 2;
     shots[ n ][ 3 ] = 3;
     shots[ n ][ 4 ] = 0;
    }
   }
   if ( shoottype == 2 ){
    n = getemptyshot();
    if ( n > -1 ){
     shots[ n ][ 0 ] = 1;
     shots[ n ][ 1 ] = px + pw;
     shots[ n ][ 2 ] = py + ph / 2;
     shots[ n ][ 3 ] = 3;
     shots[ n ][ 4 ] = 0;
    }
    n = getemptyshot();
    if ( n > -1 ){
     shots[ n ][ 0 ] = 1;
     shots[ n ][ 1 ] = px + pw;
     shots[ n ][ 2 ] = py + ph / 2;
     shots[ n ][ 3 ] = 3;
     shots[ n ][ 4 ] = -2;
    }
    n = getemptyshot();
    if ( n > -1 ){
     shots[ n ][ 0 ] = 1;
     shots[ n ][ 1 ] = px + pw;
     shots[ n ][ 2 ] = py + ph / 2;
     shots[ n ][ 3 ] = 3;
     shots[ n ][ 4 ] = 2;
    }
   }
   if ( shoottype == 3 ){
    n = getemptyshot();
    if ( n > -1 ){
     shots[ n ][ 0 ] = 1;
     shots[ n ][ 1 ] = px + pw;
     shots[ n ][ 2 ] = py + ph / 2;
     shots[ n ][ 3 ] = 3;
     shots[ n ][ 4 ] = 0;
    }
    n = getemptyshot();
    if ( n > -1 ){
     shots[ n ][ 0 ] = 1;
     shots[ n ][ 1 ] = px + pw;
     shots[ n ][ 2 ] = py + ph / 2;
     shots[ n ][ 3 ] = 3;
     shots[ n ][ 4 ] = -2;
    }
    n = getemptyshot();
    if ( n > -1 ){
     shots[ n ][ 0 ] = 1;
     shots[ n ][ 1 ] = px + pw;
     shots[ n ][ 2 ] = py + ph / 2;
     shots[ n ][ 3 ] = 3;
     shots[ n ][ 4 ] = 2;
    }
    n = getemptyshot();
    if ( n > -1 ){
     shots[ n ][ 0 ] = 1;
     shots[ n ][ 1 ] = px + pw;
     shots[ n ][ 2 ] = py + ph / 2;
     shots[ n ][ 3 ] = 0;
     shots[ n ][ 4 ] = -3;
    }
    n = getemptyshot();
    if ( n > -1 ){
     shots[ n ][ 0 ] = 1;
     shots[ n ][ 1 ] = px + pw;
     shots[ n ][ 2 ] = py + ph / 2;
     shots[ n ][ 3 ] = 0;
     shots[ n ][ 4 ] = 3;
    }

   }
   lastshottime = System.currentTimeMillis() + 300;
  }

 }

 public int getemptyshot(){
  for ( int i = 0 ; i < numshots ; i++ ){
   if ( shots[ i ][ 0 ] == 0 ){
    return i;
   }
  }
  return -1;
 }

 public void updateshots(){
  for ( int i = 0 ; i < numshots ; i++ ){
   if ( shots[ i ][ 0 ] == 1 ){
    // Update the shots location on the screen.
    shots[ i ][ 1 ] += shots[ i ][ 3 ];
    shots[ i ][ 2 ] += shots[ i ][ 4 ];
    // If the shots get outside of the screen then disable them.
    if ( shots[ i ][ 1 ] > getSize().width ){
     shots[ i ][ 0 ] = 0;
    }
    if ( shots[ i ][ 2 ] < 0 - shoth ){
     shots[ i ][ 0 ] = 0;
    }
    if ( shots[ i ][ 2 ] > getSize().height ){
     shots[ i ][ 0 ] = 0;
    }

   }
  }
 }

}

vrijdag 17 februari 2012

Rotating Side Scrolling Shooter Attack Wave Example




I made another example. This time a group of enemies fly in a circular pattern through the screen. In a shooter this would be a interesting challenge. When the enemies are done flying through the screen another wave starts. The code below shows how it is made.


 

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

public class SideScrollingAttackWave001 extends Applet implements Runnable {
 Graphics bufferGraphics;
    Image offscreen;
 int numenemies = 16;
 double enemies[][] = new double[ numenemies ][ 8 ]; // active, x , y , mx , angle , radius , locx,locy

 int enemyw = 16;
 int enemyh = 16;

 public void init() {
  setBackground(Color.black);
     offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();
  initiateattackwave();
     // Start the runnable thread.
  new Thread(this).start();
 }
 public void initiateattackwave(){
  // initiate the attack wave
     for ( int i = 0 ; i < 7 ; i++ ){
      enemies[ i ][ 0 ] = 1;
      enemies[ i ][ 1 ] = 0;
      enemies[ i ][ 2 ] = 0;
      enemies[ i ][ 3 ] = -.2;
      enemies[ i ][ 4 ] = i * 16;
      enemies[ i ][ 5 ] = 100;
      enemies[ i ][ 6 ] = getSize().width;
      enemies[ i ][ 7 ] = getSize().height / 2 - 8;
     }
 }
    public void run() {
     boolean noleft;
     for(;;) { // animation loop never ends

   // update the enemies
   for ( int i = 0 ; i < numenemies ; i++ ){
    if ( enemies[ i ][ 0 ] == 1 ){
     enemies[ i ][ 6 ] += enemies[ i ][ 3 ];
     enemies[ i ][ 4 ] += .5;
     if ( enemies[ i ][ 4 ] > 360 ){
      enemies[ i ][ 4 ] = 0;
     }
     enemies[ i ][ 1 ] = Math.sin(  Math.toRadians(
             enemies[ i ][ 4 ] ) )
             * enemies[ i ][ 5 ]
             + enemies[ i ][ 6 ];
     enemies[ i ][ 2 ] = Math.cos(  Math.toRadians(
             enemies[ i ][ 4 ] ) )
             * enemies[ i ][ 5 ]
             + enemies[ i ][ 7 ];

     if ( enemies[ i ][ 6 ] < -70 ){
      enemies[ i ][ 0 ] = 0;
     }
    }
   }

   // if there are no more active enemies then initiate new wave.
   noleft = true;
   for ( int i = 0 ; i < numenemies ; i++ ){
    if ( enemies[ i ][ 0 ] == 1 ){
     noleft = false;
    }
   }
         if ( noleft ){
          initiateattackwave();
         }

         repaint();
         try {
             Thread.sleep(16);
            }
             catch (InterruptedException e) {
             }
     }
    }
 public void update (Graphics g)
  {
  bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().height );
     bufferGraphics.setColor ( Color.white );
     bufferGraphics.drawString( "Sidescroller Attack Wave Example." , 10 , 10 );
  bufferGraphics.setColor( Color.red );
  // Draw the enemies
  for ( int i = 0 ; i < numenemies ; i++ ){
   if ( enemies[ i ][ 0 ] == 1 ){
    bufferGraphics.fillRect(  (int)enemies[ i ][ 1 ] ,
           (int)enemies[ i ][ 2 ] ,
           enemyw ,
           enemyh );
   }
  }
     g.drawImage(offscreen,0,0,this);

 }


}

3 Attack Waves for Side Scrolling Shooter Example



I created an example where 3 attack waves are shown. The code checks if there are no enemies left and then creates a new attack wave. The enemies fly through the screen from right to left.


 

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

public class ThreeAttackWavesExample001 extends Applet implements Runnable {
 Graphics bufferGraphics;
    Image offscreen;
    int numenemies = 64;
    int enemyw = 16; // enemy width
    int enemyh = 16; // enemy height
 double enemies[][] = new double[ numenemies ][ 4 ]; // active,x,y,mx

 public void init() {
  setBackground(Color.black);
     offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();
     createattackwave3();
     // Start the runnable thread.
  new Thread(this).start();
 }
    public void run() {
     for(;;) { // animation loop never ends


   if ( noenemiesareleft() == true ){
    int n = (int)( Math.random() * 4 );
    if ( n == 1 ){
     createattackwave1();
    }
    if ( n == 2 ){
     createattackwave2();
    }
    if ( n == 3 ){
     createattackwave3();
    }

   }

      moveenemies();
         repaint();
         try {
             Thread.sleep(16);
            }
             catch (InterruptedException e) {
             }
     }
    }
 public void update (Graphics g)
  {
  bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().height );
     bufferGraphics.setColor ( Color.white );
     bufferGraphics.drawString( "Sidescroller Shooter 3 attack waves Example." , 10 , 10 );

  // Draw enemies
     bufferGraphics.setColor ( Color.red );
  for ( int i = 0 ; i < numenemies ; i++ ) {
   if ( enemies[ i ][ 0 ] == 1 ){
    bufferGraphics.fillRect(  (int)enemies[ i ][ 1 ],
            (int)enemies[ i ][ 2 ],
            enemyw,
            enemyh );
   }
  }

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

 }

 public boolean noenemiesareleft(){
  boolean noleft = true;
  for ( int i = 0 ; i < numenemies ; i++ ){
   if ( enemies[ i ][ 0 ] == 1 ){
    noleft = false;
   }
  }
  return noleft;
 }

 public void createattackwave1(){
  int n;
  for ( int i = 0 ; i < 4 ; i++ ){
   n = findemptyenemy();
         //System.out.println ("slot : " + n );
   if ( n > -1 ){
    enemies[ n ][ 0 ] = 1;
    enemies[ n ][ 1 ] = getSize().width + 32 + 4 * enemyw - ( i * enemyw ) + ( enemyw * 3 );
    enemies[ n ][ 2 ] = i * ( enemyh * 1.5 ) + 32;
    enemies[ n ][ 3 ] = .5;
   }
  }
 }

 public void createattackwave2(){
  int n;
  for ( int i = 0 ; i < 4 ; i++ ){
   n = findemptyenemy();
         //System.out.println ("slot : " + n );
   if ( n > -1 ){
    enemies[ n ][ 0 ] = 1;
    enemies[ n ][ 1 ] = getSize().width + 32 + ( i * enemyw ) + ( enemyw * 3 );
    enemies[ n ][ 2 ] = i * ( enemyh * 1.5 ) + 132;
    enemies[ n ][ 3 ] = .5;
   }
  }
 }

 public void createattackwave3(){
  int n;
  for ( int i = 0 ; i < 4 ; i++ ){
   n = findemptyenemy();
         //System.out.println ("slot : " + n );
   if ( n > -1 ){
    enemies[ n ][ 0 ] = 1;
    enemies[ n ][ 1 ] = getSize().width + (int)(Math.random() * getSize().width / 2);
    enemies[ n ][ 2 ] = (int)(Math.random() * (getSize().height - 100 ) + 50);
    enemies[ n ][ 3 ] = .5;
   }
  }
 }


 public int findemptyenemy(){
  for ( int i = 0 ; i < numenemies ; i++ ){
   if ( enemies[ i ][ 0 ] == 0 ){
    return i;
   }
  }
  return -1;
 }

 public void moveenemies(){
  for ( int i = 0 ; i < numenemies ; i++ ){
   if ( enemies[ i ][ 0 ] == 1 ){
    enemies[ i ][ 1 ] -= enemies[ i ][ 3 ];
    if ( enemies[ i ][ 1 ] < 0 - enemyw ){
     enemies[ i ][ 0 ] = 0;
    }
   }
  }
 }


}

Sin Wavy Movement Example



I got the idea to program parts of a SideScrolling Shooter. I downloaded several video's of game footage for it that I will be studying. I already noticed one type of enemy ship that flew in in a wavy pattern. I recreated that in this example. It uses the Java sin command. The enemy ship starts from the right and flies to the left and does this again if it gets outside of the screen.


 

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

public class SinWavyMovementExample001 extends Applet implements Runnable {
 Graphics bufferGraphics;
    Image offscreen;

 int angle = 0;
 double blockx;
 double blocky;
 double offsety = 0;

 public void init() {
  setBackground(Color.black);
     offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();
     blockx = getSize().width;
     blocky = getSize().height / 2 - 16;
     // Start the runnable thread.
  new Thread(this).start();
 }
    public void run() {
     for(;;) { // animation loop never ends
         angle+= 3;
         if ( angle > 360 ) {
          angle = 0;
         }
         offsety = Math.sin(Math.toRadians(angle)) * 12;
   blockx -= 0.5;
   if ( blockx < -32 ){
    blockx = getSize().width;
   }
         repaint();
         try {
             Thread.sleep(16);
            }
             catch (InterruptedException e) {
             }
     }
    }
 public void update (Graphics g)
  {
  bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().height );
     bufferGraphics.setColor ( Color.white );
     bufferGraphics.drawString( "Sin Wavy Movement Example" , 10 , 10 );

  bufferGraphics.fillRect ( (int)blockx , (int)blocky + (int)offsety , 32 , 32 );

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

 }


}

dinsdag 14 februari 2012

Turrican Style Shooting Example



I have the Turrican remake on my notebook. In this game you can shoot around yourself. I remade this in Java. Shoot and then use the left and right keys to shoot around yourself.


 


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

public class TurricanShootingExample001 extends Applet implements Runnable{
 Graphics bufferGraphics;
    Image offscreen;
  boolean playerMoveRight = false;
  boolean playerMoveLeft = false;
   double playershootangle = 90-45;
   boolean shootlaser = false;
   long shootingdelay;
    double xPos=100;
    double yPos=100;
    boolean isFalling = false;
    boolean isJumping = false;
 double gravity;
 double groundLevel = 100;
 boolean isShooting;
 boolean playerFacingLeft;
 boolean playerFacingRight=true;
 int numlasers = 50;
 double lasers[][] = new double[ numlasers ][ 4 ]; // 0-active,1-x,2-y,3-angle
 long lasertimeout[] = new long[ numlasers ]; // when does the laser end

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

     // Start the runnable thread.
  new Thread(this).start();
 }
    public void run() {
     for(;;) { // animation loop never ends
         updateplayer();
         updatelasers();
         repaint();
         try {
             Thread.sleep(16);
            }
             catch (InterruptedException e) {
             }
     }
    }
    public void updatelasers(){
     if ( shootlaser == true ) {
      if ( shootingdelay < System.currentTimeMillis() ) {
       shootingdelay = System.currentTimeMillis() + 60;
       newlaser();
      }
     }
  for ( int i = 0 ; i < numlasers ; i++ ) {
   if ( lasers[ i ][ 0 ] == 1 ) {
    lasers[ i ][ 1 ] += Math.sin( Math.toRadians( 2 * lasers[ i ][ 3 ] ));
    lasers[ i ][ 2 ] += Math.cos( Math.toRadians( 2 * lasers[ i ][ 3 ] ));
    if ( lasertimeout[ i ] < System.currentTimeMillis() ) {
     lasers[ i ][ 0 ] = 0;
    }
    if ( lasers[ i ] [ 2 ] - 16 > groundLevel ) {
     lasers[ i ][ 0 ] = 0;
    }
   }
  }
    }
    public void newlaser(){
     for ( int i = 0 ; i < numlasers ; i++ ) {
      if ( lasers[ i ][ 0 ] == 0 ) {
       lasers[ i ][ 3 ] = playershootangle;
       lasers[ i ][ 0 ] = 1;
       lasers[ i ][ 1 ] =  xPos + 8
            + 10 * Math.sin( Math.toRadians( 2 * lasers[ i ][ 3 ] )) ;
       lasers[ i ][ 2 ] = yPos + 8
            + 10 * Math.cos( Math.toRadians( 2 * lasers[ i ][ 3 ] )) ;
       lasertimeout[ i ] = System.currentTimeMillis() + 1300;
    break;
      }
     }
    }
 public void updateplayer(){

   if( playerMoveRight && xPos < getSize().width - 16 && !shootlaser )
   {
    xPos++;
   }
   if( playerMoveLeft && xPos  > 0 && !shootlaser )
   {
    xPos--;
   }
   if ( playerMoveRight && shootlaser ) {
    playershootangle -= 2;
    if ( playershootangle < 0 ) {
     playershootangle = 360;
    }
   }
   if ( playerMoveLeft && shootlaser ) {
    playershootangle += 2;
    if ( playershootangle > 360 ) {
     playershootangle = 0;
    }
   }
   if( isJumping )
   {
    yPos = (int)yPos - gravity;
    gravity = gravity - .1;
    if( gravity < 0 )
    {
     isJumping = false;
     isFalling = true;
    }
   }
   if( isFalling )
   {
    yPos = (int)yPos + gravity;
    gravity = gravity + .1;
    if( yPos  >groundLevel )
    {
     isFalling = false;
     yPos = groundLevel;
     gravity  =0;
    }
   }
 }

  public boolean keyUp (Event e, int key){
    if ( key == 97 )
        {
          playerMoveLeft = false;
        }
        if ( key == 100 )
        {
          playerMoveRight = false;
        }
  if ( key == 109 ) { // m key
   shootlaser = false;
   // when the player releases the fire key then reset the shooting angle.
   if ( playerFacingLeft ) {
    playershootangle = 90+45;
   }
   if ( playerFacingRight ) {
    playershootangle = 45;
   }

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

    if ( key == 97 )
        {
         playerMoveLeft = true;
         playerFacingLeft = true;
         playerFacingRight = false;
        }
        if ( key == 100 )
        {
          playerMoveRight = true;
          playerFacingRight = true;
          playerFacingLeft = false;
        }

      if ( key == 120 ) // x character jump
      {
        if( isFalling == false && isJumping == false )
        {
            isJumping = true;
            gravity = 3;
        }
      }

  if ( key == 109 ){ // m shoot
   shootlaser = true;
  }

        System.out.println ("Charakter: " + (char)key + " Integer Value: " + key);
        return true;
  }

 public void update (Graphics g)
  {
  bufferGraphics.clearRect( 0 , 0 , getSize().width , getSize().height );
     bufferGraphics.setColor ( Color.white );
     bufferGraphics.drawString( "Turrican Shooting Example" , 10 , 10 );
     bufferGraphics.drawString( "a - left , d - right , x jump , m - shoot" , 10 , 20 );

  // draw lasers
  for ( int i = 0 ; i < numlasers ; i++ ) {
   if ( lasers[ i ][ 0 ] == 1 ) {
    bufferGraphics.fillOval( (int)lasers[ i ][ 1 ] , (int)lasers[ i ][ 2 ] , 4 , 4 );
   }
  }


     int x = (int) xPos;
     int y = (int) yPos;
     bufferGraphics.fillRect (x, y, 16, 16);



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

 }


}

stringWidth Example





stringWidth is used to see what the width of a string is that is drawn to the screen using drawString. You can easily center a string on the screen with this.

 
 


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

public class stringWidthExample001 extends Applet implements Runnable{
 Graphics bufferGraphics;
    Image offscreen;
 FontMetrics fm;

 public void init() {
  Graphics g;
  g=getGraphics();
  fm = g.getFontMetrics();
  setBackground(Color.black);
     offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();
     // Start the runnable thread.
  new Thread(this).start();
 }
    public void run() {
     for(;;) { // animation loop never ends
         repaint();
         try {
             Thread.sleep(16);
            }
             catch (InterruptedException e) {
             }
     }
    }
    public void update(Graphics g){
     String s;
     bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
        bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Stringwidth Example",10,10);
        bufferGraphics.setColor(Color.white);
        s = "This text";
  bufferGraphics.drawString(s,(this.getSize().width-fm.stringWidth(s)) / 2, 80);
        s = "is centered";
  bufferGraphics.drawString(s,(this.getSize().width-fm.stringWidth(s)) / 2, 90);
  s = "on the screen";
  bufferGraphics.drawString(s,(this.getSize().width-fm.stringWidth(s)) / 2, 100);


  // Draw the Graphics buffer
       g.drawImage(offscreen,0,0,this);
    }
}

Exclamation Boolean Example



I was reading through sourcecode and found that I was not that familiar with the shown method of using a boolean. When placing a ! before a variable you can see if it is set to false.

 


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

public class exclamationbooleanvalueExample001 extends Applet {

 // create a boolean and set its value to false
 boolean the = false;

 public void init() {
 }

 public void paint(Graphics g) {
  g.drawString( "! Example" , 10, 10 );
  g.drawString( "boolean the = false;" , 10 , 30 );
  g.drawString( "if ( !the ) { " , 10 , 40 );

  // if the is false then draw a message on the screen.
  if ( !the ) {
   g.drawString( "the value is false." , 10 , 70 );
  }

 }


}

maandag 6 februari 2012

Studying a* algorithm

I have 3 more examples for the weblog but they are still on the notepad. I am on a other computer at the moment. I do not know when I will place the examples on the weblog.

Yesterday and today I have been reading through a* sourcecode and Internet pages. At the moment I am studying a a* routine that I got from the Blitz Basic website code archives. I have been modyfying the code so that only paths through a random map are created. When I looped the program I got out of bounds errors and I fixed those. Also there is a error I think with the setting of the G cost. I am still learning how the variables are made and used and placed in the a* algorithm. The whole thing is still a mystery to me. I have little understanding of a*. I think that every a* tutorial on the Internet is badly written. Also the book that I have explaining pathfinding  routines is bad.

I think I will convert the code to Java when I have spend more time studying and modifying the routine. Or if I understand the code (not likely) I will be making my own.

A* is one important routine for turn based strategy and real time strategy games.

btw. I have also switched to the new blog interface and now the account is mixed with google+. But that account is in another language.

Edit : The code is broken. I do not know if it is becourse I modified it or if it is becourse it was broken. I will look at the code some more and see if I can fix it.