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

2014/5/13

Why does the snake start as 6 squares instead of as 1 square?

Tommy99 Tommy99

2014/5/13

#
Hello, I am creating a Snake/Worm game and am just about finished, but I have a problem with it. I want the snake to begin as one square, but it starts as a rectangular shape composed of 6 squares. Here is the code for the head: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Worm extends Actor { private final int size; private static final int EAST = 0; private static final int SOUTH = 90; private static final int WEST = 180; private static final int NORTH = 270; private int counter = 0; private int tailCounter = 0; public Worm(final int s) { switch( Greenfoot.getRandomNumber(4) ) { case 0: setRotation( EAST ); break; case 1: setRotation(WEST); break; case 2: setRotation(SOUTH); break; case 3: setRotation(NORTH); } size = s; GreenfootImage img = new GreenfootImage(size,size); img.fill(); setImage(img); setRotation(Greenfoot.getRandomNumber(4)*90); } public void act() { checkKeys(); moveForward(); tailCounter++; if( atEdgeOfWorld() ) { getWorld().removeObject(this); Greenfoot.stop(); return; }else{ Actor food = getOneIntersectingObject(Food.class); if(food!=null) { Tail.maxLife += 3; getWorld().addObject(new Food(20),Greenfoot.getRandomNumber(40),Greenfoot.getRandomNumber(30)); getWorld().removeObject(food); } } if(tailCounter==3) { getWorld().addObject(new Tail(),getX(),getY()); tailCounter=0; } } public void moveForward() { counter++; if( counter == 3 ) { move(1); counter = 0; } } public void checkKeys() { if( Greenfoot.isKeyDown("right") ) { setRotation(EAST); } if( Greenfoot.isKeyDown("down") ) { setRotation(SOUTH); } if( Greenfoot.isKeyDown("left") ) { setRotation(WEST); } if( Greenfoot.isKeyDown("up") ) { setRotation(NORTH); } } private boolean atEdgeOfWorld() { return getX()<=-1 || getY()<=-1 || getX()>=getWorld().getWidth()+0|| getY()>=getWorld().getHeight()+0; } } Here is the code for the tail: import greenfoot.*; public class Tail extends Actor { public static int maxLife = Ground.CELL_SIZE; private int lifespan; public Tail() { GreenfootImage img = new GreenfootImage(20,20); img.drawRect(0,0,20,20); img.fillRect(0,0,20,20); setImage(img); } public void act() { lifespan++; if (lifespan == maxLife) { getWorld().removeObject(this); } } } Here is the code for the World: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Ground here. * * @author (your name) * @version (a version number or a date) */ public class Ground extends World { public final static int CELL_SIZE = 20; /** * Constructor for objects of class Ground. * */ public Ground() { super(40, 30, 20, false); GreenfootImage img = new GreenfootImage(CELL_SIZE, CELL_SIZE); img.drawRect(0, 0, CELL_SIZE-1, CELL_SIZE-1); setBackground(img); img.fill(); int x = Greenfoot.getRandomNumber(40); int y = Greenfoot.getRandomNumber(30); int a = Greenfoot.getRandomNumber(40); int b = Greenfoot.getRandomNumber(30); addObject(new Worm(CELL_SIZE), x, y ); addObject(new Food(CELL_SIZE), a, b); } } Thank you very much!
patrick26 patrick26

2014/5/13

#
I think you should ask Mr. Rowan. Hope I helped! :)
patrick26 patrick26

2014/5/13

#
BTW use code tags...at the bottom of the page (under Post a Reply) press 'code' and then copy and paste your code into it.
patrick26 patrick26

2014/5/13

#
It will be much easier for people to read your code.
patrick26 patrick26

2014/5/13

#
LOL you copied my code!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
patrick26 patrick26

2014/5/13

#
BAHAHAHAHAHAHAHAHA
Tommy99 Tommy99

2014/5/13

#
patrick26 wrote...
LOL you copied my code!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I only used your code because mine wasn't working. The code in my project is still not your code though lol, I just wanted to see how to code it and I couldn't use my work because it didn't work. Derp.
patrick26 patrick26

2014/5/13

#
'
You need to login to post a reply.