Sunday, April 5, 2015

Simple Wave File Player

Introduction

This post presents a simple Wave File Player written in Java, using the sampled sound API. Since we are not using JavaFX, it will not support the mp3 format.

Import

We will be using API from the javax.sound.sampled package. Hence we make the following imports at the beginning of the program.
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;


Progress bar

We will use a JSlider as a progress bar. The scale are from 0 to 10000, which is simple to represent 0 to 100% with two decimal place. To update the progress, the following method will be invoked by a Swing Timer on every 30ms.

Sample Wave File

  epic_loop.wav : Orchestral Adventure by RevampedPRO, CC BY 3.0 license 
                  http://opengameart.org/content/orchestral-adventure 

  twoTone2.wav : "63 Digital sound effects" by Kenney, CC0 license, 
    http://opengameart.org/content/63-digital-sound-effects-lasers-phasers-space-etc
The wave files are inside a github project. Follow the following link to download the project as a zip file.
https://github.com/macteki/WavePlayer

Source Code

Saturday, February 28, 2015

Moving Objects and Sprite Manager - Part 10

This is part 10 of the series. You are suggested to read the whole series from the beginning. You may find the whole series in the Featured page.


The Game of Space Invader



Attribution

"Space Ship Shooter Pixel Art Assets" by ansimuz / CC0
You may download the file space_shooter_pack.zip at opengameart.org.

Compile and Run

You will need Scroller.java, Sprite.java, ShipSprite.java, EnemySprite.java, FireboltSprite.java, ExplosionSprite.java, LaserSprite.java and space_shooter_pack.zip Place everything in the same folder and type:
javac *.java
java Scroller


Scroller.java



EnemySprite.java



FireboltSprite.java



ExplosionSprite.java

LaserSprite.java



ShipSprite.java



Sprite.java

Moving Objects and Sprite Manager - Part 9

This is part 9 of the series. You are suggested to read the whole series from the beginning. You may find the whole series in the Featured page.


A sprite in a scrolling background



Compile and Run

You will need Scroller.java, Sprite.java, ShipSprite.java and space_shooter_pack.zip Place everything in the same folder and type:
javac *.java
java Scroller


You may download the file space_shooter_pack.zip at opengameart.org.

The Java source file are listed below.

Scroller.java



ShipSprite.java



Sprite.java

This is the same as the file Sprite.java in "Moving Objects and Sprite Manager Part 6".

Friday, February 27, 2015

Moving Objects and Sprite Manager - Part 8

This is part 8 of the series. You are suggested to read the whole series from the beginning. You may find the whole series in the Featured page.


Scrolling Background

We will use a looping background created by ansimuz.

You may find the original collection at :
http://opengameart.org/content/space-ship-shooter-pixel-art-assets

The author release the file under the CC0 license. In summary, the license allow you to use it in your own game, even for commercial purpose.

You may download the file space_shooter_pack.zip

We will use the file desert-backgorund-looped.png inside the zip file.

This is a looping background. That means if we cut the bottom row of the image, and then paste it to the top, we still have a continuous image. Hence the scrolling is as simple as: Note that we are using the same technique in Optimized flood filler to access the pixel array of the image. This is much faster than the getPixel() and setPixel() API.


Reading image from zip file

We will read directly from the zip file so that we don't need to uncompress the file to the harddrive. The source code for reading zip file is defined in readZipImage() in the demonstration. The reason to read directly from zip file is that we may redistribute other people artwork in an unmodified form. This typically fulfill most of the license requirement in http://opengameart.org.

Important
There are many free artworks available in opengameart.org. However, you must read the license of the individual entry before using it. Not every entry is released under CC0. Different authors may submit their work under different licenses. For example, many licenses require you to attribute the original author. Just like what I am doing below :



You may have a look at Best Practices for attribution


Demonstration

You will need Scroller.java and space_shhoter_pack.zip in the same folder.

Compile and run

javac Scroller.java
java Scroller


Friday, February 20, 2015

The Game of Klotski

Introduction


Klotski is a sliding block puzzle.


Compile and Run

javac *.java
java Main
You will need Block.java, BlockList.java, Board.java, Cell.java and Main.java in the same folder.


Source Code

Simple Drag and Drop

Introduction


We are going to implement a simple drag and drop function. The program will display a red block on the screen and you may drag and drop it to anywhere inside the window.

Mouse Event

For a simple implementation, we just need to implement two functions. One is known as mousePressed and the other is known as mouseDragged. These two functions are defined in two different interfaces. Hence we need to implement both interfaces. Though we are only interested in two functions, we still need to provide empty implementation for every function defined in MouseListener and MouseMotionListener

Basic Flow of Drag and Drop

  1. Remember the mouse position when the mouse is pressed. This is the starting point.
  2. When the mouseDragged event is received, calculate a displacement vector from the starting point to the current mouse position.
  3. Add the displacement vector to the position of the object.

The following is the implementation of step 1. All we have to do is just remembering the starting position when mouse is pressed.

The following is the implementation of step 2 and 3.

Demonstration


For a full demonstration, compile and run the following source code.