Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

Part 1b: Add the Ball to the Screen

In Ball.java:

In MainClass.java:

  • Create a new Ball object. Do this in the run() method. There is already an instance variable called “ball”; initialize that variable in the run() method, by creating a new Ball object. DON’T create another ball variable for now (unless you want your game to have more than one ball object).
  • We advice that you change the type of the ball to GOval instead of Ball.
  • Create a method called randomFood(). In this method, you should set the location of the ball to a random x and random y. This method needs to make sure that the ball isn’t placed outside of the canvas. Hint: You can use setLocation() from the GOval class.
  • Make sure to add the ball to the canvas once it’s positioned correctly. 

Follow this tutorial to find out how to add objects. Remember that the Ball is a GOval (Ball extends GOval), and that SnakePart’s are GRect’s (SnakePart extends GRect).