Final Project

Processing basketball game

f1

f2

code:

 

arduino:

void setup() {
Serial.begin(9600);     // initialize serial communication
}

void loop() {
// read the inputs

int xpos = analogRead(A0);
int ypos = analogRead(A1);

Serial.print(xpos);
Serial.print(“,”);                   // add a comma
Serial.println(ypos);
delay(10);                           // delay before sending the next set
}

 

processing:

/*
tweak from openprocessing example
http://www.openprocessing.org/sketch/104227
*/

import processing.serial.*;
Serial myPort;

PImage basketball, hoop, net, court;
float xpos, ypos, xvel, yvel, accel, n, xbasket, ybasket, boardHeight, give, space, i, wind;
int value = 0, dir = 1, level, score, hiscore, ballColor, comment;
boolean falling, Shoot, backboard, unlock1, unlock2, reset;
PShader blur;
void setup() {
size (1000, 800 );
basketball = loadImage(“Basketball.png”);
court = loadImage(“court2.png”);
hoop = loadImage(“hoop.png”);
xpos = width/5;
ypos = height-300;
yvel = .5;
xvel = .5;
accel = 2;
falling = false;
Shoot = false;
xbasket = random(400, 800);
ybasket = random(200, 600);
backboard = false;
give = 20;
score = 0;
space =  0;
level = 1;
hiscore = 0;
comment = 0;
unlock1 = false;
unlock2 = false;
ballColor = 4;
reset = false;

// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my  Arduino, so I open Serial.list()[0].
// Open whatever port is the one you’re using.
myPort = new Serial(this, Serial.list()[5], 9600);
// don’t generate a serialEvent() unless you get a newline character:
myPort.bufferUntil(‘\n’);
}

void draw() {
//background();
image (court, 0, 0, width, height);

fill(255);
if (level == 2) {
unlock1 = true;
}
if (level == 3) {
unlock2 = true;
}
boardHeight = 180 – level*30;
n = 180 – level*30;

noStroke();
fill(255);
if (score > hiscore) {
hiscore = score;
}
textSize(20);
text(“HIGHSCORE: ” + hiscore, 10, height – 70);
text(“SCORE: ” + score, 10, height – 30);
level = score/10 + 1;
text(“LEVEL: ” + level, 10, height – 50);
image(hoop, xbasket, ybasket, 150, 100);
println(score + “, ” + level);
if (xpos >= width||ypos >= height) {
reset = true;
}
if (xvel > 25) {
xvel = 25;
}
if (yvel >25) {
yvel = 25;
}
rect(0, 0, yvel*20, 5);
rect(0, 10, xvel*20, 5);
rect(xbasket+n, ybasket – boardHeight, 5, boardHeight+5);
if (Shoot == false||falling==false) {
xvel = (mouseX-width/5)/7;
yvel = (height-300 – mouseY)/7;
stroke(255);
line(xpos, ypos, xpos+xvel*5, ypos-yvel*5);
noStroke();
}
if (Shoot == true||falling==true) {
falling = true;
if (backboard) {
yvel -= 5 ;
xpos -= 1.25*xvel;
give = 40;
} else {
xpos+= xvel;
}

yvel -= accel;
ypos -= yvel;
}
if (xpos > xbasket-20 + n && xpos < xbasket + n + 30 && ypos > ybasket -boardHeight-5 && ypos < ybasket) {
backboard = true;
}
if (xpos>xbasket-10 && xpos<xbasket+20 && ypos < (ybasket + give) && ypos > (ybasket -10)) {
yvel = -yvel;
xvel = -xvel;
}
if (xpos>xbasket+20 && xpos<(xbasket+n) && ypos < (ybasket + give) && ypos > (ybasket -10) && yvel < 1 ) {
xpos = width/5;
ypos = height-300;
yvel = .1;
xvel = .1;
accel = 1;
falling = false;
Shoot = false;
ybasket = random(300, 700);
xbasket = random(400, 800);

//  n -=30;
backboard = false;
//  boardHeight-=30;
score += 1;
comment = score + 1;
}
if (ballColor == 4) {
fill(242, 98, 15);
}
if (ballColor == 1) {
fill(255, 0, 0);
}
if (ballColor == 2) {
fill(0, 255, 0);
}
if (ballColor == 3) {
fill(random(0, 255), random(0, 255), random(0, 255));
}

image (basketball, xpos, ypos, 40, 40);
/* if (unlock1 == true){
fill(255, 0, 0);
ellipse(int(width-40), int(height – 40), 30, 30);
fill(0,255, 0);
ellipse(int(width-80), int(height – 40), 30, 30);
fill(random(0,255),random(0,255),random(0,255));
ellipse(int(width-120), int(height – 40), 30, 30);
} */
if (reset == true) {
xpos = width/5;
ypos = height-300;
yvel = .1;
xvel = .1;
accel = 1;
n = 150;
falling = false;
Shoot = false;
ybasket = random(200, 500);
xbasket = random(400, 800);
backboard = false;
boardHeight = 150;
give = 15;
score = 0;
i = 0;
space = 0;
comment = 1;
reset = false;
}
}

void mouseClicked() {
if (value == 0) {
Shoot = true;
}
}

void keyPressed() {
if (key == ‘s’) {
Shoot = true;
}
if (key == ‘r’) {
reset = true;
}
if ( unlock1 == true) {
if (key == ‘c’) {
ballColor = 1;
}
if (key == ‘v’) {
ballColor = 2;
}
if (key == ‘b’) {
ballColor = 3;
}
if (key == ‘n’) {
ballColor = 4;
}
}
}

void serialEvent(Serial thisPort) {

// read the serial buffer:
String inputString = thisPort.readStringUntil(‘\n’);
println(inputString);

if (inputString != null)
{
// trim the carrige return and linefeed from the input string:
inputString = trim(inputString);

// split the input string at the commas
// and convert the sections into integers:
int sensors[] = int(split(inputString, ‘,’));

// if we have received all the sensor values, use them:
if (sensors.length == 2) {
// scale the sliders’ results to the paddles’ range:
mouseX = int(map(sensors[0], 0, 1023, 0, width));
mouseY = int(map(sensors[1], 0, 1023, 0, height));
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *