Project 0

by Michael Hill @ 4:22 am 13 January 2010
GDE Error: Unable to load profile settings
int linePlacement = 50;  //Y placement of first curve
int spacing = 5;         //Spacing between each curve
int amplitude = 30;      //Height of wave
float period = 30/PI;    //Initial period of wave
float curveData [];      //Container for Y data for single curve
 
void setup(){
  size(600,587);
  noLoop();  //Lets program run only once
  smooth();  //Smooths drawn elemens
 
  curveData = new float[width]; //Stores data for single curve
 
  background(255); //Set background to white
  stroke(0);  //Curve color is Black
  strokeWeight(.75);
 
  //Calculate Y data for a single curve
  for(int i=0; i< numberOfCurves; i++){
    for(int j=1; j < curveData.length; j++){
     line(j-1, curveData[j-1] + linePlacement + spacing*i, j, curveData[j] + linePlacement + spacing*i);  //Draw line part of curve
    }
  }
}

Download Source

Pong Main Code:

//1 Player Pong - Michael Hill
 
Player player1;
 
Ball ball;
 
boolean gameOver = true;
int highScore = 0;
int currentScore = 0;
 
PFont font;
 
//Holds keys that are currently pressed
ArrayList keysHeld;
 
void setup(){
  size(800, 400);
  frameRate(60);
  font = loadFont("VectorBattle-25.vlw"); //Loads font
  textFont(font); //Sets current Font
 
  keysHeld = new ArrayList();
  ball = new Ball();
 
  player1 = new Player(1);
 
}
 
void draw(){
  background(255);
 
  //Print Scores
  fill(60);
  text("High Score:" + highScore, width-textWidth("High Score:" + highScore)-10, 30);
  text("Current Score:" + currentScore, width-textWidth("Current Score:" + currentScore)-10, 60);
  fill(255);
 
 
  //Set High Score if necessary
  if (currentScore > highScore){
    highScore = currentScore;
  }
 
  processKeys();
  player1.drawPlayer();
 
  testBounce();
 
  if (gameOver == true){
    fill(0);
    text("Press Space To Begin", width/2-textWidth("Press Space To Begin")/2, 150);
 
    fill(255);
  }
  ball.drawBall(); 
}
 
void testBounce(){
   if (ball.posX = 0){
      if(ball.posY-30 >= player1.posY && ball.posY-30 <= player1.posY + 50){
        ball.posX = 41;
        ball.dX *= -1;
        ball.dX += 2;
        ball.dY += random(-2,2);
      }
      currentScore++;
   }
   else if (ball.posY = height - 10){
     ball.dY *=-1;
     currentScore++;
   }
   else if (ball.posX + 10 >= width){
     ball.dX *= -1;
     currentScore++; 
   }
   if (ball.posX < 20){
     gameOver = true;
   }
}
 
//Handles Key presses by pushing them to an array
void keyPressed() {
  int arrayIndex = 0;
  int totalKeys = keysHeld.size();
 
  if (totalKeys <= 0){
    KeyPress pressed = new KeyPress(keyCode);
    keysHeld.add(pressed);
  }
 
  else {
 
    for(int i = 0; i<totalKeys; i++){
      println( "Key " + i + " being analyzed");
      KeyPress pressed = (KeyPress) keysHeld.get(i);
      println ( String.valueOf(keyCode) + " was compared to " + String.valueOf(pressed.pressed));
      if (pressed.pressed == keyCode){
        break;
      }
      arrayIndex = i + 1;
    }
 
    //If the loop finished without breaking, add key to list
    if (totalKeys == arrayIndex){
      KeyPress pressed = new KeyPress(keyCode);
      keysHeld.add(pressed);
    }
  }
}
 
 
//Pops key from array when released
void keyReleased(){
  for(int i = 0; i < keysHeld.size(); i++){
    KeyPress pressed = (KeyPress) keysHeld.get(i);
    if (pressed.pressed == keyCode){
      keysHeld.remove(i);
    }
  }
}
 
 
//Checks all current keys that are held and runs their actions
void processKeys(){
  for (int i = 0; i < keysHeld.size(); i++){
    KeyPress currentKey = (KeyPress) keysHeld.get(i);
    if (currentKey.pressed == UP) {
      player1.moveUp();
    } 
    if (currentKey.pressed == DOWN) {
      player1.moveDown();
    } 
    if (currentKey.pressed == 32 && gameOver == true) {
      ball.resetBall();
      gameOver=false;
      currentScore = 0;
    }
  }
}

Download Source
Play

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2016 Special Topics in Interactive Art & Computational Design | powered by WordPress with Barecity