Gaming Assignment in Java

From Knowledge Kitchen
Jump to navigation Jump to search


Create a game using Processing in Eclipse.

  • You may work with up to one other student on this project. If you do so, include their name in the message when you submit your assignment.
    • If you do work with another student, your work must reflect the level of quality one would expect from two people.
  • Your game must include a photo of you, the author(s) somewhere logical in the game.
  • Include the name(s) of all authors in the Javadoc comments within each class.


Requirements

Documentation

You must document your code using javadoc comments for all classes, instance attributes, and instance methods.

Custom class

Your code must include custom classes that represents each visual object in your game.

  • If your game requires more than one object to be instantiated from any class, you may want to hold those objects in an array.


Overloaded constructors

One of your custom classes must contain at least 2 overloaded constructor methods.


Object orientated principles

  • Your code must follow the principles of abstraction and encapsulation.
  • Keep data fields private, and use getter and setter methods for any data fields that require external access.
  • Use the 'this' prefix when referring to instance properties or methods within the class they belong to (e.g. 'this.foo = 5;')
  • Use the class name prefix when referring to static properties or methods (e.g. 'Foo.bar();')

Interactive

Users must be able to interact with your game in some way and have the game visualization react meaningfully. Common interactions include clicking, hovering the mouse over a part of the visualization, or typing on the keyboard.


Example

Imagine you are recreating the classic arcade game, Asteroids, where asteroids are floating around, and the user moves a spaceship that shoots the asteroids and blows them up.

  • You would create a class called something like "PlayAsteroids", where you include the main logic of the game and where you instantiate all the other objects needed to play the game.
    • this class would override the Processing setup() and draw() methods
    • this class would instantiate all the Asteroid objects needed for the game and store them into an array (or better yet, an ArrayList).
  • You would create a custom Asteroid class that is used to instantiate each asteroid in the game
    • each asteroid would have instance variables representing its diameter, speed, x location, y location, etc.
    • each asteroid would have encapsulated methods to update its own position and draw itself to the screen
  • You would create a custom Spaceship class that represents the spaceship.
    • each spaceship would have instance variables representing its speed, orientation, x location, y location, etc.
    • each spaceship would have methods to rotate, thrust, and shoot.
    • each spaceship would have encapsulated methods to update its own position and draw itself to the screen
  • You would create a custom Bullet class that represents each bullet that the spaceship might shoot.
    • each bullet would have instance variables representing its speed,
    • each bullet would have encapsulated methods to update its own position and draw itself to the screen
    • each bullet would have an encapsulated method to check whether it has collided with any of the Asteroid objects.

..etc...

Code hints

Example of creating an array of custom objects from within your main program

Using a regular array:

//initialize an array of your custom data type
Asteroid[] asteroids = new Asteroid[200];

//loop through the existing array and create an Asteroid object to hold at each position in the array
for (int i=0; i<asteroids.length; i++) {
    //instantiate a Asteroid and store at this position in array
    Asteroid asty = new Asteroid( '''put your class's required constructor arguments here''' ); //create an Asteroid object
    asteroid[i] = asty; //add it to the array
}
//etc...


Alternate example of creating an ArrayList of custom objects from within your main program

Using an ArrayList

//initialize an ArrayList of your custom data type
ArrayList<Asteroid> asteroids = new ArrayList<Asteroid>(); //create an array list

//loop a fixed number of iterations, and create a Asteroid object and at it to the ArrayList with each iteration
for (int i=0; i<200; i++) {
    Asteroid asty = new Asteroid( '''put your class's required constructor arguments here''' ); //create an Asteroid object
    asteroids.add(asty); //add it to the ArrayList
}
//etc...

Example of looping through a regular Array and making all objects stored there draw themselves to the screen

This example assumes you have a regular Array named asteroids that stores Asteroid objects.

//loop through all Asteroid objects and make them appear on the screen
for (int i=0; i<asteroids.length; i++) {
     Asteroid asty = asteroids[i]; //get a pointer to point to the current object
     asty.show(); //make this Asteroid draw itself to the screen... obviously you need this show() method to exist in your Asteroid class
}


Example of looping through an ArrayList and making all objects stored there draw themselves to the screen

This example assumes you have an ArrayList named asteroids that stores Asteroid objects.

//loop through all Asteroid objects and make them appear on the screen
for (int i=0; i<asteroids.size(); i++) {
     Asteroid asty = asteroids.get(i); //get a pointer to point to the current object
     asty.show(); //make this Asteroid draw itself to the screen... obviously you need this show() method to exist in your Asteroid class
}

More code examples

Check out example code of interactive programming with Processing to see a simple program that involves game-like dynamics. Your code must be unique and your own - do not simply copy this example and make small changes to it.

Extra credit

Have one of your custom classes inherit from another custom class. This inheritance should be used meaningfully such that the object that inherits makes use or overrides the inherited methods or properties in a useful way.


What links here