Friday, August 31, 2012

Added sound

   I added sound to the Racket ball game today. One of the problems I had was the first time it tries to play it delays everything for a moment while loading. A temporary fix is that I have it play the sound when the ball is first created then afterwards whenever it plays the sound on bounces there is no delay. I am sure there is a better way of playing the sound, perhaps in a separate thread.
    I used a free sound program "Audacity" to record a few sounds for the game. An easy to use program with good effects. I tried Windows sound recorder  but it was awful.

   What is left to do in the game.
1: Fix the bug of the paddle sticking to the top wall.
2: Add bounce to the ball hitting the paddle, changing directions.
3: Add bounce to far wall opposite the paddle.
4: Have the score keeper function ( right now it is displaying X position)
5: Add "Play another round?" to restart the game.
6: Maybe add speed increase as the game goes on.( At the moment I am using Thread sleep to control  the speed of the game  but maybe a Timer() would work better.)

When I finish this game I want to post a video of it to YouTube

Update to today's posting
I finished  point 2, 3 and 4.  I am able to play it  but there is a speed control problem. Sound needs to be on a separate thread. The ball slows down sometimes stops and sometimes the sound is cut short. Also paddle control could be smoother.

10,000 hour to mastery.  
10,000 - 232.5 = 9767.5 hours left

Thursday, August 30, 2012

Making progress

   I was able to add to the Racket ball game today. I corrected the ball starting position and simplified the direction method. I added  change ball color when wall is hit just to add a bit of flair to it. I added paddle movement up and down but I am having two problems with it. One is the paddle sticks if it reaches the top and two is the slow response time of the paddle when the key is pressed

private double slope = (Math.random()*2-1);// 90 deg angle

    void moveBall(){
       
            posX1++; // move ball from left to right
            posY1 = posY1+slope;
           
            if(posY1 <10){   // check for collision top wall
                slope =slope *-1;// reverse up direction to down
                ballColor=(Color.green);//change color of ball
            }
            if(posY1 >350){ // check for collision bottom  wall
                slope =slope *-1;// reverse down direction to up
                ballColor=(Color.YELLOW);//change color of ball
            }
        }

I was so excited by the progress I made that I had to show a couple family members. In this day of high end graphic games they were less then impress by my simple unfinished game. That deflated my balloon a bit. They could not appreciate the effort that went in to programming the game. Lesson learned lol . 

Tuesday, August 28, 2012

First Graphical game





   This is a screen snipit of the Racket ball game. It's a game based on the old pong game  which had a single player option. Still a long way to go but I would also like to port it over to Android eventually.

1: Moving the ball in different directions. (add a bit of randomness)
2: Moving the paddle.
3: Controlling the paddle with key inputs.
4: Detecting collisions.
5: Change ball direction after collision also with a bit of randomness.
6: If ball is hit by paddle increase score.
7: Play sounds on events ( eg ball hitting paddle).
8: Perhaps have the ball and/or the paddle change color on collisions.

    I bought a dry erase board, small enough to be held in your lap and assorted color markers. Surprisingly having the ability to easily erase and having different colors helps as you are trying to figure out what needs to be done. Paper feels so permanent even though you can erase, I felt the need to think out every detail before writing anything down on paper. Perhaps because as a child I was told not to "waste" paper.





10,000 hour to mastery.    
 
10,000 - 222 = 9778 hours left

Friday, August 24, 2012

Finished Head FIrst Java

   I finally finished the Head First Java book. Completing all the examples and tests except for the end examples which were about servlets, I felt I only needed to read that part since I am actually trying to make Android apps. Now I will review the book quickly and after that go back to the first Java book I bought which is  Java for dummies. As I have been studying and trying to program Java I have been using Eclipse IDE but as I have moved along I am thinking I should try to use command line  and notepad. Eclipse tends to make it so easy by warning you about everything, offering  easily listed suggestions and auto-fill that you sort of missing something while learning. One way to explain what I mean is when you take a photography class they want you to use manual on the camera to fully grasp the concepts.

   YouTube has a long list of videos on programming but there are two problems I have encountered.
One is that they list videos in languages other than English which is pointless for me and I wish I knew a way of filtering. Setting language to English in the account has no effect. The second problem is old videos which are outdated. Basically I want to filter videos by Java (programming language, not the country) and must be in English and must be less than 2-3 years old. Also if I subscribe to someone let me sort by subject matter and PLEASE YouTube arrange part 1, part 2, part 3 .... in order . Don't force me to hunt for them. You would think that now Google owns YouTube that they would do searches better. A last note about YouTube and their transcription option for videos. A great idea if it worked but for now it is practically useless.

10,000 hour to mastery.    
 
10,000 - 215 = 9785 hours left 

Thursday, August 16, 2012

Access

  To get a better understanding  of accessing variables and methods from other classes  I created five classes
One: ParentOne , which is the super class.
Two: ChildOne, which is the subclass of ParentOne and in the same package.
Three: ChildTwo , which is a subclass of ParentOne  and is not in the same package.
Four: UnrelatedOne, which is in the same package but is not extended from ParentOne.
Five: UnrelatedTwo, which is in a different Package and does not extend ParentOne
----------------------------------------------------------------------------------------------------
ParentOne.java:
package MiscTests;
// A test of private, protected, public  and no modifier
// classes ChildTwo and UnrelatedTwo  have to import the package ParentOne is in
public class ParentOne {

