woensdag 28 december 2011

Rotating Line Example



I was preparing a swinning ball example that I once made in a one level remake of the adams family. But I had not worked with cos and sin yet. In Java the commands work with radians in stead of degrees so I needed to take that into account to. I looked up cos and sin and toTadians and quickly made this applet example. A rotating line.

 


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

public class RotatingLineExample extends Applet implements Runnable{

 Graphics bufferGraphics;
    Image offscreen;
 double x1;
 double y1;
 double x2;
 double y2;
 int angle;

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

 }

 public void rotateline(){
  x2 = Math.cos(Math.toRadians(angle)) * 100 + x1;
  y2 = Math.sin(Math.toRadians(angle)) * 100 + y1;
  angle++;
  if ( angle > 360 ) angle = 0;
 }

    public void run() {
        for(;;) { // animation loop never ends
   rotateline();
         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);
        bufferGraphics.drawString("Rotating Line Example.",10,10);
  bufferGraphics.drawLine((int)x1,(int)y1,(int)x2,(int)y2);
        g.drawImage(offscreen,0,0,this);
     }

}

Blinking Unit Example



Press the mouse on th applet to activate. Then use wsad to move the blinking unit across the applet screen.
I was preparing a turn based example but I felt it was a little bit to much to do in one time. So I decided to make a part of that example as this example. In this applet you can move a unit across the field. It stops blinking and becomes visible if you move it. It then after one second becomes invisible to become visible again after another second.

 


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

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

 int unitx;
 int unity;
 int unitsize = 16;
 boolean unitvisible = false;
 long blinktime;

 public void init() {
        setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
        bufferGraphics = offscreen.getGraphics();
  unitx = getSize().width / 2 / unitsize - 1;
  unity = getSize().height / 2 / unitsize - 1;
  new Thread(this).start();
 }

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

 public void doblinktime(){
  if ( System.currentTimeMillis() > blinktime ){
   if ( unitvisible == true ) {
    unitvisible=false;
   } else {
    unitvisible=true;
   }
   blinktime = System.currentTimeMillis() + 1000;
  }
 }

     public void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().height);
     bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Blinking Unit Example.",10,10);
        bufferGraphics.drawString("w/s/a/d to move unit.",10,220);
  if ( unitvisible == true ){
   bufferGraphics.fillRect(unitx * unitsize , unity * unitsize , unitsize , unitsize);
  }
        g.drawImage(offscreen,0,0,this);
     }

 public void resetblink(){
   blinktime = System.currentTimeMillis() + 1000;
   unitvisible = true;
 }


 public boolean keyUp (Event e, int key){
    if( key == 119 ) // w key
        {
         if( unity > 1 ) unity--;
         resetblink();
        }
        if( key == 97 ) // a key
        {
         if( unitx > 1 ) unitx--;
         resetblink();
        }
        if( key == 100 ) // d key
        {
         if( unitx < 18 ) unitx++;
         resetblink();
        }
        if( key == 115 ) // s key
        {
         if( unity < 12 ) unity++;
         resetblink();
        }

  return true;
 }

}

zondag 25 december 2011

ScrollText Example



A simple Scrolltext Example made in Java. The sourcecode is below. I can see some flickering by the scrolltext and I have no idea what causes it. I am a little rusty with Java.


 


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

public class ScrollTextExample extends Applet implements Runnable{

 Graphics bufferGraphics;
    Graphics scrolltextGraphics;
    Image offscreen;
 Image scrolltextimage;
 String message = "This is a scroll text message made in java. There is not much to report...      ";
 int scrolltextloc =0;
 int scrolltextoffset = 0;


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

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

    public void scrolltext(){
  scrolltextoffset--;
  if ( scrolltextoffset < 0 ) {
   scrolltextoffset = 12;
   scrolltextloc++;
   if ( scrolltextloc > message.length()-1) scrolltextloc = 0;
  }
     scrolltextGraphics.clearRect(0,0,getSize().width+12,12);
  scrolltextGraphics.setColor(Color.white);
  int stloc = scrolltextloc;
  String temp;
  for ( int i = 0 ; i < 30 ; i++)
  {
   temp = message.substring(stloc,stloc+1);
   scrolltextGraphics.drawString(temp,i*12+scrolltextoffset,10);
   stloc++;
   if(stloc > message.length()-1) stloc = 0;
  }

    }

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

  //scrolltextGraphics.clearRect(0,0,getSize().width+12,12);
  //scrolltextGraphics.setColor(Color.white);
  //scrolltextGraphics.drawString(message,0,10);




  bufferGraphics.drawImage(scrolltextimage,0,100,this);

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

}

Fog of War Example



Fog of War in games is hidden terrain and usually covered with black tiles. The player moves his unit across the map and the map around him gets visible. I programmed this a couple of times however I did not include the nice border finishings. I recreated the fog of war in Java this time. In the code the map is drawn if the fogmap on the draw position has a value of 1. It starts out as 0. I use a brush array to draw ontop of the player position that sets the fogmap to value 1.
Use r to reset the map and wsad keys to move.

