Pages

Showing posts with label Tetris. Show all posts
Showing posts with label Tetris. Show all posts

November 9, 2014

Implementing Derby in Greenfoot’s Jar Deployment: Tetris Game

Hello, this November should be called database month in my blog! I’ll post databases related technology such as MySQL, Derby, etc (from {Java} developer’s point of view) to familiarize myself with such technology so I can learn more advanced technology, such as spatial database technology.

Simple greenfoot tetris game
While there are so many libraries out there on the internet that you can use within greenfoot, this post will be the implementation of embedded derby in your greenfoot scenario deployment and a little of its problem. A little knowledge about greenfoot (2.4.0), jar packaging, and embedded derby will be helpful when reading this article. I also post my tetris game distribution which you can download below this post.



Imagine a situation where you have developed from scratch, a tetris game in greenfoot, and then you said to yourself,

“Hmm, I’m too lazy to write down the score saving code manually, I should use derby instead.“

If you want to utilize derby or any other libraries in greenfoot IDE, the IDE has to know where the derby library is and load the derby library’s driver from there. For that, you need to do:

Set the classpath in greenfoot environment by:
  1. Clicking edit -> preferences.
  2. In libraries tab, in user libraries, click add and enter the location to your derby.jar files .
  3. Finally, restart greenfoot.
Then, you can connect to the database via JDBC through derby’s driver. The path to the database will begin from your greenfoot scenario’s root folder.

Problem with Deployment

While it is relatively easy to use derby to create and access database right away in your greenfoot scenario, it’s not the same if you deploy it into a jar file.

You see me losing the game..
If you deploy (or ‘export’) your scenario and run it as is, all of your database related code will be useless. I don’t know the exact problem, but what I do know is that I’ve never saw any ‘jar in a jar’ files in any Java application’s jar. Maybe, your game can’t access the jar files when it is a jar file by itself and thus, the driver will never loaded.

But, worry not--remembering the Java packaging/deployment tutorials, I’ve decided to put derby library (jar) in the same directory as the game’s jar file (not inside the jar) and set the classpath in the game’s jar manifest file to include the derby library.

Derby’s database system and your database folder, will belong in the directory (root directory) that contain your game and derby library’s jar files.

These are the highscore which is saved using derby library
Finally, besides your game’s jar file and the derby’s library, you should put the (pre-generated) database in your game distribution.

The final file structure of your distribution more or less should look like this:

[your game directories] /
   |--- [your game].jar
   |--- derby.jar
   |---derby.log
   |---database /
     |---[your database]

To summarize things up, you can use external libraries in greenfoot by addressing the libraries in the preferences. After you deploy your greenfoot scenario into jar file, you should put the library besides your scenario jar’s file (or database folder) and tell your game’s jar file manifest where the library is.

Tetris game: https://drive.google.com/file/d/0B27ziWaCDimLenNKNkZVYWNwdms/view?usp=sharing


December 26, 2010

Membuat game tetris!

Akhirnya game tetris yang saya buat selesai juga! Gamenya bisa dilihat disini: http://greenfootgallery.org/scenarios/2296

Pada game tetris ini saya hanya mempunyai beberapa class(kelas) pokok, yaitu:
  • TetrisWorld yang merupakan subclass dari World, disini kita akan banyak membuat gameplay.
  • Tetromino yang merupakan kelas abstrak, subclass dari Actor, disinilah fisik dan sifat dari blok-blok tetris dibuat. Kelas tetromino ini mempunya banyak subclass(subkelas) yang berupa bentuk bentuk yang sudah tidak abstrak lagi (sudah konkrit). Subkelas tersebut hanya memberikan informasi tentang lokasi dari blok-blok pembentuk tetromino, karena sifat sifatnya sudah terkandung dalam kelas tetromino ini. Berikut ini adalah subkelas dari Tetromino:
    • TBlock - bongkahan tetris berbetuk T
    • SBlock - bongkahan tetris berbetuk S
    • OBlock - bongkahan tetris berbetuk O
    • LBlock - bongkahan tetris berbetuk L
    • ZBlock - bongkahan tetris berbetuk Z
    • JBlock - bongkahan tetris berbetuk J
    • IBlock - bongkahan tetris berbetuk I
  • Block yang merupakan blok pembangun tetromino, tidak ada kode yang istimewa, hanya untuk membantu kode pada kelas Tetromino.
Sedangkan kelas lainnya pada game ini hanyalah tambahan. Berikut ini kelas tambahan dan fungsinya:
  • Counter kelas untuk menampilkan skor pada game ini. Kelas ini bisa di download di kelas pembantu    http://greenfoot.org/programming/classes.html
  • MidiPlayer kelas untuk memainkan/membuat midi. Bisa didapatkan pada link diatas juga.
  • MidiFilePlayer kelas untuk memainkan file midi. Karena saya mengalami sedikit masalah pada kelas MidiPlayer, maka saya cari kelas lainnya untuk memainkan musik midi, dan saya menemukan kelas ini dari skenario milik tantinton pada greenfootgallery.org
Oke bagi yang mau mempelajarinya, silahkan download dulu sourcenya dari sini:
http://greenfootgallery.org/scenarios/2296
      Update!!! 30, April 2011 
       Telah terjadi error pada pemutar midi, jadi scenarionya tidak bisa dimainkan, tetapi bisa di akali dengan tidak menggunakan suaranya.

      December 16, 2010

      Sebentar Lagi: Tetris Game!

      Kali ini saya akan membuat game Tetris! Tentunya dengan menggunakan Greenfoot. Untuk pembuatannya sendiri masih mencapai tahap fisiknya saja, belom permainannya (gameplaynya). Langsung saja kita lihat screenshootnya :
      Kalian bisa lihat blok blok "Tetromino" tetris yang ada diaatas. Mereka semua masih belum bisa melakukan apa apa. Belum bisa diputar ataupun digerakan–baru bisa jatuh. ^_^". Ya seperti ini, masih fisiknya saja, belum ada gameplay.
       ...
      Okeee waktunya buat saya untuk melanjutkan pembuatan game ini!

      Update! 19 Desember 2010
      Fisik sudah selesai. Besok saya akan buat gameplaynya. 

      Update! 21 Desember 2010
      Disempurnakan dan dirapihkan kodingnya biar lebih gampang.
      Gameplay setengah jadi. Satu tahap lagi! :D 

      Game sudah di upload ke Greenfootgallery.org 
      http://greenfootgallery.org/scenarios/2296 
      tapi tetap, masih belum ada gameplaynya, harap sabaar. 

      Update! 23 Desember 2010
      Sudah ada gameplaynya! game sudah jadi! Tinggal  ditambahkan "dekorasi" hehehe
      Scoreboard sudah ada, penambahan kecepatan juga.
      Tutorialnya menyusul!