Thursday, September 27, 2012

Character animation

   I was not happy with the animation of Bob in my Crazy Bob Escapes game but I did not want to mess around with the code till I knew exactly what would work. I decided to make a test animation. A simple black line that rotated around like a clock hand ( in the picture you see to that are moving in sync. I added the second just for the fun of it).
  Making the images was rather simple using paint.net ( a free drawing program). I then started by making three classes
Clock ( to set up the frame and start the rest of the code)
Draw ( to do the actual paint the images in the panel)
Pics. (To hold the image info).

Clock class is pretty much as I originally wrote it except for adding bg.myPics() and bg.cycle(). Which gave me fits and had me pulling my hair out because the program was not drawing the images. Finally I remembered that not only do you have to instantiate an object but you also have to call it to run. One of the distractions that kept me from seeing the problem  was also trying to create an Array of images and my thinking was that the problem was that  it was not accessing the array correctly to display the pictures. So I rewrote it and assigned each image to a variable ( something I knew how to do).

Draw class was just to handle the drawing on a panel originally but I decided to combine the picture images and painting in to one class.

I learned a few things , when testing  try to test only one part ( I should not have tried to also test creating an Array while testing a timer arrangement for animating characters). After getting the timer functioning I added an Array of Images
Not only do you create an object you need to also call it.
And sometimes you need to step away from the problem for a little bit and come back fresh.

10,000 hour to mastery.
10,000 - 282.5 = 9717.5 hours left

Code

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Draw extends JPanel {
    private static final long serialVersionUID = 1L;
    private Image clockImage;
    static int xPos = 0;//I will change this later
    ImageIcon nck;
    Image[] pictures = new Image[8];

    public Image getClockImage() {
        return clockImage;
    }

    public void setClockImage(Image clockImage) {
        this.clockImage = clockImage;
    }

    public void myPics() {
        nck = new ImageIcon(this.getClass().getResource("clock1.png"));
        pictures[0] = nck.getImage();
        nck = new ImageIcon(this.getClass().getResource("clock2.png"));
        pictures[1] = nck.getImage();
        nck = new ImageIcon(this.getClass().getResource("clock3.png"));
        pictures[2] = nck.getImage();
        nck = new ImageIcon(this.getClass().getResource("clock4.png"));
        pictures[3] = nck.getImage();
        nck = new ImageIcon(this.getClass().getResource("clock5.png"));
        pictures[4] = nck.getImage();
        nck = new ImageIcon(this.getClass().getResource("clock6.png"));
        pictures[5] = nck.getImage();
        nck = new ImageIcon(this.getClass().getResource("clock7.png"));
        pictures[6] = nck.getImage();
        nck = new ImageIcon(this.getClass().getResource("clock8.png"));
        pictures[7] = nck.getImage();
    }

    // step thru pictures Array based on time passed
    public void cycle() {

        long lastTime = System.currentTimeMillis();
        long nowTime;
        int count = 0;

        while (true) {
            nowTime = System.currentTimeMillis();
            if (nowTime - lastTime >= 100) {
                setClockImage(pictures[count]);// next image from array
                count++;
                if (count > 7) {
                    count = 0;
                }
                lastTime = nowTime;
            }
        }
    }// end of cycle()

    public void paintComponent(Graphics gph) {
        super.paintComponent(gph);
        // plain white can get boring also I wanted to make sure it was drawing
        // something

        this.setBackground(Color.LIGHT_GRAY);

        gph.drawImage(getClockImage(), xPos, 0, this);
        // for the fun of it show another clock animation next to the first
        gph.drawImage(getClockImage(), xPos + 65, 0, this);
        repaint();
    }
}// end of class Draw

Sunday, September 23, 2012

Mouse control

 I was able to change the paddle control  from keyboard to mouse control which really boosted response and in the process realized that I had set up the threading to create a new thread every time the paddle was moved with the keys which I fixed just by removing threading for the paddle. I will have to learn more about threading to do it correctly. Still there is no lagging with that thread removed. Sound is in it's own thread and that was the major lagging part.

10,000 hour to mastery.
10,000 - 274.5 = 9725.5 hours left

Upgraded to Juno

    I finally got around to upgrading from Indigo to Juno. Dreading it I delayed it for a while. The biggest fear was losing all my work. So this morning I over came my fear and did it. It was actually easier then I thought it was going to be. First I made a backup of the workspace folder just to be safe. Second was renaming Eclipse to OldEclipse . Next I downloaded  Juno, unzipped in a folder I named Eclipse, as an afterthought I should have named it Juno but never mind. Next was a matter of deleting the old shortcut icon and creating a new one. I double clicked the icon and up popped Juno Eclipse and it automatically loaded my workspace. Then for good measure I ran my game and also checked for updates. Everything worked out well. I have been sick for the past week , so I haven't done anything including programming or studying. I am feeling a lot better now and I hope to catch up quickly.

