PERTEMUAN 5 5.1.Teks Terdapat 2 cara memunculkan teks a. Dibuat sebagai image .png Teks dibuat dengan Photoshop / coreldraw/ atau software lain, disimpan sebagai image. Image dicopy kefolder image dari project yang dibuat. Langkah selanjutanya buat actor, actor diberi image sesuai gambar berisi teks..
b. Menggunakan class: greenfoot.image 1.membuat Actor contoh :tulisan, tanpa gambar. Buka editor, isikan program ini. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Font; import java.awt.Color; /** * Write a description of class tulisan here. * * @author (your name) * @version (a version number or a date) */ public class tulisan extends Actor { private String huruf; private int panjangHuruf; public tulisan( String depan ) { huruf = depan; // membuat kotak untuk tulisan panjangHuruf = (huruf.length() +2) *16; setImage (new GreenfootImage(panjangHuruf,24)); // menentukan font GreenfootImage gambar = getImage(); Font jenisHuruf = gambar.getFont(); gambar.setFont(jenisHuruf.deriveFont(24.0F)); //mencetak gambar.clear(); gambar.setColor(Color.RED); gambar.drawString(huruf, 1, 18); }
2.Memanggil Contruktors, sebgai latihan di panggil dari world :
4. Supaya muncul maka perlu di tambahkan objek import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bg here. * * @author (your name) * @version (a version number or a date) */ public class Bg extends World { tulisan T=new tulisan("anak kelas C dan D"); /** * Constructor for objects of class Bg. * */ public Bg() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); addObject(T,200,100); } public void act(){ }
6.2 Membuat game Dasar
Siapakan bahan : 1.gambar selamat datang 2.Gambar menang 3.Gambar kalah 4.Buat project 5.Buat latar 6.Buat actor : a.musuh b.Menang( untuk menanpilkan kata menang) c.lakon d.Score e.Awal ( untuk menanpilkan kata selamat datang) f.kalah
a.Latar import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class latar here. * * @author (your name) * @version (a version number or a date) */
public class latar extends World { private int delay=0; public int jum_skor=0; public boolean mulai=false; /** * Constructor for objects of class latar. * */ score p= new score("Cah ndeso ,Skor : "); public latar() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); prepare(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { lakon lakon = new lakon(); addObject(lakon, 476, 50); awal l = new awal(); addObject(l, 300,300);
} public void mulai_game() { mulai=true; }
public void ubahSkor(int nilai) { p.setSkor(jum_skor); } public void tambah_score() { p.tambah(1); }
public void act(){ if(mulai==true){ addObject(p, 150,380); if(delay++ ==50){ addObject(new musuh(),0,Greenfoot.getRandomNumber(300)); delay=0;} ubahSkor(jum_skor); if(jum_skor >= 10){ Menang m=new Menang(); addObject(m, 200, 200); Greenfoot.stop(); } if(jum_skor == 0){ Kalahk=new kalah(); addObject(k, 200, 200); Greenfoot.stop(); }
} } }
b.awal import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class awal here. * * @author (your name) * @version (a version number or a date) */ public class awal extends Actor { /** * Act - do whatever the awal wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.isKeyDown("enter")){ ((latar) getWorld()).mulai_game(); getWorld().removeObject(this); } } }
c.Lakon import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class lakon here. * * @author (your name) * @version (a version number or a date) */ public class lakon extends Actor { /** * Act - do whatever the lakon wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void cek_tombol(){ if(Greenfoot.isKeyDown("up")){ setLocation(getX(), getY()-2);} if(Greenfoot.isKeyDown("down")){ setLocation(getX(), getY()+2);} if(Greenfoot.isKeyDown("left")){ setLocation(getX()-1, getY());} if(Greenfoot.isKeyDown("right")){ setLocation(getX()+1, getY());} Actor b=getOneIntersectingObject(musuh.class); if(b !=null){ ((latar) getWorld()).tambah_score(); ((latar) getWorld()).jum_skor++; getWorld().removeObject(b); } } public void act() { // Add your action code here. cek_tombol(); } }
C.Score import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Font; import java.awt.Color; /** * Write a description of class score here. * * @author (your name) * @version (a version number or a date) */ public class score extends Actor { private String huruf; private int panjangHuruf; private int angka; /** * Act - do whatever the score wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */
public score( String depan ) { huruf = depan; panjangHuruf = (huruf.length() +2) *16; setImage (new GreenfootImage(panjangHuruf,24)); GreenfootImage gambar = getImage(); Font jenisHuruf = gambar.getFont(); gambar.setFont(jenisHuruf.deriveFont(24.0F)); }
public void cetak_score(){ GreenfootImage gambar = getImage(); gambar.clear(); gambar.setColor(Color.RED); gambar.drawString(huruf + angka, 1, 18); } public void tambah (int skor) { angka += skor; } public void setSkor (int skor) { angka = skor; } public int getSkor () { return angka; } public void act() { // Add your action code here. cetak_score(); } }
d.Musuh import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class musuh here. * * @author (your name) * @version (a version number or a date) */ public class musuh extends Actor { /** * Act - do whatever the musuh wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. setLocation(getX()+1,getY()); if(getX()>=(getWorld().getWidth()-10)) { getWorld().removeObject(this); } } }
Tugas ! 1.Buat games seperti diatas, 2.Lengkapi dengan kalah, 3.Evek Music. 4.Gambar dibuat yang menarik/enak dilihat; www.cahyosuminar.wordpress.com