I will see if I can program another couple of examples. I wanted to do a scrolltext example and am thinking about it tonight. I also had a turn based example planned and a bomberman example and a swinging bal platformer example. But I have no idea if these will be finished this weekend.

Well Merry christmas from me and if I do not post anymore till next year a happy new year.

 


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


public class FogOfWarExample extends Applet implements Runnable {

 Random r = new Random();
 Graphics bufferGraphics;
    Image offscreen;
    Image image2;
    Image image3;
    Image image4;

 private short fogbrush[][]={
 {0,0,1,0,0},
 {0,1,1,1,0},
 {1,1,1,1,1},
 {0,1,1,1,0},
 {0,0,1,0,0},
 };

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

 private short ground1[][]={
       {4,4,4,4,5,5,4,4,4,4,4,4,5,4,4,4},
       {4,5,4,4,4,4,4,4,4,4,4,4,5,5,4,4},
       {4,4,5,4,4,4,4,4,4,4,4,4,5,5,4,4},
       {4,4,5,5,5,5,4,4,4,4,4,4,4,5,5,4},
       {4,4,5,5,5,5,4,4,4,5,4,4,4,4,5,4},
       {5,4,4,4,5,5,4,4,4,5,4,4,4,4,4,5},
       {5,4,4,4,4,4,4,4,4,5,4,4,4,4,4,4},
       {4,4,4,4,5,4,4,4,4,5,5,4,4,4,4,4},
       {4,4,4,4,4,5,4,4,4,5,5,5,4,4,4,4},
       {4,4,4,4,4,4,4,4,4,5,5,5,5,4,4,4},
       {4,4,5,4,4,4,4,4,4,4,5,5,5,4,4,4},
       {4,4,5,5,4,4,4,4,4,4,5,5,5,5,4,4},
       {4,5,5,5,4,4,4,4,5,4,4,5,5,5,4,4},
       {4,5,5,5,5,5,4,4,5,5,4,4,5,5,4,4},
       {4,4,4,4,5,5,5,4,4,4,4,4,4,5,4,4},
       {4,4,4,4,4,5,5,5,4,4,4,4,5,4,4,4},
       };

 int playerx;
 int playery;
 short[][] fogmap = new short[16][10];

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

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

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

  playerx = 16 / 2;
  playery = 10 / 2;
  unfog();
  new Thread(this).start();

     }

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

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

     public void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().height);
     bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Fog of War Example",10,10);
        bufferGraphics.drawString("Use w/s/a/d to move - r to reset",10,230);
        for( int y = 0 ; y < 10 ; y++){
         for( int x = 0 ; x < 16 ; x++){
          if (fogmap[x][y] == 1 ) {
           bufferGraphics.drawImage(image4,32+x*16,32+y*16,this);
          }
         }
        }

    int r1 = 0;
        for( int y = 0 ; y < 10 ; y++){
         for( int x = 0 ; x < 16 ; x++){
    if (map[y][x] == 1 && fogmap[x][y] == 1 ) {
       bufferGraphics.drawImage(image2,32+x*16,32+y*16,this);
      }
      if (map[y][x] == 2 && fogmap[x][y] == 1 ){
       bufferGraphics.drawImage(image3,32+x*16,32+y*16,this);
      }
         }
        }
      bufferGraphics.setColor(Color.white);
        bufferGraphics.fillRect(playerx * 16 , playery * 16 , 16 , 16);

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

  public boolean keyDown (Event e, int key){

    if( key == 119 ) // w key
        {
        }
        if(key == 97) // a key
        {
        }
        if( key == 100 ) // d key
        {
        }
        if( key == 115 ) // s key
        {
        }


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

   return true;
  }

 public void unfog(){
  for ( int y = 0 ; y < 5 ; y++){
  for ( int x = 0 ; x < 5 ; x++){
  if (fogbrush[x][y] == 1){
   if ( playerx - x > -1 && playerx - x < 16 ){
   if ( playery - y > -1 && playery - y < 10 ){
    fogmap[playerx - x][playery - y] = 1;
   }
   }
  }
  }
  }
 }

 public boolean keyUp (Event e, int key){
    if( key == 119 ) // w key
        {
         if( playery > 2 ) playery--;
         unfog();
        }
        if( key == 97 ) // a key
        {
         if( playerx > 2 ) playerx--;
         unfog();
        }
        if( key == 100 ) // d key
        {
         if( playerx < 17 ) playerx++;
         unfog();
        }
        if( key == 115 ) // s key
        {
         if( playery < 11 ) playery++;
   unfog();
        }
  if ( key == 114 ) // r key
  {
   // reset the fogmap
   for(int y = 0 ; y < 10 ; y++)
   {
    for ( int x = 0 ; x < 16 ; x++)
    {
     fogmap[x][y] = 0;
    }
   }
   unfog();
  }

  return true;
 }


 }



donderdag 22 december 2011

This weekend - and steam sale

