Anda di halaman 1dari 47

Latihan Membuat Game Sederhana

http://bit.ly/latihangreenfoot
Question? Ask Me Please:
HATMA Suryotrisongko
Whatsapp: 0853.5900.7300
Email: suryotrisongko@gmail.com

1. Buka Greenfoot, Create New


Scenario

2. Background. Buat New Subclass


World Space
(space.gif)

B = Gambar2 yang diperlukan untuk latihan ini sudah tersedia


folder 1. Gambar2 Untuk Membuat Game Latihan

3. Player Ship. Buat New Subclass


Actor Ship
(rocket.png)

4. Tambahkan Object Ship ke World


(klik kanan pada Class Ship, new)

5. Klik Kanan (Atau menu Control),


Save The World addObject()

6. Kontrol Keyboard. Buka


Source Code Class Ship, buat
method. isKeyDown

private void moveUp()


{
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY() - 1);
}
}

private void moveDown()


{
if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY() + 1);
}
}

private void moveLeft()


{
if (Greenfoot.isKeyDown("left"))
{
setLocation(getX() - 1, getY()) ;
}
}
private void moveRight()
{
if (Greenfoot.isKeyDown("right"))
{
setLocation(getX() + 1, getY() );
}
}

etika Copy Paste, klik menu EDIT > AUTO LAYOUT supaya rapi kode prog

7. Tambahkan pemanggilan method


di dalam public void act() { } Tes !
public void act()
{
moveUp();
moveDown();
moveRight();
moveLeft();
}

8. Shooting Missiles. Buat New


Subclass Actor Laser
(missile.jpg)

9. Edit Source Code Laser, Tambahkan


di public void act() { } setLocation
public void act()
{
setLocation( getX() + 10,
getY() );
}

10. Buat method fire() di class Ship,


lalu tambahkan di act(), tes
menembakkan peluru
public void fire()
{
if (Greenfoot.isKeyDown("space"))
{
getWorld().addObject( new Laser(), getX(), getY()
);
}
}
public void act()
{
moveUp();
moveLeft();
fire();
}

moveDown();

moveRight();

11. sound effect tambahkan


playSound() di method fire()
(shot1.wav)

NB = file audio harus sudah di copy ke dalam folder


sounds di project greenfoot anda.

public void fire()


{
if (Greenfoot.isKeyDown("space"))
{
getWorld().addObject(new Laser(), getX(),
getY());
Greenfoot.playSound("shot1.wav");
}
NB = File Audio yang diperlukan untuk latihan ini sudah tersed
}
di folder 2. Audio Suara Untuk Membuat Game Latihan

12. The setPaintOrder()


method supaya peluru keluar
dari perut pesawat

NB = tambahkan setPaintOrder() ke dalam


Constructor World
public Space()
{
super(600, 400, 1);
setPaintOrder(Ship.class,
Laser.class);
prepare();
}

13. Removing the missiles from the


world. Edit class Laser, act() method
public void act()
{
setLocation(getX() + 10, getY());
if ( getX() >= getWorld().getWidth()
- 1)
{
getWorld().removeObject( this );
}
}

14. Spawning alien ships randomly


buat class Enemy (cruiser.gif)

15. Method act() Enemy


bergerak ke kiri, menghilan kalau
sudah sampai pojok
public void act()
{
setLocation(getX() - 5, getY());
if (getX() <= 0)
{
setLocation(getWorld().getWidth() + 20 ,
Greenfoot.getRandomNumber(400));
}
}

16. Tambahkan beberapa objek enemy


pada constructor Space addObject()
private void prepare()
{
Ship ship = new Ship();
addObject(ship, 91, 221);
addObject(new Enemy(), getWidth() + 20 ,
Greenfoot.getRandomNumber(400));
addObject(new Enemy(), getWidth() + 20 ,
Greenfoot.getRandomNumber(400));
addObject(new Enemy(), getWidth() + 20 ,
Greenfoot.getRandomNumber(400));
}

Check point 1. Ketinggalan ? aja project


greenfoot yang ada di dalam file zip
CheckPoint1.zip

17. Hold that fire! buat variabel di class


Ship, dekremen setiap act(), cek kondisi
tembak peluru
private int canShoot = 0;
public void fire()
{
if (Greenfoot.isKeyDown("space") && (canShoot <= 0))
{
getWorld().addObject(new Laser(), getX(), getY());
Greenfoot.playSound("shot1.wav");
canShoot = 25;
}
}
public void act()
{
moveUp();moveDown();
fire();
canShoot--;
}

moveRight(); moveLeft();

Check point 2. Ketinggalan ? aja project


greenfoot yang ada di dalam file zip
CheckPoint2.zip

18. Collision
Detection. ???
if (getOneIntersectingObject(Laser.class) !
= null)
{
Show explosion
Play explosion sound
Remove the missile from the world
Remove the enemy (this enemy object)
from the world
}

18. Explosion. Create Actor new subclass,


set image explosion.png, copypaste code
dari Explosion.java

B = Kode program java Explosion.java untuk latihan ini sudah tersedia


folder 3. Helper Class Untuk Membuat Game Latihan

19. Collision Detection. Edit Enemy


class, tambahkan di act() method. Tes!
if (getOneIntersectingObject(Laser.class) !=
null)
{
getWorld().addObject(new Explosion(),
getX(), getY());

Greenfoot.playSound("explosion.wav");
Actor laser =
getOneIntersectingObject(Laser.class);
getWorld().removeObject(laser);
NB = File Audio yang diperlukan untuk latihan ini sudah tersed
getWorld().removeObject(this);
di folder 2. Audio Suara Untuk Membuat Game Latihan
}

20. Let's keep those enemies coming! Buat


method resurrect() utk menggantikan
removeObject(Enemy)
private void resurrect()
{
setLocation(getWorld(). getWidth() + 20,
Greenfoot.getRandomNumber(400));
}
if (getOneIntersectingObject(Laser.class) != null)
{
getWorld().addObject(new Explosion(), getX(), getY());
Greenfoot.playSound("explosion.wav");
Actor laser = getOneIntersectingObject(Laser.class);
getWorld().removeObject(laser);
resurrect();
}

Check point 3. Ketinggalan ? aja project


greenfoot yang ada di dalam file zip
CheckPoint3.zip

21. The Score()class: Class declaration,


drawstring() and setImage() buat new
subclass Actor Score()
Buat constructor di class Score
public Score(String text)
{
GreenfootImage img = new
GreenfootImage(text, 24, Color.WHITE,
Color.BLACK);
setImage(img);
}
Tambahkan statement import di class Score
import java.awt.* ;

22. The World class (Space):


constructing and initiating the
Score object

Tambahkan ke dalam constructor

Score score_field = new Score


("Score:");
addObject(score_field, 50, 50);

23. Modifikasi constructor Space


passing object Score ke object Enemy
For the Enemy objects constructor, we need to pass the score_field as its parameter:
private void prepare()
{
Ship ship = new Ship();
addObject(ship, 91, 221);
Score score_field = new Score ("Score:");
addObject(score_field, 50, 50);
addObject(new Enemy(score_field), getWidth() + 1 ,
Greenfoot.getRandomNumber(400));
addObject(new Enemy(score_field), getWidth() + 1 ,
Greenfoot.getRandomNumber(400));
addObject(new Enemy(score_field), getWidth() + 1 ,
Greenfoot.getRandomNumber(400));
}
in order to make the Score object accessible to the Enemy object. The reason for this
is that we will be passing the value of every new score from the Enemys collision
method, as explained below. Unless the Enemy object can access the Score object, it
will have no way of displaying the new score when it changes.

24. The Enemy class: Collecting