Three things I want to do with the game :
1: Better animation of the sprite "Bob".
2: Multiple sound effects.
3: Mouse control instead of keyboard.

Monday, September 17, 2012

Upgrading from Indigo to Juno


 I have a few computers and two of them I easily upgraded to Juno because I had no important files I wanted to save. I just deleted the old Eclipse and installed the newest one. My main computer though is trickier. There are files I want to keep so I can't just delete the old Eclipse. I was hoping that the new Eclipse would do the upgrade itself. No such luck. The upgrade option in the help tab finds no updates . So I searched online for an answer and I basically found that you had to add the download site to you upgrade list of sites . But it seems I did not do it correct because it still does not upgrade to Juno. So I am left with moving all the files I want to save, delete the old Eclipse, install Juno then import the kept files.

   Thoughts on game development. A good  interesting game should have a few key features going for it. Visually exciting (How do you measure that?), challenging but not so hard that it can't be done( Again, how do you quantify that?) and it should have a goal (thinking outside the box, maybe a game does not need a goal?).
As for visuals I know there are games that are just text and that calls for more of a visualization on the part of the player then graphics. Some people would say that a game must not be buggy but so games actually incorporated the bugs in to the game as features. I once read that Lora Croft's breast size was a programming error but the developers decided to keep it. I am guessing the developers were male...
  As a final thought on features for a successful game, I think you need good sounds. Try playing any game without sound and see how much different it feels.

10,000 hour to mastery.
10,000 - 268 = 9732 hours left 

Friday, September 14, 2012

Latest look


The latest game view. I tried to give the feeling of being in a room with sickly tiles on the floor.. I have been asking myself if I should set this program aside and move on to something else. Maybe a maze game . I think that would give me other challenges such as collision detection of multiple walls and more sprites to animate.
   There are a few more things I would like to do to this game and I hate to leave it unfinished. 

Time again

   I was able to remove the thread sleep I used to control the speed of the game and replaced it with  a timer. It was actually a lot easier then I thought it was going to be. What I did was first make TimerTest class to workout exactly what I would need to do it. Then I applied what I had learned to  the game

public class TimerTest {

    public static void main(String[] args) {

        long nowTime;
        long lastTime = System.currentTimeMillis();// starting time
        int counter = 0;
        while (counter < 20) {// we are going count to  20

            nowTime = System.currentTimeMillis();

            if (nowTime - lastTime >= 1000) {// one second difference
                counter++;

                // print out what we have so far
                System.out.println(nowTime - lastTime);
                System.out.println(counter);
                System.out.println();// just to make it look more readable

                lastTime = nowTime;// assign new time starting point
            }
        }
    }
}

 

Things left to do:
Add a background that is more interesting then plain black ( This is more for style then anything).  (done ,for now)
Use a timer for control. (done)
Smoother Bob animation
Better looking Bob drawing
Perhaps change control of paddle to mouse ( would it respond faster?).
Change sound effects , perhaps add background music as well.
Maybe add obstacles that Bob has to move around.  
Eventually port over to Android.  



10,000 hour to mastery.
10,000 - 264.75 = 9735.25 hours left

Wednesday, September 12, 2012

To time or to sleep?

 When I made my game ( now named Crazy Bob Escapes )I only knew about  thread sleep and I used that to control the speed of the game. While watching videos on game programming I noticed that some of them used timers " System.currentTimeMillis()"  as a control. After researching further  there seemed to be a disagreement as to which way it should be done. Some even combined them by using the timer to adjust for lag in the sleep. At the moment sleep seems to work well enough and I could leave that part alone but the whole point of making the game was to learn. So I will try to set up a timer.

Things left to do:
Add a background that is more interesting then plain black ( This is more for style then anything).
Use a timer for control.
Smoother Bob animation
Better looking Bob drawing
Perhaps change control of paddle to mouse ( would it respond faster?).
Change sound effects , perhaps add background music as well.
Maybe add obstacles that Bob has to move around.  
Eventually port over to Android.  



10,000 hour to mastery.
10,000 - 262.25 = 9737.75 hours left

Tuesday, September 11, 2012

Animate

   Making a good animated character is a bit time consuming . You have to get the movement just right between image frames. Picking colors for the character is important . Contrast is needed for certain things to accentuate movement. An introduction dialog window was added which was easier then I thought it would be. I was anticipating having to add a timer to the dialog and close it. I was thinking the dialog would be on top of the game, but it turns out that the game window does not start till you close the dialog.