I think I will be programming this weekend. I think I will make the fog of war example and maybe other things that I have had on a list.

I have been playing a lot of games recently. There is a steam end of year things sale and I bought the quake pack and the Unreal pack and the game gta san andreas. I have been playing some quake 1 online and was totally humiliated. I could stay alive for a couple of seconds before I was fragged. I played quake 1 in the 90's and got pretty good at it but now I play on a notepad and this is harder.

I also have been thinking on a game example while I was lying on my bed. The game Rampage, I want to keep the example simple and short so I have been going though the features.

I am cooking at the moment. The patatoes are not done yet. I am eating peas, goulash and patatoes. This is what I eat a lot. It is cheap and done pretty quickly. I am saving money for next december so I can buy a new notepad. This time I will buy a more expensive model. I think I will pay up to 800 euro's for a notepad then. As long as it has a big hard disk.

zondag 4 december 2011

Not that much activity

I have not been coding a lot the last couple of weeks. I made something in another language but that was about it. I do think about what I want to code but never sit and make something. I have been looking through the Java ebook I bought to learn things.
Some ideas that I had I have written down for if I feel like coding again. I have been playing games and surfing news and programming sites. I wonder if I will get that motivation to code again. I have been sleeping a lot the last months to.

Starfield example



I was looking through sourcecode and found code that made a starfield. It was using the build in java points. I copied / retyped the code from the source and made my own starfield example. It is only a one speed per star example.

 


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

public class StarField01 extends Applet implements Runnable{

 int         numStars;
 Point[]     stars;
 Graphics     bufferGraphics;
    Image      offscreen;


 public void init() {
  setBackground(Color.black);
     numStars = 100;
     stars = new Point[numStars];
     for (int i = 0; i < numStars; i++)
       stars[i] = new Point((int) (Math.random() * getSize().width), (int) (Math.random() * getSize().height));
    offscreen = createImage(getSize().width,getSize().height);
     bufferGraphics = offscreen.getGraphics();
  new Thread(this).start();

 }

    public void run() {
     for(;;) { // animation loop never ends
         repaint();
         try {
                for (int i = 0; i < numStars; i++)
                {
                 stars[i].x -= 1;
                 if (stars[i].x < 0) stars[i].x = getSize().width;
                }

             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("Starfield Example.",20,30);
      bufferGraphics.setColor(Color.white);
      for (int i = 0; i < numStars; i++)
        bufferGraphics.drawLine(stars[i].x, stars[i].y, stars[i].x, stars[i].y);

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


}

MathRandom


Math.Random() gives you a random floating point number. If you place (int) before it then it rounds the number to a integer value. To get a number above one then you must multiply the Math.Random() times a value.

 


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

public class MathRandom extends Applet {

 public void init() {
 }

 public void paint(Graphics g) {

  g.drawString("Math random example",50,40);
  g.drawString("(int)(Math.Random() * 100) = "+(int)(Math.random()*100), 50, 60 );

 }
}


CurrentTimeMilliseconds


I could not remember it so I decided to make a little example of it. currentTimeMillis() returns the milliseconds time to you. If I remember it correctly it gives the time passed since 1972 or so.


 


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

public class CurrentTimeMilliseconds01 extends Applet {

 public void init() {
 }

 public void paint(Graphics g) {
  g.drawString("Getting the milliseconds.",20,40);
  g.drawString("System.currentTimeMillis() : " + System.currentTimeMillis(), 20, 60 );

 }
}

maandag 31 oktober 2011

Bought a Java Book - A natural introduction to Computer programming with Java

Last night I bought a digital Java book. It was only 8 euro's and I could pay directly through internet. I have been looking through the book and I see that it is a beginners book. There are over 600 pages of material. I already looked through the Array chapter but it did not have the multidimensional array setup covered. Though I will be using this book for studying. I still copy and paste and modify older sourcecode to make new things. So I need to learn how to code from nothing up to the end result. The last thing that I went through was classes. I worked with Types in Blitz Basic and I thought that classes replace this. Still I need to be certain that the applets work on this blog so I have some experimenting to do.
Edit: I found a older post from the blog here where I used linked lists to store classes into. Classes do replace types. You can even add functions to classes. There is a different class compiled for each class declared in the sourcecode.

Last night I also started working on a new example for my blog. A fog of war example. I am making a scrolling map where a oval is centered in the middle of the map that can be moved through the map. The fog of war is something that I programmed a number of times already. The map is covered in black and a brush in a array draws in a hidden_tiles_array where the oval is and sets the tile values to unhidden. This way when you move through the map the map becomes visible. The map draw routine draws the tiles and checks if it can draw a tile if the flag is set to unhidden. An extra map array is needed. I have selected a older example from my blog as a base for this fog of war example. I am hoping to finish the fog of war example today though I may do different things. But I placed the fog of war example on my to do list for the weblog.

zondag 30 oktober 2011

Been studying a* pathfinding code

In the Blitzbasic language there was this pathfinding code on the archives. I have been studying the code. I retyped a part in blitz basic changing variable names and got it working. But when I placed the routine in a function the code stopped working. I could not figure out what the problem was. I then copy pasted from the original and recreated the pathfinding program.
I still practically do not understand how a pathfinding routine works. It is something like the floodfill pathfinding that I have on this blog that I made myself but different. I have converted a pathfinding program into the delphi language years ago to learn how it works but to no avail. It is so difficult to understand. But I read in my ai book that you should study source code multiple times and even on paper to learn how it works.
I was planning on converting the Blitz Basic pathfinding program into Java but I have no idea when I will do this. This pathfinding routine does not check terrain difficulty only blocked and open. But it is a pretty solid version that might turn into a nice applet to try it with.
I have an other pathfinding code in Blitz Basic that does use more than blocked values in pathplanning but it uses types and I have not enough programming experience to figure out how to convert that to Java. I really should buy a good book on Java to learn more about it. But the Blitz Basic language with the confusing Type code has little books for it. It is not that widely used.

The example I have been working with is made for Rogue type games. I modified the new version to only show random paths on bigger maps. I created random obstacles on the map each time you press the mouse on the screen. I have been thinking on how I would convert the code to Java since the code is in Blitz and uses strings to store the path into. I have not really worked with Strings with Java but I found on the Internet that Substring replaces the Mid function in Basic. The other parts of the code uses Multi Dimensional Arrays and other variables. I think I will be able to convert the code to Java with not to many difficulties. But one thing troubles me and that is the fact that I have no idea how I got that out of bound error while retyping the code. I see nothing in the code that prevents this and still no out of bounds error occurs in the original. I did notice that the map is larger then what is being used in the code so that must be a part of it, I have learned most of my programming by copying succesfull code so I must learn this thing to.

I also have been thinking of what to do with the pathfinding code. Shall I turn it into a game of some sorts. But I have no clues of what to add into the game. I really should select a type of game a clone it. Maybe a small Rogue type game would be neat but they are not my favourite types of games.

Well I barely program anymore but I did get stuff done this weekend.

dinsdag 4 oktober 2011

Trying to learn a* (again)

For years I have not been able to understand how the a* pathfinding thing works. A couple of weeks ago I memorized the g h and f variables. F is the combined g and h values. G is the movement cost and H is the estimated movement cost. Memorizing these got me a little further in understanding the a* algorithm. But I still have a long way to go to fully understand it.

For instance. How do you expand the search area. I can not understand this yet.

I wish there were more articles on a* for people who do not understand the algorithm. I have under 10 different source codes of a* but what the codes do is still a mystery.

Update :
I learned that the h cost is the distance to the end position. And that g is the distance from the start position. I think that anyways after studying the examples I found. Still I am still stuck on how to find that path itself. I have no idea on how to do that. I plan to study sourcecode and I hope to learn something on that.

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.

zondag 21 augustus 2011

String Compare - Comparing Strings - equals





I needed this to code the Sim City road connection example. I could not get the == thing comaring working so I looked up how to compare strings with each other. This is done with equals. That worked for me. Below you can see how it is done.


 


/**
 * @(#)StringCompare01.java
 *
 * StringCompare01 Applet application
 *
 * @author
 * @version 1.00 2011/7/21
 */

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

public class StringCompare01 extends Applet {

	public void init() {
	}

	public void paint(Graphics g) {

		String banana = "";

		for ( int i = 0 ; i < 9 ; i++){
			banana = banana + "0";
		}
		System.out.println(banana);
		if ( banana.equals("000000000")) {
			g.drawString("String equals 000000000", 50, 90 );
		}

		g.drawString("Welcome to Java!!", 50, 60 );

	}
}

Sim City Road System - Auto Connect roads





I learned to do this in the 90's. I first saw it I think in Sim City. Where you lay roads and they automatically change to connect with each other. It is a magical thing when you see it. It is also used in games like Civilization where roads and railroads connect with each other.

Press the mouse in the applet to see the new road and click near the last road to connect the roads together.



 


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


public class SimCityRoadSystem01 extends Applet {
	Random	r = new Random();
	Graphics bufferGraphics;
    Image offscreen;
	short[][] map = new short[20][15];
	short[][] roadmap = new short[20][15];
	BufferedImage roads[] = new BufferedImage[17];


	private short road[][][]={
							// road 0 - connects to all
							{
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0}
							},

							// road 1 - connects to all
							{
							{0,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,1,1,1,1,0,0},
							{0,0,1,1,1,1,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,0}
							},
							// road 2 - connects top to center
							{
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,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,0}
							},
							// road 3 - connects right to center
							{
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0},
							{0,0,0,1,1,1,1,1},
							{0,0,1,1,1,1,1,1},
							{0,0,1,1,1,1,1,1},
							{0,0,0,1,1,1,1,1},
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0}
							},
							// road 4 - connects bottom to center
							{
							{0,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,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0}
							},
							// road 5 - connects left to center
							{
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0},
							{1,1,1,1,1,0,0,0},
							{1,1,1,1,1,1,0,0},
							{1,1,1,1,1,1,0,0},
							{1,1,1,1,1,0,0,0},
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0}
							},
							// road 6 - connects top and right to center
							{
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,1,1},
							{0,0,1,1,1,1,1,1},
							{0,0,1,1,1,1,1,1},
							{0,0,0,1,1,1,1,1},
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0}
							},
							// road 7 - connects top and bottom to center
							{
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0}
							},
							// road 8 - connects top and left to center
							{
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{1,1,1,1,1,1,0,0},
							{1,1,1,1,1,1,0,0},
							{1,1,1,1,1,1,0,0},
							{1,1,1,1,1,0,0,0},
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0}
							},
							// road 9 - connects top and bottom and right to center
							{
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,1,1},
							{0,0,1,1,1,1,1,1},
							{0,0,1,1,1,1,1,1},
							{0,0,1,1,1,1,1,1},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0}
							},
							// road 10- connects top and bottom and left to center
							{
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{1,1,1,1,1,1,0,0},
							{1,1,1,1,1,1,0,0},
							{1,1,1,1,1,1,0,0},
							{1,1,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0}
							},
							// road 11 - connects right and bottom to center
							{
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0},
							{0,0,0,1,1,1,1,1},
							{0,0,1,1,1,1,1,1},
							{0,0,1,1,1,1,1,1},
							{0,0,1,1,1,1,1,1},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0}
							},
							// road 12 - connects left and right to center
							{
							{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,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}
							},
							// road 13 - connects left and bottom to center
							{
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0},
							{1,1,1,1,1,0,0,0},
							{1,1,1,1,1,1,0,0},
							{1,1,1,1,1,1,0,0},
							{1,1,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0}
							},
							// road 14 - connects left and bottom to center
							{
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{1,1,1,1,1,1,1,1},
							{1,1,1,1,1,1,1,1},
							{1,1,1,1,1,1,1,1},
							{1,1,1,1,1,1,1,1},
							{0,0,0,0,0,0,0,0},
							{0,0,0,0,0,0,0,0}
							},
							// road 15 - connects left and bottom to center
							{
							{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,1,1,1},
							{1,1,1,1,1,1,1,1},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0}
							},
							// road 16 - connects top and left and bottom to center
							{
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0},
							{1,1,1,1,1,1,1,1},
							{1,1,1,1,1,1,1,1},
							{1,1,1,1,1,1,1,1},
							{1,1,1,1,1,1,1,1},
							{0,0,1,1,1,1,0,0},
							{0,0,1,1,1,1,0,0}
							},

	};

    public void init(){
        Graphics2D g;

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

		for ( int i = 0 ; i < 17 ; i++){
			roads[i] = new BufferedImage(16,16,BufferedImage.TYPE_INT_ARGB);
 			g = roads[i].createGraphics();
			for ( int y = 0 ; y < 8 ; y++){
				for ( int x = 0 ; x < 8 ; x++){
					if (road[i][x][y] == 1) {
						g.setColor(new Color(150,150,150,255));
					}
					if (road[i][x][y] == 0) {
							g.setColor(new Color(0,0,0,0));
					}
					g.fillRect(y*2,x*2,2,2);
				}
			}
		}

     }

	public boolean doroadmap( int x , int y ){
		if ( x < 0 ) return false;
		if ( x > 19 ) return false;
		if ( y < 0 ) return false;
		if ( y > 14 ) return false;
		String roadsit;

		roadsit = "";

		if ( roadmap[x][y] == 1 ) roadsit=roadsit + "1";

		if ( y - 1 < 0 ) {
			roadsit = roadsit + "0";
		}else{
			if ( roadmap[x][y-1] == 1 ) {
				roadsit=roadsit + "1";
			}else{
				roadsit=roadsit + "0";
			}
		}
		if ( x - 1 < 0 ) {
			roadsit = roadsit + "0";
		}else{
			if ( roadmap[x - 1][y] == 1 ){
				roadsit = roadsit + "1";
			}else{
				roadsit = roadsit + "0";
			}
		}
		if ( x + 1 > 19 ) {
			roadsit = roadsit + "0";
		}else{
			if ( roadmap[ x + 1 ][ y ] == 1 ){
				roadsit = roadsit + "1";
			}else{
				roadsit = roadsit + "0";
				}
		}
		if ( y + 1 > 14 ){
			roadsit = roadsit + "0";
		}else{
			if ( roadmap[x][y+1] == 1 ){
				roadsit = roadsit + "1";
			}else{
				roadsit = roadsit + "0";
			}
		}

		if ( roadsit.equals("10000") ) map[x][y] = 1;
		if ( roadsit.equals("11000") ) map[x][y] = 2;
		if ( roadsit.equals("10010") ) map[x][y] = 3;
		if ( roadsit.equals("10001") ) map[x][y] = 4;
		if ( roadsit.equals("10100") ) map[x][y] = 5;
		if ( roadsit.equals("11010") ) map[x][y] = 6;
		if ( roadsit.equals("11001") ) map[x][y] = 7;
		if ( roadsit.equals("11100") ) map[x][y] = 8;
		if ( roadsit.equals("11011") ) map[x][y] = 9;
		if ( roadsit.equals("11101") ) map[x][y] = 10;
		if ( roadsit.equals("10011") ) map[x][y] = 11;
		if ( roadsit.equals("10110") ) map[x][y] = 12;
		if ( roadsit.equals("10101") ) map[x][y] = 13;
		if ( roadsit.equals("11110") ) map[x][y] = 14;
		if ( roadsit.equals("10111") ) map[x][y] = 15;
		if ( roadsit.equals("11111") ) map[x][y] = 16;



		return true;

	}

 	public boolean mouseDown (Event e, int x, int y) {
        if (e.modifiers == Event.META_MASK) {
        //    info=("Right Button Pressed");
        } else if (e.modifiers == Event.ALT_MASK) {
        //    info=("Middle Button Pressed");
        } else {
        //   info=("Left Button Pressed");
//        	System.out.println( "left mouse pressed ");
        	roadmap[ x / 16 ][ y / 16 ] = 1;
        	for ( int y1 = -1 ; y1 <=1 ; y1++ ){
        		for ( int x1 = -1 ; x1 <=1 ; x1++ ) {
            		doroadmap ( ( x / 16 ) + x1  ,  ( y / 16 ) + y1  );
        		}
        	}
        }
        repaint();
        return true;
    }
	public void paint(Graphics g){
    	bufferGraphics.clearRect(0,0,getSize().width,getSize().height);
    	bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Sim City Road System.",10,10);
		bufferGraphics.drawString("Press the mouse on the applet ",10,20);
     	bufferGraphics.drawString("to lay roads.",10,30);


		for ( int x = 0 ; x < 20 ; x++ ) {
			for ( int y = 0 ; y < 15 ; y++ ) {
				bufferGraphics.drawImage( roads[map[ x ][ y ]] , x * 16 , y * 16 , this );
			}
		}


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

     }
    public void update(Graphics g){
          paint(g);
     }
 }



Gaming and surfing.

I a'm playing games and not doing any programming. I also spend a lot of time surfing the Internet. I have almost 1500 visits to my weblog here since I started. This is quite a lot. Certain code gets a good number of hits.

I have a couple of posts ready to be published. One is a Sim City road thing. In that applet roads automatically connect to the neighbouring road. I learned to do this in the 90's. It is one of the best things I can do. I am not that great a coder.

I do not know when I will start programming again and when I will add those last code posts to the blog. Maybe I will do it next.

The games I have bought with Steam and am playing are not that good. The notebook has a touchpad and it is buggy in games. Civilization 3 was the most adictive game I played in a long time. I can program a couple of features from that game myself. I like spending time figuring out how certain things can be programmed from the games that I play a lot.
At the moment I am listening to music from the Mod archive. I downloaded gigabytes of mod music and am compiling a list of the best music which I will probably blog sometimes when it is big enough. There is not a lot of good music in the gigabytes of music. But sometimes there is a good song to be heared. Some of my own music is in the Mod archive. I already heared one song while listening to the collection in random.

I bought this book on artificial intelligence and am sorry I spend money on it. I sometimes check it and have learned something though. How to correct paths with the pathfinding thing.

I do spend time thinking of programming. I have a 4 thread computer and am not able to find a good source of multithreaded programming programming information. The dual core 2.66 ghz processor I have is fast and I  do not read anything on how to use the speed to the max. I use one thread of the 4 available. This is bad. In a couple of years I will probably have a 4 or 6 core processor so I wonder why I have such a hard time finding multithread examples or a new language that has this automatically done.
Java is the most used language on the planet according to certain sources so there is more on the web about it I guess. I am dissapointed that there is so little good and cool examples and tutorials on how to program things from games. I really would like to program my own Civilization and Command and Conquer and other things. But the Internet is only 20 years old and such and things are hard to find.

