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

2016/10/2

Snake game- score counter

nush99 nush99

2016/10/2

#
I am current making a snake game. Can someone please help me create a score counter? I want the score to increase by one point when the snake eats a piece of food. When the score reaches to 15, the game is over and the user wins. Here is the code: MyWorld-
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
 
     
    public MyWorld()
    {   
        super(30, 20, 20); //30 width, 30 height, 20x20 pixels
         
        GreenfootImage img= new GreenfootImage (20,20);
        img.drawRect(0, 0, 19, 19); //draw rectangle
        setBackground(img);
         
        addHead(); //add 'head' to world
         
        addFood(); //adding food to world
        addFood();
         
         
    }
     
    public void addHead()
    {
        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject(new Head(), x, y); //random placement of 'head'  
    }
     
    public void addFood()
    {
        int x = Greenfoot.getRandomNumber(getWidth()); //random x co-ordinate
        int y = Greenfoot.getRandomNumber(getHeight()); //random y co-ordinate
        addObject(new Food(), x, y); //random placement of food
    }
     
}
Tail:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color; //import colour class to set image as black
 
/**
 * Write a description of class Tail here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Tail extends Actor
{
    private int age = 0; //initial age of tail
    private int lifeSpan;
     
    public Tail(int lifeSpan)
    {
       GreenfootImage img = new GreenfootImage (20,20);
       img.fill(); //no colour specified- black
       setImage(img);
         
       this.lifeSpan = lifeSpan; //this=tail
    }
     
    public void act()
    {
        if (age == lifeSpan) {
            getWorld().removeObject(this);
        }
        age++;
    }
}
Food:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color; //import colour class to set image as red
 
/**
 * Write a description of class Food here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Food extends Actor
{
    public Food()
    {
        GreenfootImage img = new GreenfootImage(20,20);
        img.setColor(Color.RED);
        img.fill();
        setImage(img);
    }
     
    public void act()
    {
    }   
}
Head:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color; //import colour class to set image as black
 
/**
 * Write a description of class Head here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Head extends Actor
{
    private final int NORTH = 270;
    private final int SOUTH = 90;
    private final int EAST = 0;
    private final int WEST = 180;
     
    private final int SPEED = 10;
    private int counter = 0;
     
    private int foodEaten = 0;
 
    public Head()
    {
        GreenfootImage img = new GreenfootImage (20,20);
        img.fill(); //no colour specified- black
        setImage(img);
         
        setRotation(Greenfoot.getRandomNumber(4)*90); //gets a random number(0,1,2,3) and multiplies by 90 to get private final int)
    }
     
    public void act()
    {
        moveRandom();
        getKeys();
        if(isTouching(Food.class)) { //checks to see if actor is touching any other object
            removeTouching(Food.class); //removes object from class that the actor is touching
            foodEaten++;
            MyWorld world = (MyWorld)getWorld();
            world.addFood();
        }
         
        if ( facingEdge() ) {
            Greenfoot.stop(); //stop game if facing edge of world
        }
    }
       
    private boolean facingEdge()
    {
        switch(getRotation()) {
            case EAST: return getX()==getWorld().getWidth()-1;
            case SOUTH: return getY()==getWorld().getHeight()-1;
            case WEST: return getX()==0; //if x co-ordinate is o
            case NORTH: return getY()==0; //if y co-ordinate is o
        }
        return false;
    }
     
    public void moveRandom()
    {
       counter ++; //counter + 1
       if(counter==SPEED) { //speed = 10
               getWorld().addObject(new Tail(foodEaten*SPEED), getX(), getY());
               move(1); //move one cell = 20x20 pixels
               counter=0;
       }
        
    }
     
    public void getKeys()
    {
       if (Greenfoot.isKeyDown("right")) {
               setRotation(EAST);
       }
        
       if (Greenfoot.isKeyDown("left")) {
               setRotation(WEST);
       }
        
       if (Greenfoot.isKeyDown("up")) {
               setRotation(NORTH);
       }
        
       if (Greenfoot.isKeyDown("down")) {
               setRotation(SOUTH);
       }
    }
}
danpost danpost

2016/10/2

#
Whether you want a visible counter or not, you can check out my Value Display Tutorial scenario.
You need to login to post a reply.