This is a work in progress. It has been a pleasure working with Yang because he’s not afraid of a little challenge and will work hard to understand things that are not familiar to him. From the challenges we encountered, we learned a lot about programming from each other. Most of the challenges pertained to dealing with objects and functions.
Please check out this video clip! Next week, there will be a timer, and some other “game like” elements added. I really want to stick with this for my midterm because it will allow me to “K.I.S.S” but could be extended in many ways which will allow me to learn and practice key concepts of programming.
Ball Shooting Game – Version 4.0
import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
Minim minim;
Timer timer; // One timer object
AudioPlayer song, song2;
Ball [] myBall= new Ball[20];
float edgeLeftDistance;
float edgeRightDistance;
int ballAmt=0;
int ellipseX=0;
float ellipseY;
float ellipseY2;
int ellipseX2=0;
int dir=1;
int score;
PFont font;
void setup() {
size(640,480);
frameRate(20);
background(0);
timer=new Timer(300);
timer.start();
minim = new Minim(this);
song = minim.loadFile(“song.mp3″);
song2 = minim.loadFile(“bubblepop.mp3″);
song.play();
//int passedTime = millis()- savedTime;
//text(passedTime, 10, 50);
for(int i=0; i< myBall.length; i++){
myBall[i]=new Ball(color(random(255), random(255), random(255)),random(640),random(480),random(3), random(3));
}
}
void draw() {
background(255);
font = loadFont(“Arial-Black-48.vlw”);
textFont(font, 32);
text(score, 10, 30);
for(int i=0; i<myBall.length; i++){
myBall[i].drive();
myBall[i].display();
}
}
void mousePressed() {
for(int i=0; i< myBall.length; i++){
if (myBall[i].onScreen() && myBall[i].hitting(mouseX, mouseY)){
myBall[i].removeFromScreen();
score=score+dir;
song2.setVolume(5);
song2.rewind();
song2.play();
break;
}
}
}
class Timer {
int savedTime; // When Timer started
int totalTime; // How long Timer should last
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
// Starting the timer
void start() {
// When the timer starts it stores the current time in milliseconds.
savedTime = millis();
}
// The function isFinished() returns true if “totalTime” has passed.
// The work of the timer is farmed out to this method.
boolean isFinished() {
// Check how much time has passed
int passedTime = millis()- savedTime;
text(passedTime, 10, 50);
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}
class Ball {
color c;
float xPos, yPos;
float tempXpos;
float xSpeed, ySpeed;
boolean isOnScreen;
Ball(color tempC, float tempXpos, float tempYpos, float tempXspeed, float tempYspeed) {
c = tempC;
xPos = tempXpos;
yPos = tempYpos;
xSpeed = tempXspeed;
ySpeed = tempYspeed;
isOnScreen = true;
}
boolean onScreen() {
return isOnScreen;
}
void removeFromScreen(){
isOnScreen = false;
}
boolean hitting(float xHit, float yHit) {
if (pow((xHit – xPos), 2) + pow((yHit – yPos), 2) <= 400){
return true;
} else {
return false;
}
}
void display() {
stroke(0);
fill(c);
rectMode(CENTER);
noStroke();
if (isOnScreen){
ellipse(xPos,yPos,20,20);
}
}
void drive() {
xPos = xPos + xSpeed;
yPos = yPos + ySpeed;
if (xPos > width) {
xPos = 0;
isOnScreen = true;
}
if ( yPos > height) {
yPos = 0;
isOnScreen = true;
}
}
void stop()
{
song.close();
song2.close();
minim.stop();
}
//
//
}