Java 7 is out and is broken I read. The loops are not working as they should. I read I should wait a couple of months until the bugs are repaired. What I can find on Java 7 is not that much. Nothing about speed increases of multicore things. Still I will upgrade when the problems are repaired.

I follow a few things on the Internet. One is the P Project. This is a blog about remaking Powermonger. I got the idea to buy that Artificial Intelligence book from that Blog. There is not that much activity in programming on that blog. But I do follow it. Though the graphical direction it is going into is discouraging. I was hoping for a remake of Powermonger with as much as layout and style alikeness as possible.

woensdag 22 juni 2011

Not programming for a while

I bought a new notepad. It is a Intel Core i5-480 with 4 gb of ram with Intel HD Graphics. I had a old Steam Account and Halflife 2 ran good on the new notepad. Since Steam has a option that allows me to buy directly from steam I bought a set of games. I have been playing these.

I also installed the Java sdk and Jbuilder but have not really programmed anything yet. I was working on an platformer Ice Sliding example but I have been playing games to much to finish it.

The notepad has a problem though. The cursor keys sometimes do not respond if you keep them pressed in. I have been back to the store but they could not notice it. They said to reinstall the windows and to try a different keyboard to see if that has the same behaviour.

I do read the Java gaming forum daily and check the statistics for this blog. At the moment the isometric example has 18 views. The pathfinding example also usually has a good hit count.