If you want me to also post the code , please leave a comment.

Things left to do:
Fix the delays.  (done)
Add a introduction message   (done)
Animate Bob walking. (done)
Perhaps change control of paddle to mouse ( would it respond faster?).
Change sound effects , perhaps add background music as well.
Maybe add obstacles that Bob has to move around.  
Eventually port over to Android. 



10,000 hour to mastery.
10,000 - 258.5 = 9741.5 hours left 

Monday, September 10, 2012

Bug fixed

Well I was able to fix the blank screen fix  by rearranging Thread code. I am still not sure why it works and it does not look pretty.

Things left to do:
Fix the delays.  (done)
Clean up code.
Add a introduction message.
Animate Bob walking.
Perhaps change control of paddle to mouse ( would it respond faster?).
Change sound effects , perhaps add background music as well.
Maybe add obstacles that Bob has to move around.  
Eventually port over to Android. 


10,000 hour to mastery.
10,000 - 256 = 9744 hours left

One bug fixed another created

I managed to thread the sound class which fixed the  delay whenever it was played but in doing so I created another bug . Now the window is blank for the first game. when the game ends and it asks you if you want to play again and you click yes , then the game displays correctly and runs fine.
  I think the problem might be that sound is the last object created  and that uses an applet. So I think the applet window is painting  over the game display.

10,000 hour to mastery.   
10,000 - 250.5 = 9749.5 hours left

Sunday, September 9, 2012

Game rename







   The game is evolving, instead of a ball I had added a little man named Crazy Bob to move around. Since the ball had been removed I thought we needed a name change as well from "Racket ball" to "Crazy Bob Escapes". Also I changed the ending message from Game over to "Crazy Bob got out!".
   One problem was that I needed to change the direction that Bob looked  depending on if he was going left to right or the other direction. Since Bob is a image I thought there might be a easy flip image  method or something but I could not find it so I went in to GIMP to flipped the image, saved a copy in to the game and now the image of Bob is selected to match the direction he is going. Which might actually be faster for the game.

Things left to do:
Fix the delays.
Add a introduction message.
Animate Bob walking.
Perhaps change control of paddle to mouse ( would it respond faster?).
Change sound effects , perhaps add background music as well.
Maybe add obstacles that Bob has to move around. 
Eventually port over to Android.

Saturday, September 8, 2012

Game restart







   Added a " play again? " Dialog to the game. First I had to figure out what "Yes, No and Cancel" returned. Which I did by just using System.out.print. Yes equaled zero, No was one and cancel was 2. Then I setup a if- else chain though I could have used a switch -case. No and cancel was easy enough to implement. I just used dispose() but playing again was harder. I did some research and there is no command I can find that says to restart the program. So I just reset the ball position X and Y ,also the score back to zero. Then I called the go() method.



import java.awt.Canvas;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class RacketBall extends Canvas {

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        RacketBall racketBall = new RacketBall();
        racketBall.go();
    }

    public void go() {
        // create window
        final JFrame racketGame = new JFrame("Racket Ball");
        racketGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        racketGame.setVisible(true);
        racketGame.setSize(600, 450);
        racketGame.setLocation(500, 300);
        racketGame.setResizable(false);
        racketGame.setFocusable(true);
        racketGame.setFocusTraversalKeysEnabled(false);

        // for paddle control
        racketGame.addKeyListener(DrawDisplay.paddle);

        // create display area for wall, ball and paddle
        DrawDisplay drawGame = new DrawDisplay();
        racketGame.add(drawGame);

        // keep moving ball until paddle is passed
        Thread move = new Thread() {
            public void run() {
                int trackBall = (int) DrawDisplay.ball.getPosX1();
                while (trackBall < 560) {
                    DrawDisplay.ball.moveBall();

                    // control speed
                    try {
                        Thread.sleep(8);
                    } catch (Exception e) {
                    }

                    trackBall = (int) DrawDisplay.ball.getPosX1();
                }
               
                int newGame = JOptionPane
                        .showConfirmDialog(null, "Play again");
                //yes, then reset positions and score
                if (newGame == 0) {//yes
                    DrawDisplay.ball.setPosX1(15);//Beginning
                    DrawDisplay.ball.setPosY1((int)(Math.random() * 330) + 20);
                    Paddle.count = 0;
                    go();
                   
                } else if (newGame == 1) {// no
                    racketGame.dispose();// get rid of game
                } else {// cancel
                    racketGame.dispose();// get rid of game
                }
                racketGame.dispose();//needed to remove old game
            }
        };
        move.start();// thread start
    }// end of go
}// end of class RacketBall