cones and incrementing the score
edit class Enemy
public class Enemy extends Actor
{
private Score scoreNum;
public static int count = 0;
public Enemy(Score score_field)
{
scoreNum = score_field;
}

25. The Score class: Creating a


method() to refresh the score
public void setText(String text)
{
GreenfootImage img = new
GreenfootImage(text, 24,
Color.WHITE, Color.BLACK);
setImage(img);
}

26. The Enemy class: Using the


collision event to increase the score
if (getOneIntersectingObject(Laser.class) != null)
{
getWorld().addObject(new Explosion(), getX(),
getY());
Greenfoot.playSound("explosion.wav");
Actor laser = getOneIntersectingObject(Laser.class);
getWorld().removeObject(laser);
resurrect();
count++;
scoreNum.setText("Score: " + count);
}

Check point 4. Ketinggalan ? Extract saja


project greenfoot yang ada di dalam file zip
CheckPoint4.zip

27. welcome screen. Add new


subclass world class Welcome
(welcome.png)

PS = anda bisa membuat banyak subclass World, misal untuk welcome screen,
vel 1, level 2, dll supaya World ditampilkan, klik kanan, New

28. Edit class Welcome, buat method


Act() Click Mouse to start!
public void act()
{
if
( Greenfoot.mousePressed(this) )
Greenfoot.setWorld( new
Space() );
}

Check point 5. Ketinggalan ? aja project


greenfoot yang ada di dalam file zip
CheckPoint5.zip

29. Background Suara. Edit


constructor class Space
private void prepare()
{
Ship ship = new Ship();
addObject(ship, 91, 221);
Score score_field = new Score ("Score:");
addObject(score_field, 50, 50);
addObject(new Enemy(score_field), getWidth() + 1 ,
Greenfoot.getRandomNumber(400));
addObject(new Enemy(score_field), getWidth() + 1 ,
Greenfoot.getRandomNumber(400));
addObject(new Enemy(score_field), getWidth() + 1 ,
Greenfoot.getRandomNumber(400));
GreenfootSound backsong = new
GreenfootSound("level1.mp3");
backsong.play();
}

30. Banyak Level


Buat static variabel di Class Space
public static int level = 1;
public static int skormenang = 10;
Edit constructor Space
private void prepare()
{
Ship ship = new Ship();
addObject(ship, 91, 221);
Score score_field = new Score ("Score:");
addObject(score_field, 50, 50);
GreenfootSound backsong = new GreenfootSound("level1.mp3");
backsong.play();
for(int i=0; i<level; i++)
addObject(new Enemy(score_field), getWidth() + 1 ,
Greenfoot.getRandomNumber(400));
}

31. Buat new subclass World class


Win
(win.png)

32. Cek sudah menang? Edit class


Enemy, method act(), tambahkan
if (count>= Space.skormenang)
{
int tmpnewlevel = ++Space.level;
int tmpskormenang =
Space.skormenang + 10;
Greenfoot.setWorld( new
Win(tmpnewlevel, tmpskormenang) );
}

33. Edit class Win


public class Win extends World
{
private int newlevel, newskormenang;
public Win(int newlevel, int newskormenang)
{
super(600, 400, 1);
this.newlevel = newlevel;
this.newskormenang = newskormenang;
}
public void act()
{
if(Greenfoot.mousePressed(this))
Greenfoot.setWorld( new Space(newlevel, newskormenang) );
}
}

34. Buat Constructor baru di class


Space

public Space(int newlevel, int


newskormenang)
{
super(600, 400, 1);
setPaintOrder(Ship.class, Laser.class);
prepare();
level = newlevel;
skormenang = newskormenang;
}

Check point 6. Ketinggalan ? aja project


greenfoot yang ada di dalam file zip
CheckPoint6.zip

34. Game Over!.. Create new subclass


World Gameover (gameover.png)

35. Edit class Ship method act()..


Collision dengan object Enemy = kalah
public void act()
{
moveUp();
moveDown();
moveRight();
moveLeft();
fire();
canShoot--;
if (getOneIntersectingObject(Enemy.class) != null)
{
getWorld().addObject(new Explosion(), getX(), getY());
Greenfoot.playSound("explosion.wav");
Greenfoot.setWorld( new Gameover(Space.level,
Enemy.count) );
}
}

36. Edit kode program class


Gameover
public class Gameover extends World
{
public Gameover(int level, int skor)
{
// Create a new world with 600x400 cells with a cell size
of 1x1 pixels.
super(600, 400, 1);

Score score_field = new Score ("Anda kalah di level:" +


level + " skor:" +skor);
addObject(score_field, 300, 50);
}
}
modifikasi variabel count di class enemy menjadi public supaya bisa dia
class Gameover

Check point 7. Ketinggalan ? aja project


greenfoot yang ada di dalam file zip
CheckPoint7.zip

Thankyou. Question? Ask me: Hatma


Suryotrisongko (0853.5900.7300)
suryotrisongko@gmail.com