Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java For this assignment you need to write a program using classes provided by t

ID: 3691948 • Letter: J

Question

Java

For this assignment you need to write a program using classes provided by the Processing package (specifically Applet class, but you may use other classes). Your program has to be interactive and it has to be developed using object oriented programming (i.e. things are represented using classes). Using good OOP practices will be a significant portion of your grade. Implement 'Pong' http://www.ponggame.org/ or 'Tic Tac Toe' http://en.wikipedia.org/wiki/Tic-tac-toe. The game should be two player. If you prefer you can create your own game. Talk to me in office hours or after class about your idea. Call your runnable program: MyGame. java. You may pick whatever names you wish for all other classes that you need.

Explanation / Answer

Duck.java
import processing.core.PApplet;

import processing.core.PImage;
public class Duck extends PApplet {
   public float x, y;
   private PApplet canvas;
   private PImage duck;

   public Duck(PApplet canvas) {
       this.canvas = canvas;
       // figure out our starting position
       this.x = random (100, 600);
       this.y = random (100, 600);
       // load in our artwork
       this.duck = this.canvas.loadImage("duck.png");

   }
   public void display() {
       this.canvas.image(this.duck, this.x, this.y);
   }
}

MyGame.java

import processing.core.PApplet;
import processing.core.PImage;

public class MyGame extends PApplet {
   public static void main (String args []){
       PApplet.main (new String [] {"MyGame"});
   }

   // start timer
   Bubble[] theBubbles;
   Duck duck;
   public boolean gameOver = false;
   private long start = System.currentTimeMillis();
   private long end = start + 10* 1000;

   public void setup()                       {
       duck = new Duck(this);
       // creates new duck
       size (900, 900);
       background (118, 204, 247); // blue background
       Bubble.width = 900;
       Bubble.height = 900;
       this.theBubbles = new Bubble [900];// create an array of bubble objects
       for (int i = 0; i < this.theBubbles.length; i++) {
           this.theBubbles[i] = new Bubble(random(0, width), random(0, height));
       }// for
   }// if

   public void draw() {
       // erase the background
       if (!gameOver){
           background(118, 204, 247);
           long currentTime = System.currentTimeMillis();
           duck.display();
           for (int i = 0; i < this.theBubbles.length; i++) {
               this.theBubbles[i].move();
               ellipse(this.theBubbles[i].x, this.theBubbles[i].y, 90, 90); // size of ellipses
           }// for}
           drawCursor();
           if (currentTime > end){
               background(118, 204, 247);
               duck.display();
               PImage loser = loadImage("loserscreen.png");
               image(loser, 130, 140); // you ran out of time
               return;
           }
           if (gameOver == true){
               // you won!
               background(118, 204, 247);
               duck.display();
               PImage winner = loadImage("winnerscreen.png");
               image(winner, 130, 140);
           }
       }
   }
   // draw
   public void drawCursor(){
       if (this.mousePressed){
           // bubbles in click area need to move!
           int mouserange;
           mouserange = 70;
           for (int q = 0; q <theBubbles.length; q++){ //for each bubble in ze array
               if (dist(mouseX,mouseY,theBubbles[q].x,theBubbles[q].y) < mouserange){
                   theBubbles[q].setSpeed((float)((theBubbles[q].x - this.mouseX)),
                           (float)((theBubbles[q].y - this.mouseY)));
               }// if loop

           }
           if ((mouseX >= duck.x && mouseX <= duck.x + 225) && (mouseY >= duck.y && mouseY <= duck.y + 215)){
               // if it fits within the png coordinate parameters
               this.gameOver = true;
           }
       }
   }

}//drawCursor//

Bubble.java
import processing.core.PApplet;
public class Bubble {
            // store my position
          // all balls should keep track of their x and y positions
          public float x;
          public float y;

          // all balls should have an x speed and y speed
          private float xSpeed;
          private float ySpeed;

          public void setSpeed(float x, float y) {
          
              if (x < -6) {
                  xSpeed = -6;
              } else if (x > 6) {
                  xSpeed = 6;
              } else {
                  xSpeed = x;
              }
          
              if (y < -6) {
                  ySpeed = -6;
              } else if (y > 6) {
                  ySpeed = 6;
              } else {
                  ySpeed = y;
              }
          
          }
      
          // the Ball class should keep track of the size of the world the balls live in
          public static int width;
          public static int height;
          // constructor
          public Bubble (float x, float y){
           // store my position
                this.x = x;
                this.y = y;

                // give me a random speed
                this.xSpeed = (float) (Math.random() * 4) - 3;
                this.ySpeed = (float) (Math.random() * 4) - 3;
              }

              // move a ball
              public void move() {
                this.x += this.xSpeed;
                this.y += this.ySpeed;

                // bounce!
                if (this.x > width) {
                  this.x = width;
                  this.xSpeed *= -1;
                }
                if (this.x < 0) {
                  this.x = 0;
                  this.xSpeed *= -1;
                }
                if (this.y > height) {
                  this.y = height;
                  this.ySpeed *= -1;
                }
                if (this.y < 0) {
                  this.y = 0;
                  this.ySpeed *= -1;
                }
          }
}
      

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote