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);

 }


}