This site requires JavaScript, please enable it in your browser!
Greenfoot back
Starrqwas
Starrqwas wrote ...

2014/12/8

I am having trouble with a snake game

1
2
Super_Hippo Super_Hippo

2014/12/12

#
1
super(30,20,35,false);
Is this what you are looking for?
Starrqwas Starrqwas

2014/12/15

#
Yes! it works, how did I not realize that before? lol but thanks for the help Super hippo
Starrqwas Starrqwas

2014/12/17

#
I am still having the issue where the snake won't go through one side and come out the opposite side with the same X or Y axis
danpost danpost

2014/12/17

#
Just execute the following in the Snake head and body classes after moving them:
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());
which could be done this way:
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);
and even done this way:
1
setLocation((getX()+getWorld().getWidth())%getWorld().getWidth(), (getY()+getWorld().getHeight())%getWorld().getHeight());
Starrqwas Starrqwas

2014/12/19

#
Hmm... I wonder if having the head and body move methods being called from the main actor is causing it not to work. I have placed the code that you have provided in all move methods that I could find (3 in all ( the main actor, the head, and the body) ), but it still crashes into the wall.
danpost danpost

2014/12/19

#
What is the main actor? is it part of the snake? which part? What classes do these classes extend?
Starrqwas Starrqwas

2014/12/19

#
The main actor is "Ular" and these sub classes extend "Ular"
Starrqwas Starrqwas

2014/12/19

#
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());
     
    }
     
     
     
}
Starrqwas Starrqwas

2014/12/19

#
I pasted in the code that you had provided me, so that code is not normally in there.
danpost danpost

2014/12/19

#
I think your problem stems from the fact that you have movement control for each part of the snake instead of for the snake as a whole unit. Moving each part individually (by way of the act method of the parts) there is no coordination, or synchronization. You should probably use the world act method to control the snake (as a whole). I was wondering how the body parts know which way to continue. That is, if the head makes a turn, each body part should make that same turn at each successive act cycle (or timed cycle). What is the mechanism that performs this task in your scenario?
danpost danpost

2014/12/19

#
I was also wondering why you went through such an elaborate sequence of steps in your enum. It could be written as follows:
1
2
3
4
5
6
7
enum Arah {
    ATAS, BAWAH, KIRI, KANAN;
 
    public static Arah arahAcak()  {
        return values()[Greenfoot.getRandomNumber(values().length)];
    }
}
and 'List', 'Arrays', 'ArrayList', 'Collections' and 'Random' do not need imported. I do not see a need to import the 'Color' class either.
You need to login to post a reply.
1
2