Sprite


   The ball has been replaced with a little sprite that looks like a walking man ( not animated yet ). The little guy seems to move faster then the ball did. I guess it is easier for the computer to display an image then to calculate an oval and draw it. So I had to add a little bit to the wait before moving and also adjust collision detection to account for the 32 x 32 pix image  An odd side effect is the delay that was annoying with the ball has the opposite affect here. It seems more like the little guy is deciding which way to go. Still I need to figure out how to fix the delays.

Things left to do:
Animate  sprite.
Fix delays.
Add play again button which resets game or closes it.
Different sounds added.

10,000 hour to mastery.   
10,000 - 248.5 = 9751.5 hours left


Friday, September 7, 2012

Video capture



   I tried a few different  free screen video capture programs and Hypercam had a ok interface and was simple but the quality of the capture was jerky. AVS video editor interface sucked and was also jerky. Both tried to install bloatware and browser add-ons. So I resorted to pointing a video camera at the monitor and recording that way. A Rather lame way of doing it but it worked. The bad part is it is Apple's MOV format and I don't have an editor  for that. I could convert it but I was in no mood to download more software at the moment. Also the video was not compressed and even at a 1 minute and 11 seconds it was 71.1 MB in size and my internet upload speed is slow. In this demonstration you can see that the ball has a randomness added to it when it hits the paddle and also I have the game sound disabled.

Tuesday, September 4, 2012

Trouble with Threads



  A bit of trouble with threads. To fix the hesitation of the ball and sound I added threads for ball, paddle and sound. Still there is hesitation when it plays sounds. If I disable sound  the ball and paddle seem to move ok. So I don't think I implemented threads correctly.  There is also a delay of key detection between when I press the  arrow keys and when the paddle moves. It moves once then waits for the keyboard  to send multiple inputs. I added a display of GAME OVER which gave me a chance to play with fonts. My first attempt to make it restart the game after the game was over resulted in an infinite loop of creating games.  Randomness was added to the ball bouncing off the paddle which makes for a more interesting game. 

10,000 hour to mastery.    
 
10,000 - 239 = 9761 hours left

Saturday, September 1, 2012

One year

    Its been about one year of the start of my endeavor to learn how to make Android App.
This blog  was started August 19th 2011. I began by reading Android for Dummies book before that. Lets say August 1st 2011 . So Thirteen months  and  lots of hours reading , watching videos and trying to work  with Eclipse. February 16th 2012 I started 10,000 hours to Mastery. Keeping track of time devoted to learning to program. That time included Videos, reading in books, reading online and actual programming. As of today it's 232.5 hours since I started keeping track. If we were to include the time before I started writing it down , about 6 months at say an average of between half hour to an hour a day. So 6 x 30 days x .5 = 90 or twice that 180. I have studied between 322.5 and 412.5 hours. Roughly about an hour per day. I must say my average has gone up recently now that I am able to understand it better and having the little accomplishments such as my games. Programs I put together and not just typing in what someone else wrote.

   My advice to anyone wanting to learn  to program. 

First: There are many languages out there. I had to learn Java in order to make Android apps which is my goal. Research online the languages  and see which would be most suited to what you want to do. If you want to make Android Apps don't even bother picking up a book on it till you know Java. In fact you don't really need a book, there are so many online sources to learn from. I just like books because I find they are easier to read from. Also YouTube is a good place to start for videos.

Second: Take notes while watching the videos. Those notes should include questions that come to mind while watching. This also applies to reading.

Third: DO NOT go online and ask for help (basically asking for someone else to do the work for you) without first doing a search for the answer yourself. Google is great for that. Bing might be good for it also but I never use that search. If you get to the point where you can't find an answer  and you need to ask in a forum. Ask a direct question and be polite. Don't copy paste your code online hoping someone will tell you where the bug is. I have never asked for help like this but I have asked for advice  on where to find answers because sometimes you are not even sure what you should type in to google  search

Fourth: Do keep a log of the hours studying and set a goal of the number of hours per day. Which combines two tricks I have learned to help accomplish goals . 10,000 hours to Mastery and Don't break the chain. ( read past post to learn more about those).

Fifth: Know that you will get frustrate and feel like you are not learning anything. If you been studying more than 30 minutes , get up and walk a bit then come back. I have a timer with an alarm to keep track of it. Which also helps keep track of my hours per day goal. Do not eat while at the computer but you can have a drink. My choice is black coffee . I find that eating makes me sleepy and I eat to many calories from the snacks.

Sixth: Be sure to have a bit of fun while learning. If you are making a game for example  make it do something silly like play a funny noise. I recorded myself saying " Boo Yah!"and applied it to my Racket ball game. So every time  it bounces  you hear the sound. If its something that just prints text , have it say something encouraging like "YES, It worked!"