zondag 4 december 2011

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

}