Tuesday, May 12, 2015

Ping Pong 3(In class)

For the last few weeks in CSE we've been learning how to create a Ping Pong game. Below is the code taught to the class by Mr. Miles.

// create paddle
// create the ball
// update the screen every few milliseconds with position
// start in 1D


/* things that you can do:
ball/paddle goes off the screen, comes out the other side
paddles can't go off the screen
score - the game ends when you reach a certain amount?
balls/paddles speed up as the game goes on
balls added as game goes on
incorporate top and bottom
more than 2 players
make it pretty/add music
balls can run into each other
title screen/winning screen
create a computer opponent
fix any bugs
*/


float paddle1Y;
float paddle2Y;
float maxSpeed;
boolean[] paddleControl;
//Ball ball1;
//Ball ball2;


// Ball[] ballArray;
ArrayList<Ball> myList;


void setup() {
 size(600, 600);
 background(#000000);
 smooth();
 frameRate(30); // draw() 30 times/seconds


 paddle1Y = height/2 - 40;
 paddle2Y = height/2 - 40;
 maxSpeed = 5;
 paddleControl = new boolean[4];
 //ball1 = new Ball(50, 100);
 //ball2 = new Ball(50, 50, 25, 10, color(0, 0, 255));
 
 // println(ball1.getDiameter());
 // ballArray = new Ball[5];
 
 myList = new ArrayList<Ball>();
 myList.add(new Ball());


 for (int i = 0; i < paddleControl.length; i++) {
   paddleControl[i] = false;
 } // end of for
 
//  for(int i = 0; i < ballArray.length; i++){
//    ballArray[i] = new Ball();
//  } // end of for
} // end of setup


void draw() {
 background(#000000);
 fill(255, 0, 0);
 
 // move paddles
 if(paddleControl[0]){
   paddle1Y -= maxSpeed;
 }
 if(paddleControl[1]){
   paddle1Y += maxSpeed;
 }
 if(paddleControl[2]){
   paddle2Y -= maxSpeed;
 }
 if(paddleControl[3]){
   paddle2Y += maxSpeed;
 }


 // draw player 1
 rect(10, paddle1Y, 10, 80);


 // draw player 2
 rect(width - 20, paddle2Y, 10, 80);
 
  //ball1.update();
  //ball2.update();
 
 //println(ballArray.length);
 
//  for(int i = 0; i < ballArray.length; i++){
//    ballArray[i].update();
//  }


 // update balls
 for(int i = 0; i < myList.size(); i++){
//    Ball temp = myList.get(i);
//    temp.update();
     myList.get(i).update();
 } // end of update loop
 
} // end of draw


void keyPressed() {
 if (key == 'w' || key == 'W') {
   paddleControl[0] = true;
 }
 if (key == 's' || key == 'S') {
   paddleControl[1] = true;
 }
 if (keyCode == UP) {
   paddleControl[2] = true;
 }
 if (keyCode == DOWN) {
   paddleControl[3] = true;
 }
 if(key == ' '){
   myList.add(new Ball());
 }
} // end of keyPressed


void keyReleased(){
 if (key == 'w' || key == 'W') {
   paddleControl[0] = false;
 }
 if (key == 's' || key == 'S') {
   paddleControl[1] = false;
 }
 if (keyCode == UP) {
   paddleControl[2] = false;
 }
 if (keyCode == DOWN) {
   paddleControl[3] = false;
 }
} // end of keyReleased


No comments:

Post a Comment