    // class variables
    public static String pubNam = "A public string";
    private static String priNam = "A private string";
    protected static String proNam = "A protected string";
    static String openNam = "An open string";

    public static void main(String[] args) {
        System.out.println(pubNam);
        System.out.println(priNam);
        System.out.println(proNam);
        System.out.println(openNam);
        go();
    }


    // no modifier then subclass can not access
    // and Unrelated class outside of project can not access
    // private then nothing outside of class ParentOne can access
    // protected then unrelated classes outside of the project can not access
    public static void go() {
        // can only use "final" modifier on variables
        // can not use private, public , protected
        String openMethStg = "go method string";
        System.out.println(openMethStg);
    }
}

---------------------------------------------------------------------------------------------------
 What I Think I have learned.
   Making everything as accessible as possible  is easier in the short run but defeats the purpose of Object Orientated Programming , which is to protect your program from incorrect access and changes  that you don't want.
   To protect your program, only permit enough access to allow it to run. Restrict what access you can, Encapsulate as much as possible.
   If something needs access then create getters and setters (eg. getName and setName)

Wednesday, August 8, 2012

Android Focus

  I came across a good series of Android videos.

http://youtu.be/5RHtKIo_KDI

As I was watching it, it occurred to me that I have been focusing on Java so much that I lost sight of why I was learning Java in the first place, which is to be able to make Android  Apps. Many things in Java apply to Android except those things that involve the GUI. In Java it is Swing which I believe is a part of the awt library.
Speaking of GUI , this is a good place to start for designing the look and feel of your app.
http://developer.android.com/design/index.html

10,000 hour to mastery.    
10,000 - 195= 9805 hours left  



Sunday, August 5, 2012

Milestone

   I have reached a milestone. A complete OOPs program ( modest as it may be) completely designed and written by me. Two classes besides the main class. Player and Die ( it's a dice game). This is the first time I can say I thought in terms of Objects while programming. Of course there are improvements I could make  such as a better handling of the input Exception. Instead of stopping the program because of a wrong input it should keep asking for a correct input but I will save that for another time. Here is the program. Feel free to comment.


import java.util.Scanner;
/*
 Dice game between two players using two dies. Highest number of the two dice combined wins roll.
 User determines number of rolls of the dice. 
 Three outcomes for dice roll. Player one wins, Player two wins or both players are equal which is a Draw
 */
public class DiceRollGame {
public static void main(String[] args) {
int draw = 0;// could have made draw a Player class
Player playerOne = new Player();
Player playerTwo = new Player();
Die dieOne = new Die();
Die dieTwo = new Die();
Scanner sc = new Scanner(System.in);
System.out.println("You are player 1 competing against player 2");
System.out.println("How many times would you like to roll?");
try {
int rolls = sc.nextInt();
for (int i = 0; i < rolls; i++) {
// roll dice for playerOne
int die1Player1 = dieOne.DieRoll();
int die2Player1 = dieOne.DieRoll();
int pl1Total = die1Player1 + die2Player1;
// roll dice for playerTwo
int die1Player2 = dieTwo.DieRoll();
int die2Player2 = dieTwo.DieRoll();
int pl2Total = die1Player2 + die2Player2;
// show dice roll outcome
System.out.println();
System.out.print("You rolled " + die1Player1 + " and "
+ die2Player1);
System.out.println(" Total " + (die1Player1 + die2Player1));
System.out.print("Player Two rolled " + die1Player2 + " and "
+ die2Player2);
System.out.println(" Total " + (die1Player2 + die2Player2));
// check who had a higher roll. display winner then set
// winner's score
if (pl1Total > pl2Total) {
System.out.println("You won that roll");
playerOne.setScore(1);
} else if (pl1Total < pl2Total) {
System.out.println("Player 2 won that roll");
playerTwo.setScore(1);
} else if (pl1Total == pl2Total) {
System.out.println("Draw");
draw++;
}
}// end of loop
// Check winner and display scores
int Totalpl1 = playerOne.getScore();
int Totalpl2 = playerTwo.getScore();
if (Totalpl1 > Totalpl2) {
System.out.println();
System.out.println("You are the winner, with a total  wins of "
+ Totalpl1);
System.out.println("Player Two had " + Totalpl2 + " Wins");
System.out.println("Draws " + draw);
} else if (Totalpl1 < Totalpl2) {
System.out.println();
System.out.println("Player Two is the winner, with a total  wins of "
+ Totalpl2);
System.out.println("You had " + Totalpl1 + " Wins");
System.out.println("Draws " + draw);
} else if (Totalpl1 == Totalpl2) {
System.out.println();
System.out.println("You " + Totalpl1);
System.out.println("Player Two " + Totalpl2);
System.out.println("Draws " + draw);
System.out.println("A draw , so we are all winners!");
}
sc.close();
} catch (Exception e) {
System.out.println("I needed a integer");
}
}// end main()
}// end class DiceRollGame
   class Player {
private int score = 0;
public int getScore() {
return score;
}
public void setScore(int score) {
this.score += score;
}
 }// end class Player
class Die {
public int DieRoll() {
int roll = (int) ((Math.random() * 6) + 1);
return roll;
}
}// end class Die

10,000 hour to mastery.    
10,000 - 192 = 9808 hours left  

Wednesday, August 1, 2012