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

2019/12/30

Need help with my Snake game

Sedrik Sedrik

2019/12/30

#
So I have this problem with my Snake holding to its length. After eating an apple he gets bigger for a while, but then he falls back to deafult size. Can anyone please help me with that issue? Heres my code:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Chvost here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Chvost extends Actor
{
    int x;
    /**
     * Act - do whatever the Chvost wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        //vymazuje koniec chvostu a zároveň ho zväčšuje
        int pocitadlo = this.getWorld().getObjects(Had.class).get(0).getPocitadlo();
        int score     = this.getWorld().getObjects(Had.class).get(0).getScore();
        if(pocitadlo> score)
        {
            getWorld().removeObject(this);
        }  
    }   
}
Sedrik Sedrik

2019/12/30

#
Also here is a code from class "Had" where I create variables mentioned higher in class "Chvost".
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Had here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Had extends Actor
{
    World svet = this.getWorld();
     
    private int pocitadlo=0;
    int speed  = 1;
    int player = 0;
    private int score=1;
    private int counter=0;
    public void ovladanie()
    {
        //ovladanie hada
     this.move(speed);
     if (Greenfoot.isKeyDown("up"))
     {
      this.setRotation(270);
     }
     if (Greenfoot.isKeyDown("down"))
     {
      this.setRotation(90);
     }
     if (Greenfoot.isKeyDown("left"))
     {
      this.setRotation(180);
     }
     if (Greenfoot.isKeyDown("right"))
     {
      this.setRotation(0);
     }
     
    }
    public void kolizia()
    {
        //pri naraze na seba, alebo na kraj mapy hrac prehra
        Lose l = new Lose();
        if(this.isAtEdge() || this.isTouching(Chvost.class))
        {
            svet.addObject(l,15,10);
            svet.removeObject(this);
          
        }
    }
    public int getScore()
    {
          
         if(this.isTouching(Jablko.class))
         {
             score=score+1;
              
         }
         return score;
    }
     
    public int getPocitadlo()
    {
          
         if(this.isTouching(Jablko.class))
         {
             pocitadlo = 0;
              
         }
         return pocitadlo;
    }
     
    /**
     * Act - do whatever the Had wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        //pridavanie chvostu
        pocitadlo++;
        this.getWorld().addObject(new Chvost(), getX(), getY());
         
        this.ovladanie();
        this.kolizia();
        //this.jedenie();
         
    }   
}
danpost danpost

2019/12/30

#
Once line 21 is true for any Chvost object, it will be true for all of them. So, if one is ever removed, all of them will be..
Sedrik Sedrik

2019/12/31

#
But when I put static number there instead of variable, it works. When there is static number snake gets bigger with bigger numbers. Once I put variable there it stops working.
danpost danpost

2019/12/31

#
Sedrik wrote...
But when I put static number there instead of variable, it works. When there is static number snake gets bigger with bigger numbers. Once I put variable there it stops working.
That is usual behavior for when the variables are in the wrong class -- but, I am not sure that is the case here. The if statements in your getScore and getPocitadlo methods are misplaced - or, at least the one in getScore is. Each Chvost instance will call these methods and the value of score, in particular, will be inflated (incremented once per Chvost instance in world during an act cycle the Had objects touches a Jablko object. Move and combine the if blocks into a separate method or put them (as one if block) in the act method itself.
You need to login to post a reply.