So, i am creating a snake game in greenfoot and i've run into a bit of a problem. My program is not doing anything from what i can see; a while ago it was running ok but half of it wasnt even coded back then. Now it isn't even showing anything. I've tried rewritting the code (different algorithms, etc.) but still nothing ive attached the code for the classes in this post so that you guys can take a look and see what is going on.
The world class:
The food class
The SnakeHead class
The SnakePart class
import greenfoot.*; import java.util.*; public class Main extends World { /** * Constructor for objects of class SnakeWorld. */ public Main() { // Create a new world with 500x500 cells with a cell size of 15x15 pixels. super(500, 500, 15); addObject(h, 250, 250); addObject(f, r.nextInt(500), r.nextInt(500)); //GreenfootImage baseImage = new GreenfootImage("board.jpg"); setPaintOrder(SnakeHead.class, SnakePart.class, Food.class); } public int i = 1; public SnakeHead h = new SnakeHead(); public SnakePart p = new SnakePart(0); public Food f = new Food(); public Random r = new Random(); public void act(){ h.act(); if(!f.exist()){ addObject(f, r.nextInt(500), r.nextInt(500)); } if(h.isDead() == true){ removeObject(h); for(int j = 1; j<=i; j++){ removeObject(p); } removeObject(f); } if(i > h.trailMax()){ for(int j = 1; j <= (h.trailCT - h.trailMax()); j++){ if(p.i < (h.trailCT - h.trailMax())){ removeObject(p); } } } } public SnakeHead head(){ return h; } public int snakePartNum(){ return i; } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Food extends Actor { public World w = getWorld(); /** * Act - do whatever the Food wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Food(){ exist = true; setImage("food.png"); } private boolean exist; public boolean exist(){ return exist; } //SnakeHead h = new SnakeHead(); public void act() { Actor snake = getOneObjectAtOffset(0, 0, SnakeHead.class); exist = true; if(snake != null){ exist = false; } } }
import java.util.*; import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class SnakeHead extends Actor { private World w = getWorld(); public int trail = 0; public int trailCT = 0; public SnakeHead(){ this.setImage("red.png"); setLocation(250,250); } /** * Act - do whatever the SnakeHead wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. **/ public void act() { control(); } private Main m = new Main(); private Food f = new Food(); public boolean foundFood(){ Actor food = getOneObjectAtOffset(0, 0, Food.class); if(food != null){ return true; } else { return false; } } public void eatFood(){ Actor food = getOneObjectAtOffset(0, 0, Food.class); if(food != null){ getWorld().removeObject(food); trailMax += 3; growSnake(); } } public int hr; public void collision(){ for(int i = 0; i <= m.snakePartNum(); i++){ if(intersects(m.p)){ die(); } } } public void control(){ while (!isDead()){ if(Greenfoot.getKey() == "up"){ setRotation(269); }else if(Greenfoot.getKey() == "down"){ setRotation(89); }else if(Greenfoot.getKey() == "left"){ setRotation(179); }else if(Greenfoot.getKey() == "right"){ setRotation(0); }else{ this.move(1); } hr = getRotation(); this.move(1); if(foundFood()){ eatFood(); } } } private boolean grow; public boolean growable(){ return grow; } private int trailMax = 2; public int trailMax(){ return trailMax; } public void growSnake(){ grow(true); if(trail <= trailMax){ trail++; for(; trail <= trailMax; trailCT++){ if (trail == 1){ getWorld().addObject(new SnakePart(trail), getX() , getY()); trail = 0; } } } } public void grow(boolean b){ grow = b; } private boolean dead = false; public boolean isDead(){ return dead; } public void die(){ dead = true; } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.*; public class SnakePart extends Actor { public Main w = new Main(); public SnakePart(int trailCT){ i = trailCT; exist = true; } public int i; /** * Act - do whatever the SnakePart wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { this.setImage("green.png"); setLocation(getX(),getY()); //while(!SnakeHead.isDead()){ //followPrev(); //} } public boolean exist; public SnakeHead sh = new SnakeHead(); public int r = getRotation(); //public int prevR = w.p(i-1).getRotation(); public void turn2faceP(SnakePart p){ setRotation((int)(180*Math.atan2(p.getY()-getY(),p.getX()-getX())/Math.PI)); } /* public void followPrev(){ //if(head != null){ // turn2face(head); /// if(this == w.p(0)){ //do nothing }else { while(!intersects(w.p[i-1])){ if(prevR = setLocation( } } }*/ }