SnakeBody = new Snake // // initialization of the Snakeįor( int i = 0 i < snakeBody.length i++)ĪddObject(snakeBody, 90+(snakeBody.Task 1 The impact of computer games on society Negative impact of computer games on society Score = new Counter("Score : ") // initialization of the score If(Greenfoot.getKey() !=null) // check if any key is pressedĮlse if ( gameState = 1) // Game Activate Stateįood = new Food() // initialization of the foodĪddObject(food, 0,0) // adding food actor to the world If(Greenfoot.isKeyDown("right")) // checks if right key is pressedĮlse if(Greenfoot.isKeyDown("down")) // checks if down key is pressedĮlse if(Greenfoot.isKeyDown("left")) // checks if left key is pressedĮlse if(Greenfoot.isKeyDown("up")) // checks if up key is pressed If( x!= snakeBody.getX() || y!=snakeBody.getY()) Condition to check food will not touch the snake Y = Greenfoot.getRandomNumber(getHeight()) įor( int i = 1 i < snakeBody.length i++) X = Greenfoot.getRandomNumber(getWidth()) SnakeBody = new Snake įor( int i = 0 i < snakeBody.length -1 i++)ĪddObject(snakeBody, x, y) Snake oldSnake = new Snake įor(int i = 0 i < snakeBody.length i++) * This method will update the length of the Snake. If( snakeBody.getX() = snakeBody.getX() & GrowSnake(previousLocationX, previousLocationY) įor ( int i = 1 i < snakeBody.length i++)// Checks if the snake eats itself If(snakeBody.getX()=food.getX() & snakeBody.getY()=food.getY()) // Checks if the snake eats food SnakeBody.move(20) // entire body moves one step If(snakeBody.getX()!=previousLocationX || snakeBody.getY()!=previousLocationY) // Checks if the snake hits walls Int previousLocationY = snakeBody.getY() Int previousLocationX = snakeBody.getX() UpdateFoodLocation() // updates the location of the food Create a new world with 600x400 cells with a cell size of 1x1 pixels. * Constructor for objects of class Playground. * Defines the variables required in game. * Write a description of class Playground here. Import greenfoot.* // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) So, we must ensure that the actor is still in the world before executing 'eat'. The 'return ' statements in (2) above only exits you from the 'move' method and puts you back in the 'act' method now, you do not want to execute the call to 'eat' when the object is not in the world. You do not want to execute any statements beyound the call to 'move' if the object is removed from the world. (3) if (getWorld() = null) return : this is related to (2) above. So, if you find that the object has hit the right or left edge of the world and you remove the object, and then try to see if it has hit the top or bottom edge, you will get the error because 'getX()' and 'getY()' cannot return a value if the actor is not in the world. The 'return ' statement immediately exits the method without any further instructions being executed. (2) the return satements: once you call die (and the object is removed from the world), you do not want to (and no longer can) do anything more with the object itself as far as moving, checking intersections, or checking world boundaries). 'removeObjects' will remove all the object in the list of objects that are in that world. 'getObjects(null)' will return a list of all objects in that world. Since 'removeObjects' and 'getObjects' are World methods (not Actor methods) we need to prefix them with 'getWorld()' which return the world object that the actor is in. (1) You wanted to remove all the objects from the world in the 'die' method.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |