GT LCC 6310 - The Computer as an Expressive Medium

Unformatted text preview:

1LCC 6310The Computer as an Expressive MediumLecture 7OverviewProgramming conceptsSubclassesProcessing, pp.320-322Methods that return valuesTalk about Assignment 2Project 1 - examples & discussionRevisit our exampleSo far we have a rocket that flies around in a field of asteroids…What if we want our rocket to be able to fire missiles?But we don’t want to get rid of our non-firing rocketWe can create a subclass!InheritanceSubclasses inherit fields and methods from parentWe create our subclass as follows:class ArmedRocket extends Rocket {…}Let's try to add this to our Rocket example in Processing…2Our subclass needs a constructorOur empty ArmedRocket example causes an errorProcessing doesn’t know how to construct an ArmedRocketWe want the ArmedRocket constructor to do the same work as the Rocket constructor ArmedRocket(int initialX, int initialY, float initialRot) {super(initialX, initialY, initialRot);}The keyword super means to call the method in the parent with the same name. Back to Processing…Now we have ArmedRocketWe can now use an ArmedRocket in our exampleInstead of creating an instance of Rocket, we can create an instance of Armed Rocket in Processing…But at the moment it’s basically just a copy of RocketThe only reasons to define an ArmedRocket are:To add new capabilities, or To override old onesSo add a fire() methodAn ArmedRocked should be able to fire missilesSo we want a fire method to draw a missile that shoots out of the rocketWe could have the fire method draw the missile…Is there a problem with this?Missiles should also be objectsThe object oriented solution is to make the missile an object as wellAll the different types of "things" in our domain should have a corresponding classLike asteroids and rockets, the missile class should know how todraw itselfA Missile is similar to a rocket (position, rotation, draw method, etc.)But its initial position will be the position of the ArmedRocket that fired itAlso, missiles aren't going to wrap around the display. Instead, they should travel on a straight path and disappear off-screen.Let's create a missile class...3The missile classclass Missile {final float velocity = 150;float xPos, yPos;float velocityX, velocityY;long lastDrawMillis;Missile(float initialX, float initialY, float rotation) {xPos = initialX; yPos = initialY;// get the missile's velocity in x and y directions based on its orientationvelocityX = sin(rotation) * velocity;velocityY = -cos(rotation) * velocity;lastDrawMillis = millis();}// Also needed: drawMe() and isVisible() methods...}Missile drawMe()void drawMe() {// update the current and last draw timeslong currentMillis = millis();float timeSinceLastDraw = ((float)currentMillis -(float)lastDrawMillis)/1000;lastDrawMillis = currentMillis;// update the position based on the velocity and the time// elapsed since the last time the asteroid was drawn xPos = xPos + velocityX * timeSinceLastDraw;yPos = yPos + velocityY * timeSinceLastDraw;pushMatrix();translate(xPos, yPos);ellipse(0, 0, 5, 5);popMatrix();}When to draw the missile?Missiles travel on a straight path, so we only want to draw a missile when it's within the bounds of the displaySo we'll need a method that checks if the missile is within boundsIt returns a boolean: true if in bounds, false otherwiseboolean isVisible() {// missiles don't wrap around so they are only visible when// they are within the bounds of the display windowif ((xPos > XSIZE) || (xPos < 0) || (yPos > YSIZE) || (yPos < 0))return false;elsereturn true;}The fire() methodNow that we have a missile object, our ArmedRocket.fire() method can just create and return a missile…Missile fire() {Missile m = new Missile(xPos, yPos, rotation);return m;}Now we need to add code in draw() to draw missiles…4But first, a place to store missilesFirst we'll need some variables to store the missilesWe can use an array like we did for AsteroidsWe'll also need a size for the array. This will be the maximum number of missiles that can be displayed at a timestatic final int MAX_MISSILES = 5;Missile[] m;We also need to create an array instance in our setup() methodm = new Missile[MAX_MISSILES];Now for the drawing part…Drawing missilesIn our draw() method, we want to draw all the existing missiles that have been fired by our ArmedRocketfor(int i = 0; i < MAX_MISSILES; i++) {if (m[i] != null) {m[i].drawMe();if (!m[i].isVisible())// delete missile when it goes off-screenm[i] = null;}}That's all great, but how do we fire?!Let's use the keystroke 'm' to fire a missileif (key == 'm') {// Only add a missile if there are less than MAX_MISSILES on screenprintln("Pressing the missile button");for(int i = 0; i < MAX_MISSILES; i++) {if (m[i] == null) {m[i] = r1.fire();break; // java keyword to break out of a loop - no reason to keep// looking for blank missile slots once we've found one}}}Where do we add this code?What happens if we put it where we currently check for other keystrokes?Using the keyPressed() methodSo far we've been checking for key presses in the draw() loopIf we fire missiles in the draw() loop, notice that we fire many times even if we push the key quicklyThis is because draw() is called many times a secondInstead we can use the keyPressed() methodkeyPressed() is a built in Processing method that is called when a key is pressed (similar to the mousePressed() method we've seen already)Useful because you can tie events to keystrokes instead of to draw()5keyPressed() methodvoid keyPressed() {// With keyPressed(), we only check to see if we should fire a missile if (key == 'm') {// Only add a missile if there are less than MAX_MISSILES on screen// Runs through the missile array checking for any empty slots (null)for(int i = 0; i < MAX_MISSILES; i++) {if (m[i] == null) {m[i] = r1.fire();break; // java keyword to break out of a loop - no reason to keep// looking for blank missile slots once we've found one}}}} Much better!! Now our ArmedRocket works as desired.Assignment 2Posted online, due FridayA2-01: Using beginShape() and endShape(), create a composition with five or more vertices.A2-02: Using beginShape() and endShape(), create a composition with ten or more vertices.A2-03: Create an image different from A2-02, but using the same vertex data.A2-04: Write a function with one parameter and demonstrate it visually.A2-05: Write a function for drawing triangles and visually demonstrate its flexibility.A2-06: Write a function with three or more parameters and visually demonstrate


View Full Document

GT LCC 6310 - The Computer as an Expressive Medium

Documents in this Course
Load more
Download The Computer as an Expressive Medium
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view The Computer as an Expressive Medium and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view The Computer as an Expressive Medium 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?