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

2012/11/29

Infinite Array

macforce macforce

2012/11/29

#
hey I know that theres been alot of discussions lately about this but I cant figure out how to do it. I want to make my bodypart array infinite, to do so I understand that I should use System.arraycopy and make a new one, one size bigger than bodypart . the problem is that I cant get it to work when I implement it in my world. thankful for tips! My world (background) import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class Snake here. * * @author (your name) * @version (a version number or a date) */ public class background extends World { int OneSnakePart = 100; int MAX_SNAKE_PARTS= 11; Snake bodypart = new Snake; int posX=200; int posY=200; int lastX; int lastY; int dx = -15; int dy = 0; private Food food; private int NumOfSnakes = 3; public background() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(400, 400, 1); GreenfootImage bg = new GreenfootImage(400,400); bg.setColor(Color.LIGHT_GRAY); bg.fill(); setBackground(bg); bodypart=new Snake(true); for(int i = 1; i<bodypart.length; i++) { bodypart=new Snake(false); } createSnake(); createFood(); //createEnemy(); } public void act() { moveSnake(); } public void createSnake() { addObject(bodypart, posX, posY); for(int i = 1; i<NumOfSnakes; i++) { posX=posX+12; addObject(bodypart, posX, posY); } } public void addSnakePart() { int i = NumOfSnakes; if (i<bodypart.length) { addObject(bodypart, bodypart.getX()+12, bodypart.getY()); NumOfSnakes++; moveSnake(); } } public void moveSnake() { lastX=bodypart.getX(); lastY=bodypart.getY(); bodypart.setLocation(lastX+dx, lastY+dy); // Move the head for(int i=1; i < NumOfSnakes; i++) { int x = bodypart.getX(); int y = bodypart.getY(); bodypart.setLocation(lastX,lastY); lastX = x; lastY = y; // get the body parts to follow the head } if(Greenfoot.isKeyDown("left")) { dx=-15; dy=0; } if(Greenfoot.isKeyDown("right")) { dx=+15; dy=0; } if(Greenfoot.isKeyDown("up")) { dx=0; dy=-15; } if(Greenfoot.isKeyDown("down")) { dx=0; dy=+15; } CheckCollision(); } public void CheckCollision() { int headX = bodypart.getX(); int headY = bodypart.getY(); if( headX > 397 || headX < 3) { Greenfoot.stop(); GreenfootImage GameOver = new GreenfootImage(400,400); GameOver.setColor(Color.BLACK); GameOver.fillRect(110, 120, 200, 150); GameOver.setColor(Color.WHITE); GameOver.drawString("GAME OVER", getWidth()/2-25,getHeight()/2); setBackground(GameOver); } if( headY > 397 || headY < 3) { Greenfoot.stop(); GreenfootImage GameOver = new GreenfootImage(400,400); GameOver.setColor(Color.BLACK); GameOver.fillRect(110, 120, 200, 150); GameOver.setColor(Color.WHITE); GameOver.drawString("GAME OVER", getWidth()/2-25,getHeight()/2); setBackground(GameOver); } for(int i=1; i < NumOfSnakes; i++) { if (headX == bodypart.getX() && headY == bodypart.getY()) { Greenfoot.stop(); GreenfootImage GameOver = new GreenfootImage(400,400); GameOver.setColor(Color.BLACK); GameOver.fillRect(110, 120, 200, 150); GameOver.setColor(Color.WHITE); GameOver.drawString("GAME OVER", getWidth()/2-25,getHeight()/2); setBackground(GameOver); } } } public void createFood() { Food food = new Food(); addObject(food, Greenfoot.getRandomNumber(380)+20, Greenfoot.getRandomNumber(380)+20); } public void createEnemy() { for (int i = 0; i < 3; i++) { Enemy enemy = new Enemy(); addObject(enemy, Greenfoot.getRandomNumber(380)+20, Greenfoot.getRandomNumber(380)+20); } } }
davmac davmac

2012/11/29

#
Please, paste code as code. Use the 'code' button/link below the text area.
// This is code 
And, explain what you mean by "can't get it to work". What goes wrong?
macforce macforce

2012/11/29

#
Alright davmac will do that in the future. To the problem, I was able to copy the array with system.arraycopy. I had a new array called newSnake but it was only one "slot" bigger than my bodypart array, I dont know how to, could we say update the arrays so that if I have 5 snake parts in bodyparts then newSnake will be "updated" to have 6 slots. I dont know if this was a good explanation of the problem, but what I want is that the snake should be able to grow to infinity
davmac davmac

2012/11/29

#
Ok, but your code above doesn't show the System.arrayCopy or the newSnake variable at all, so it's not possible to say what you're doing wrong.
macforce macforce

2012/11/29

#
Let´s start over then, this is as far I´ve come (and im not even sure that it´s even close to right.) Am I supposed to create a method in world that I can call from lets say when my snake eats the food? or should i create the array like I did with the other one (bodypart)?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Snake here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class background extends World
{   
    int OneSnakePart = 100;
    int MAX_SNAKE_PARTS= 11;
    Snake [] bodypart = new Snake[MAX_SNAKE_PARTS];
    Snake [] newSnake;
    int posX=200;
    int posY=200;
    int lastX;
    int lastY;
    int dx = -15;
    int dy = 0;
    private Food food;
    private int NumOfSnakes = 3;
    public background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(400, 400, 1); 
        GreenfootImage bg = new GreenfootImage(400,400);
        bg.setColor(Color.LIGHT_GRAY);
        bg.fill();
        setBackground(bg);

        bodypart[0]=new Snake(true);

        for(int i = 1; i<bodypart.length; i++)
        {
            bodypart[i]=new Snake(false);
        }
        createSnake();
        createFood();
        //createEnemy();
    }

    public void act()
    {
        moveSnake();  

    }

    public void newArray()
    {
        Snake [] newSnake = new Snake[bodypart.length+1];
        System.arraycopy(bodypart.length);   
    }

    public void createSnake()
    {   
        addObject(bodypart[0], posX, posY);    
        for(int i = 1; i<NumOfSnakes; i++)
        {   
            posX=posX+12;
            addObject(bodypart[i], posX, posY);
        }
    }

    public void addSnakePart()
    {   
        int i = NumOfSnakes;
        if (i<bodypart.length)
        {
            addObject(bodypart[i], bodypart[i-1].getX()+12, bodypart[i-1].getY());
            NumOfSnakes++;
            moveSnake();
        }
    }

    public void moveSnake()
    {   
        lastX=bodypart[0].getX();
        lastY=bodypart[0].getY();
        bodypart[0].setLocation(lastX+dx, lastY+dy); // Move the head
        for(int i=1; i < NumOfSnakes; i++)
        {  
            int x = bodypart[i].getX();
            int y = bodypart[i].getY();

            bodypart[i].setLocation(lastX,lastY);
            lastX = x;
            lastY = y; // get the body parts to follow the head
        }

        if(Greenfoot.isKeyDown("left"))
        {
            dx=-15;
            dy=0;
        }
        if(Greenfoot.isKeyDown("right"))
        {
            dx=+15;
            dy=0;
        }
        if(Greenfoot.isKeyDown("up"))
        {
            dx=0;
            dy=-15;
        }
        if(Greenfoot.isKeyDown("down"))
        {
            dx=0;
            dy=+15;
        }
        CheckCollision();
    }

    public void CheckCollision()
    {   
        int headX = bodypart[0].getX();
        int headY = bodypart[0].getY();
        if( headX > 397 || headX < 3)
        {
            Greenfoot.stop();
            GreenfootImage GameOver = new GreenfootImage(400,400);
            GameOver.setColor(Color.BLACK);
            GameOver.fillRect(110, 120, 200, 150);
            GameOver.setColor(Color.WHITE);
            GameOver.drawString("GAME OVER", getWidth()/2-25,getHeight()/2);
            setBackground(GameOver);
        }
        if( headY > 397 || headY < 3)
        {
            Greenfoot.stop();
            GreenfootImage GameOver = new GreenfootImage(400,400);
            GameOver.setColor(Color.BLACK);
            GameOver.fillRect(110, 120, 200, 150);
            GameOver.setColor(Color.WHITE);
            GameOver.drawString("GAME OVER", getWidth()/2-25,getHeight()/2);
            setBackground(GameOver);
        }
        for(int i=1; i < NumOfSnakes; i++)
        {
            if (headX == bodypart[i].getX() && headY == bodypart[i].getY())
            {
                Greenfoot.stop();
                GreenfootImage GameOver = new GreenfootImage(400,400);
                GameOver.setColor(Color.BLACK);
                GameOver.fillRect(110, 120, 200, 150);
                GameOver.setColor(Color.WHITE);
                GameOver.drawString("GAME OVER", getWidth()/2-25,getHeight()/2);
                setBackground(GameOver);
            }
        }
    }

    public void createFood()
    {
        Food food = new Food();
        addObject(food, Greenfoot.getRandomNumber(380)+20, Greenfoot.getRandomNumber(380)+20);
    }

    public void createEnemy()
    {
        for (int i = 0; i < 3; i++)
        {   
            Enemy enemy = new Enemy();
            addObject(enemy, Greenfoot.getRandomNumber(380)+20, Greenfoot.getRandomNumber(380)+20);
        }
    }
}
danpost danpost

2012/11/29

#
I think you need to look at the description of the System class arraycopyarraycopy method to see how it works.
davmac davmac

2012/11/30

#
I think you also need to think about what your variables are for. What is bodypart for and what is newSnake for? Why do you need both? Also, what is the newArray() method supposed to do?
You need to login to post a reply.