My copy of the book Game ai by Example has come in. I had to mail the bookstore since it took ocer a month to get a link to a download. The book is bad. It is short and not that well written. It has math that I do not understand. I wish there were better books but I have not seen any.

I am drinking a beer at the moment and still have some housekeeping to do. I also am going to get some food.  Then I will probably surf the Internet for a while and play a couple of games. I also bought Civilization 3 again on steam this time and will maybe play it. I have a game where I am stuck on a desert section of a map. I remember that you can finish the game without fighting. I did have to pay money to a other player once.
I programmed a bunch of Turn based game things and have a bigger turn based game that I wrote. I will write Turn Based game examples for my blog. Civilization 2 is my all time favourite game but they do not sell it online as a download for as far as I can find. I lost my old copy. I am certainly going to buy it again if I find it anywhere.

I am also looking for other games to buy and have used Youtube to view game play footage of games that looked interesting. So far a space strategy game and a Halflife mod looked like a buy.

I do think of programming when I am in my bed. I was thinking of how to make bots but it seems difficult.

Well I am going to finish my beer and will go get some food and start kooking.

zondag 5 juni 2011

Platformer Bump Head into Tiles Example.



I made this example this weekend. In the game mario you can bump your head into tiles and they will spawn presents out of them. Above in the applet I made something like that. Click in the applet to activate it and use the cursor keys and space to move around in the map. Use space to bump into the yellow tiles and after a number of times a presents will be put ontop of it.

 


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

public class PlatformerHitTiles01 extends Applet implements Runnable {
 // Graphics for double buffering.
 Graphics    bufferGraphics;
    Image     offscreen;
 private short map[][]={
      {1,1,1,1,1,1,1,1,1,1,1,1,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,2,0,0,1},
      {1,0,0,0,0,0,0,0,0,2,0,0,1},
      {1,0,0,0,0,0,0,0,0,2,0,0,1},
      {1,0,0,0,0,0,0,0,0,2,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,2,0,0,1},
      {1,0,0,0,0,0,0,0,0,2,0,0,1},
      {1,0,0,0,0,0,0,0,0,2,0,0,1},
      {1,0,0,0,0,0,0,0,0,2,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,0,0,0,0,0,0,0,0,0,0,0,1},
      {1,1,1,1,1,1,1,1,1,1,1,1,1}
      };
 int     mapwidth =    20;
 int      mapheight =   13;
 int      cellwidth =   16;
 int      cellheight =   16;
 double     px =     200;
 double    py =    100;
 int     pwidth =    cellwidth/2;
 int     pheight =    cellheight;
 boolean    isjumping =   false;
 boolean    isfalling =   false;
 double    gravity =    0;
 boolean    ismovingright =  false;
 boolean    ismovingleft =   false;
 double    jumpforce =   3;
 short    numhitblocks =  32;
 double[][]   hitblocks =   new double[numhitblocks][9];    // 0 - active , 1 - x
                   // 2 - y , 3 - type
                   // 4 - numhits(how many times to jump into for present)
                   // 5 - jump , 6 - jumpgravity , 7 - falling
                   // 8 - incy;
 int     currenthitblock =  0;
 int     numpresents =   32;
 double[][]   presents =    new double[numpresents][9];  // 0 - active , 1 - x
                   // 2 - y

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

 }

 public void initmap(){
  // initialize the hit tiles (the blocks you jump into)
  int counter = 0;
  for ( int y = 0 ; y < mapheight ; y++ ){
   for ( int x = 0 ; x < mapwidth ; x++ ){
    if ( map[x][y] == 2 ){
     hitblocks[counter][0] = 1;
     hitblocks[counter][1] = x * cellwidth;
     hitblocks[counter][2] = y * cellheight;
     hitblocks[counter][3] = 0;
     hitblocks[counter][4] = 3;
     counter++;
    }
   }
  }
 }

 public void paint(Graphics g) {
 }

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

    public void updateplayer(){

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

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

  if ( isjumping == true && isfalling == false ){
   for ( int i = 0 ; i < gravity ; i++){
    if ( mapcollision ( (int)px , (int)(py - 1) , pwidth , pheight ) == false ){
     if ( hitblockcollision ( (int)px , (int)(py - 1) , pwidth , pheight ) == false ){
      py -= 1;
     }else{
      gravity = 0;
      isfalling = true;
      isjumping = false;
      hitblocks[currenthitblock][4] -= 1;
      hitblocks[currenthitblock][5] = 1;
      if( hitblocks[currenthitblock][4] < 0 ){
       hitblocks[currenthitblock][0] = 0;
       map[ (int)hitblocks[currenthitblock][1] / cellwidth ][ (int)hitblocks[currenthitblock][2] / cellheight ] = 1;
       newpresent((int)hitblocks[currenthitblock][1] / cellwidth , ((int)hitblocks[currenthitblock][2] / cellheight ) - 1);
      }
     }
     //System.out.print("still jumping : " + gravity);
    }else{
     gravity = 0;
     isfalling = true;
     isjumping = false;
    }

   }
   if( gravity < 1 ) {
    gravity = 0;
    isfalling = true;
    isjumping = false;
   }
   gravity -= .1;
  }



    }

 public void updatepresents(){
  for( int i = 0 ; i < numpresents ; i++ ){
   if( presents[i][0] == 1 ){
    Rectangle rec1 = new Rectangle( (int)px,
            (int)py,
            pwidth,
            pheight);
    Rectangle rec2 = new Rectangle( (int)presents[i][1],
            (int)presents[i][2],
            cellwidth,
            cellheight);
    if (rec1.intersects( rec2 )){
     presents[i][0] = 0;
    }
   }
  }
 }

 public boolean hitblockcollision( int x , int y , int width , int height ){
  for ( int i = 0 ; i < numhitblocks ; i++ ){
   if( hitblocks[i][0] == 1 ){
    Rectangle rec1 = new Rectangle( x , y , width , height );
    Rectangle rec2 = new Rectangle( (int)hitblocks[i][1],
            (int)hitblocks[i][2],
            cellwidth,
            cellheight);
    if( rec1.intersects( rec2 )){
     currenthitblock = i;
     return true;
    }
   }
  }
  return false;
 }

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

 public boolean newpresent(int x, int y){
  for( int i = 0 ; i < numpresents ; i++){
   if ( presents[i][0] == 0 ) {
    presents[i][1] = x * cellwidth;
    presents[i][2] = y * cellheight;
    presents[i][0] = 1;
    return true;
   }
  }
  return false;
 }

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

 public void updatehitblocks(){
  for ( int i = 0 ; i < numhitblocks ; i++ ){
   if( hitblocks[i][0] == 1 ){
    if( hitblocks[i][5] == 1 ){
     hitblocks[i][6] -= hitblocks[i][8];
     hitblocks[i][8] += .1;
     if( hitblocks[i][6] < -6 ) {
      hitblocks[i][5] = 0;
      hitblocks[i][7] = 1;
      hitblocks[i][8] = 0;
     }
    }
    if( hitblocks[i][7] == 1 ){
     hitblocks[i][6] += hitblocks[i][8];
     hitblocks[i][8] += .1;
     if( hitblocks[i][6] > 0 ){
      hitblocks[i][6] = 0;
      hitblocks[i][7] = 0;
      hitblocks[i][8] = 0;
     }
    }
   }
  }
 }

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

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

  // Draw hitblocks
  bufferGraphics.setColor( new Color(255,255,0));
  for ( int i = 0 ; i < numhitblocks ; i++ ){
   if( hitblocks[i][0] == 1 ) {
    bufferGraphics.fillRect(  (int)hitblocks[i][1],
           (int)hitblocks[i][2] + (int)hitblocks[i][6] ,
           cellwidth,
           cellheight);
   }
  }

  // Draw presents
  bufferGraphics.setColor( new Color(0,255,0));
  for ( int i = 0 ; i < numpresents ; i++){
   if ( presents[i][0] == 1 ){
    bufferGraphics.fillOval( (int)presents[i][1],
           (int)presents[i][2],
           cellwidth,
           cellheight);
   }
  }
        // Draw player
        bufferGraphics.setColor(Color.red);
        bufferGraphics.fillRect( (int)px , (int)py , pwidth , pheight );

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

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

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

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

   return true;
  }

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

  return true;
 }

}


maandag 30 mei 2011

RTS Selecting Units #1 - Mouse Selecting rectangle


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

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

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

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

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

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

 }

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

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

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

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

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

        return true;
    }

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

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


}

maandag 23 mei 2011

Platformer Spikes Example


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

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

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

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

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

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

 }

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

 public void paint(Graphics g) {
 }

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

    public void updateplayer(){

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

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

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



    }

  public void updatespikes(){

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

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

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

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

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

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

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



   }
  }
 return false;
 }

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

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

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

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

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

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

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

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

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

   return true;
  }

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

  return true;
 }

}

donderdag 19 mei 2011

Drawing Tiles ontop of Tiles with Transparancy mask Example


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


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


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

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

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

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

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


     }


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

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

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


Data Sprites in Java (array holding sprite data)


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

 


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

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

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

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


maandag 16 mei 2011

Bought the ebook Game ai by Example.

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

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

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

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

Platformer Moveable Blocks/Tiles Example



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

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

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

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

 }

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

 public void paint(Graphics g) {
 }

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

    public void updateplayer(){

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

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

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

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



    }

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

 }

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


  return false;
 }

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

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

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

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

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

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

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

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

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

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

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

   return true;
  }

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

  return true;
 }

}