![]() An expression that will be true 10 percent of the time could be: When you want to talk about the chance in percent, dealing with random numbers out a hundred is the easiest way to do it. It uses the < symbol.ģ.1 Try This: On your word document try to figure out an expression using getRandomNumber and < that will when you run it be true exactly ten percent of the time.ģ.2 Try this: Type up another expression that is true 7 percent of the time. Less than works by comparing two numbers and returning true if the first number is smaller (less than) the second. One way to do this is using a random number and a less-than operator. So we need to make an expression that will put in place of something–is–true that will return true in exactly 10 percent of the cases. That’s why you need to put the name of the class the method is in, then a dot, then the name of the method we want to use and it’s parameters (if any).Īlright, let’s say we want a ten percent chance that the crab will turn a bit off course every step. Dot notation means the method is not in the class we are using right now, but is in the class Greenfoot. Greenfoot.getRandomNumber(20) is different from the other methods we called before, it uses the dot notation. Means that you want Greenfoot to pick a random number for you between 0 and 19. With greenfoot the highest number possible is actually one number less than the limit and the lowest number possible is zero. When you tell someone to pick a number between 1 and 10, you are setting the limit to 10 (they will probably pick 7 by the way). The method getRandomNumber has to have a parameter that tells it how high the number can go (the limit of the number). Greenfoot can come up with a random number for us with getRandomNumber. One way to do that with Greenfoot is by using random numbers. The crab should actually go more or less in a straight line, but every now and then it should turn off course a little bit. While it walks, it goes in a straight line. Right now, the crab can walk across the screen and then turn when it hits the edge. This makes Crab a subclass of an Actor subclass called Animal. GameControllerĭon't write: if (grid.First, load the scenario Animalcrab from the handout folder. Then you don't have to initialize node to null in the declaration. ![]() In the method move(), your switch statement needs a default case where you set node to null. There's no reason to use this with fields unless you're disambiguating it from a parameter. This will do: private LinkedList body = new LinkedList() you don't need to repeat the generic type on the right-hand side. I this statement: private LinkedList body = new LinkedList() It's clearer and less likely to cause bugs when being maintained. If fact, I put braces around all the clauses in an if statement. Never put the else clause on the same on the same line as else. Private Snake initSnake() else isCovered = false Public boolean isDirectionChanged = false Private Direction snakeDirection = Direction.LEFT // initial direction is LEFT Return Math.abs(head.getX() - food.getX()) + Math.abs(head.getY() - food.getY()) = 0 ![]() Private LinkedList body = new LinkedList() Pretty much anything that will help me become a better programmer.Īlso, I have commented a few things out of pure guess, so if it appears as if I have gotten something wrong, please correct me!Īlso, any extra tips for my future projects would be much appreciated! I am asking those who are more advanced programmers to help me with implementing programming techniques, better organization, how to comment better, etc. I don't really want help on how to make the game better in visuals, levels, or anything like that. I am skeptical on how good of a programmer I am, and I code messy. I have rearranged the program into separate classes & methods. I have recently created a snake game in Java with the help of an online tutorial. I am a self-taught coder, and have been learning Java for the last 2 months.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |