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

For a brief explanation of the source code, please visit Teach Yourself Java Programming in 21 Games

Attribution

"Space Ship Shooter Pixel Art Assets" by ansimuz / CC0
You may download the file space_shooter_pack.zip at opengameart.org.
Alternatively, you may download an unmodified version of the zip file here.

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

For a brief explanation of the source code, please visit Teach Yourself Java Programming in 21 Games

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.
Alternatively, you may download an unmodified version of the zip file here.

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. For a brief explanation of the source code, please visit Teach Yourself Java Programming in 21 Games


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.


Board.java


Cell.java


BlockList.java


Block.java

Main.java

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.