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

2022/6/3

Snake

ronald ronald

2022/6/3

#
public class MyWorld extends World
{
    Snake snake = new Snake();
    
    private int xVel = 5;
    private int yVel = 2;
    private int speed = 2;
    
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld() {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1);
        snake.setImage(new GreenfootImage("smiley1.png"));
        addObject(snake, 1, 0);
        addBodySegment(0, 0, snake);
    }
    
    private void addBodySegment(int xLoc, int yLoc, Snake leader) {
        Snake body = new Snake();
        leader.setFollower(body);
        addObject(body, xLoc, yLoc);
    }
    
     public void act() {
        moveAndGrow();
        wallCollision();
    }
    
    private void moveAndGrow() {
        int atX = snake.getX();
        int atY = snake.getY();
        
        Snake body = snake;
        moveSnakeHead();
        
        while(body.getFollower() != null) {
            body = body.getFollower();
            int newX = body.getX();
            int newY = body.getY();
            body.setLocation(atX, atY);
            atX = newX;
            atY = newY;
        }
        
        if (Greenfoot.getRandomNumber(100) < (10))
            addBodySegment(atX, atY, body);
    }
    
    private void moveSnakeHead() {        
        snake.setLocation(snake.getX()+(xVel+speed), snake.getY()+(yVel+speed));
    }
    
    public void wallCollision() {
        if (snake.getX() < 40 || snake.getX() > 860) {
            xVel = -xVel;
        }
    
        if (snake.getY() < 40 || snake.getY() > 560) {
            yVel = -yVel;
        }
    }
    
}
hello I am making a snake with the danpost scenario it moves but it sticks the head and the body I guess you have to modify xloc and yloc it doesn't bounce either Thank you for your help
RcCookie RcCookie

2022/6/3

#
Why do you want the snake to bounce? What do you expect to happen? It will just run right into itself. Also I don’t see where you get any key input. Maybe also post the snake class code.
ronald ronald

2022/6/3

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Snake here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Snake extends Actor
{
    /**
     * Act - do whatever the Snake wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private Snake follower = null;
    
    public void act() {
        // Add your action code here.
    }
    
    public void setFollower(Snake follows) {
        follower = follows;
    }
    
    public Snake getFollower() {
        return follower;
    }
}
the snakes, it jumps to the neck no I say that for fun I would like the snake to bounce I don't see a coiled snake in the script, it just lies down I give you the code of the snake class
ronald ronald

2022/6/3

#
ok i just figured out some of the danpost code, the images are 20 pixels so it couldn't work with my snake so I redid my snake with the entire code of danpost I have the impression that the danpost snake is slower in movement than mine, mine moves very quickly, I have no time to turn right or left I'm wondering is it due to the size of world or the size of pixels and I don't know what offset is?? I looked on google, no more
You need to login to post a reply.