I am attempting to make a game based off the arcade game Snake.
I will post my code below. I have been having numerous problems with it so far. I am here to discuss one that has to do with the parameters of an object. You can see in my code that in order to get the snake to move, I add a small object at the location of the head of the snake that tells the head which way to move. When the head eats food, 3 'body pieces' are added to the end of the head, and originally they inherit the same direction as the head. when the head turns it leaves behind its messaging object so the parts behind it can follow the same direction. I as of now do not have a problem if I happen to overlap and go over other "MoveDown's" as I call them (objects that tell which direction to go, while they don't only go down as the name suggests).
The problem comes when i do quick maneuvers. If i turn left and then up quickly, the whole snake starts to move in the direction that the last MoveDown indicated, in this case, up. I don't know why this is so, and when i check the variables on all the other MoveDown indicators on the map they all say they are now MoveDown's that would move the object up. Essentially when a new MoveDown is created all existing MoveDown's have their parameters changed to the latest ones. How can i fix this?
World class code:
Head Code:
MoveDown code:
Sorry about the pseudo 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 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 | 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 { String direct; /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 600 , 400 , 1 ); spawn(); } public void act() { spawn(); set(); } Head head = new Head(); int x, y; int count= 0 ; Food food = new Food(); public void spawn() { if (getObjects(Head. class ).size() < 1 ) { addObject(head, 308 , 210 ); } if (Greenfoot.isKeyDown( "down" ) && direct != "up" ) { addObject ( new MoveDown ( 0 , 14 ), head.getX() , head.getY()); direct = "down" ; } if (Greenfoot.isKeyDown( "up" ) && direct != "down" ) { addObject ( new MoveDown ( 0 , - 14 ), head.getX() , head.getY()); direct = "up" ; } if (Greenfoot.isKeyDown( "right" ) && direct != "left" ) { addObject ( new MoveDown ( 14 , 0 ), head.getX() , head.getY()); direct = "right" ; } if (Greenfoot.isKeyDown( "left" ) && direct != "right" ) { addObject ( new MoveDown (- 14 , 0 ), head.getX() , head.getY()); direct = "left" ; } //have a facing direction variable, if right, then can only go up or down, if up or down then turn it } public void set() { x = Greenfoot.getRandomNumber ( 593 ); y = Greenfoot.getRandomNumber ( 393 ); x = x - (x % 14 ); y = y - (y % 14 ); // x += 7; // y += 7; addObject(food, x , y ); } public void body() { //if y or x value is divisible by 5 then can turn } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Head here. * * @author (your name) * @version (a version number or a date) */ public class Head extends Actor { /** * Act - do whatever the Head wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Head() { GreenfootImage image = getImage(); image.scale(image.getWidth()- 135 , image.getHeight() - 135 ); setImage(image); } public static boolean isKill = false ; static boolean turn; public void act() { down(); body(); isTurn(); } int x,y; int mockx, mocky; int device; public void down() { MoveDown mdown = (MoveDown) getOneIntersectingObject ( MoveDown. class ); //MoveDown mdown = (MoveDown)getOneObjectAtOffset (0, 0, MoveDown.class); if (mdown != null ) { x = mdown.x; y = mdown.y; } setLocation (getX() + x, getY() + y); } public void body() { if (isTouching(Food. class )) { removeTouching(Food. class ); isKill = true ; for ( int i = 1 ; i < 4 ; i ++) { device = 14 * i; if (((MyWorld)getWorld()).direct == "right" ) { getWorld().addObject( new Head(), getX() - device, getY() ); getWorld().addObject( new MoveDown( 14 , 0 ), getX() - device, getY()); } } } else { isKill = false ; } } public void isTurn() { if (getX() % 14 == 0 && getY() % 14 == 0 ) { turn = true ; } else { turn = false ; } } } |
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 MoveDown here. * * @author (your name) * @version (a version number or a date) */ public class MoveDown extends Actor { /** * Act - do whatever the MoveDown wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ static int x; static int y; public MoveDown( int x, int y) {GreenfootImage image = getImage(); image.scale(image.getWidth() - 148 , image.getHeight() - 146 ); setImage(image); this .x = x; this .y = y; } public void act() { //dissapear(); } public void dissapear() { Head head = (Head)getOneObjectAtOffset( 0 , 0 , Head. class ); if (head == null ) { getWorld().removeObject( this ); } } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Body here. * * @author (your name) * @version (a version number or a date) */ public class Body extends Actor { public Body() { GreenfootImage image = getImage(); image.scale(image.getWidth()- 135 , image.getHeight() - 135 ); setImage(image); } public static boolean isKill = false ; public void act() { down(); } int x,y; public void down() { MoveDown mdown = (MoveDown)getOneObjectAtOffset ( 0 , 0 , MoveDown. class ); //MoveDown mdown = getObjectsInRange (10, MoveDown.class); if (mdown != null ) { x = mdown.x; y = mdown.y; System.out.println(x); System.out.println(y); } setLocation (getX() + x, getY() + y); } } |