1 | super ( 30 , 20 , 35 , false ); |


1 | super ( 30 , 20 , 35 , false ); |
1 2 3 4 | if (getX() < 0 ) setLocation(getX()+getWorld().getWidth(), getY(); if (getY() < 0 ) setLocation(getX(), getY()+getWorld().getHeight()); if (getX() >= getWorld().getWidth()) setLocation(getX()-getWorld().getWidth(), getY()); if (getY() >= getWorld().getHeight()) setLocation(getX(), getY()-getWorld().getHeight()); |
1 2 3 4 5 | int w = getWorld().getWidth(), h = getWorld().getHeight(); if (getX() < 0 ) setLocation(getX()+w, getY()); if (getY() < 0 ) setLocation(getX(), getY()+h); if (getX() >= w) setLocation(getX()-w, getY()); if (getY() >= h) setLocation(getX(), getY()-h); |
1 | setLocation((getX()+getWorld().getWidth())%getWorld().getWidth(), (getY()+getWorld().getHeight())%getWorld().getHeight()); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Point; import java.awt.Color; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; import java.util.Arrays; /** * Write a description of class Ular here. * * @author * @version 1.0 */ public abstract class Ular extends Actor implements BisaDiikuti { /** * Membuat enumerasi arah. */ enum Arah { ATAS, BAWAH, KIRI, KANAN; private static final List<Arah> VALUES = Collections.unmodifiableList(Arrays.asList(values())); private static final int SIZE = VALUES.size(); private static final Random RANDOM = new Random(); public static Arah arahAcak() { return VALUES.get(RANDOM.nextInt(SIZE)); } } /** * posisiAkhir, menunjukan posisi terakhir dari ular ini. * arah, menunjukan arah ular ini. * bagianBadan, bagian bagian badan yang akan bertambah bila ular ini makan. * * kita buat semua variabel disini private (privat) karena * variabel ini tidak digunakan oleh kelas lainnya (hanya digunakan oleh kelas ini). * hal ini bisa mengurangi penggunaan memori. */ protected Point posisiAkhir; protected boolean bisaDiikuti; public Point posisiAkhir() { return posisiAkhir; } public boolean bisaDiikuti() { return bisaDiikuti; } protected void move(Arah arah) { switch (arah) { case ATAS: setLocation(getX(), getY()- 1 ); break ; case BAWAH: setLocation(getX(), getY()+ 1 ); break ; case KIRI: setLocation(getX()- 1 , getY()); break ; case KANAN: setLocation(getX()+ 1 , getY()); break ; default : arah=Arah.KANAN; } if (getX() < 0 ) setLocation(getX()+getWorld().getWidth(), getY()); if (getY() < 0 ) setLocation(getX(), getY()+getWorld().getHeight()); if (getX() >= getWorld().getWidth()) setLocation(getX()-getWorld().getWidth(), getY()); if (getY() >= getWorld().getHeight()) setLocation(getX(), getY()-getWorld().getHeight()); } } |
1 2 3 4 5 6 7 | enum Arah { ATAS, BAWAH, KIRI, KANAN; public static Arah arahAcak() { return values()[Greenfoot.getRandomNumber(values().length)]; } } |